Dynamically allocated pointer data storage - pointers

int* p = new int;
Hello everyone!!!
I have a question about the pointer, in the memory area, what is the size of the above statement? pointer p is 8 BYTE, new int is 4 BYTE so the sum is 12 BYTE? Hope everyone answers.

Related

Can I assign a #define value to Char*?

I have been writing a C - programm. Where I have a structure with char* members.
#define SS_Value_1 "Value for SS1"
#define SS_Value_1 "Value for SS2"
struct aSamplestruct {
char* s1;
char* s2;
}aSample;
aSample ss;
fun1( &aSample );
I am sending the structure point to a function and I know the best practice is to allocate memory to S1 and copy the string what ever we want and free the allocated memory after usage, As shown below
ss->s1 = (char*) MEM_alloc(sizeof(char) * (strlen(SS_Value_1) + 1);
strcpy(ss->s1, SS_Value_1);
use the variable ss.s1 in a report and do mem free.
MEM_free(ss.s1);
Its working fine no worries. but I have to write the same piece of code for some 10 char* members in 36 different conditions.
The other way is that, without allocating any memory I am able to directly assign the #define values to my structure members as below.
ss->s1 = SS_Value_1;
use this variable in a report and no need to free any memory.
this way also fine, no problems in a sample execution.
what I would like to know is
whether this will cause any memory leaks ?
will it stop executing for large data ?
Thanks in advance
Regards,
Sudhir
This is similar to
char *str;
str = "abc";
That is, you are declaring a char pointer, creating a string literal and assigning its address to the pointer. should work for any amount of data.

Memory allocation for Pointer

In which section memory is allocated if I write something like
1. int *ptr;
*ptr = 22;
2. int *ptr = new int(22);
What I Understand is when we use keyword new then memory is get reserved into Heap and that reserved memory address is get returned .
But what happened in case we didn't use keyword new ?? Where memory is get allocated ??
is Both Syntax is Same ?? If No, what is Exact difference between these two statement ??
You code examples can be rephrased as follows:
1st:
int * ptr;
*ptr = 22;
2nd:
int * ptr;
ptr = new int; //the only difference
*ptr = 22;
What happens in the second one:
int * ptr; means create variable capable of storing address of int variable. For now variable isn't initialized, so it stores garbage. If you interpret garbage as pointer, it can points anywhere (it can be 0, or 0xabcdef11, or 0x31323334, or literally ANYTHING which is left on non-cleared memory form previous usage)
ptr = new int; means "allocate memory area capable of holding int and store its address in ptr variable". Since this line, ptr points to specific memory
*ptr = 22; means put value 22 to memory pointed by ptr.
In the first example you create variable, but don't initialize it. ptr contains garbage, but you ask to interpret it as address and store 22 to this address. What can happen:
address is invalid (e.g. 0, or out of address range, or points to protected memory) => program crashes
address is valid and writable, but memory area is used by another part of the program: you'll write 22, but it will corrupt someone's data, result totally unpredictable.
address is valid and writable, memory area isn't in use. You'll write 22, but you aren't guaranteed to read it back. Memory can become used for different purpose and 22 will be overwritten.
anything else. All this is actually an undefined behavior, everything is possible.
That's why it's always recommended to initialize pointer immediately:
int * ptr = NULL; //or better "nullptr" starting from C++11
Attempt to store value *ptr = 22; will at least explicitly crash the program.

Conversion with Pointsers in C

I need to implement but I am not sure how can I as I am completely new into this. A function called get_values that has the prototype:
void get_values(unsigned int value, unsigned int *p_lsb, unsigned int *p_msb,
unsigned int *p_combined)
The function computes the least significant byte and the most significant byte of the value
parameter. In addition, both values are combined. For this problem:
a. You may not use any loop constructs.
b. You may not use the multiplication operator (* or *=).
c. Your code must work for unsigned integers of any size (4 bytes, 8 bytes, etc.).
d. To combine the values, append the least significant byte to the most significant one.
e. Your implementation should be efficient.
The following driver (and associated output) provides an example of using the function you are
expected to write. Notice that in this example an unsigned int is 4 bytes, but your function
needs to work with an unsigned int of any size.
Driver
int main() {
unsigned int value = 0xabcdfaec, lsb, msb, combined;
get_values(value, &lsb, &msb, &combined);
printf("Value: %x, lsb: %x, msb: %x, combined: %x\n", value, lsb, msb, combined);
return 0;
}
Output
Value: abcdfaec, lsb: ec, msb: ab, combined: abec
I think you want to look into bitwise and and bit shifting operators. The last piece of the puzzle might be the sizeof() operator if the question is asking that the code should work with platforms with different sized int types.

Multi-gpu allocation through another function [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Using CUDA, I want to allocate memory for different arrays, one for each GPU from a different function than main(), but I must have missed something in regard to pointer arithmetic. Here's what I thought,
void InitThisMemory(int***, int N, int Nout, size_t* pitch, int height, int width); // This function's purpose is to initialize A and the pitch
int main(void){
int** A;
int N = 10;
int NOut = 2;
int height = 2, width = 2;
size_t pitch;
InitThisMemory(&A, N, NOut, &pitch, height, width);
return 0;
}
InitThisMemory(int ***A, int N, int Nout, size_t* pitch, int height, int width){
int i;
*A = (int**)malloc(Nout * sizeof(int*));
for(i = 0;i < Nout;i++){
cudaSetDevice(i);
cudaMallocPitch((void**)&(*A[i]), &(*pitch), width, height);
}
}
Disclaimer: Not my actual code but this should reproduce the error. Let me know if I missed an allocation of a variable somewhere.
Why do I think that the problem is in the arithmetic? Simply because this works pretty well if Nout = 1 (which means that I am using only one device).
Any ideas?
Your bug, I think, is writing (void**)&(*A[i]) instead of (void **) (&(*A)[i]), but I recommend you refactor as follows:
use a local int ** variable to hold the malloc() return value;
use that local in your call to cudaMallocPitch();
pass back the malloc() return value only if all cudaMallocPitch() calls succeed.
If you do these things, then it will be simpler to write correct cleanup code in the event that one of the cudaMallocPitch() calls fails, and you needn't propagate the passback unless everything has succeeded.

add value to struct to pointer segmentation error in C

people, i've an issue now..
#include <stdio.h>
#include <stdlib.h>
typedef struct a
{
int *aa;
int *bb;
struct b *wakata;
}a;
typedef struct b
{
int *you;
int *me;
}b;
int main()
{
a *aq;
aq = (a*)malloc(sizeof(a*));
*aq->wakata->you = 1;
*aq->wakata->me = 2;
free(aq);
return 0;
}
and compiled, then debugged :
gcc -o tes tes.c --debug
sapajabole#cintajangankaupergi:/tmp$ gdb -q ./tes
Reading symbols from /tmp/tes...done.
(gdb) r
Starting program: /tmp/tes
Program received signal SIGSEGV, Segmentation fault.
0x08048414 in main () at tes.c:22
22 *aq->wakata->you = 1;
well, the question is, how to set the value to variable inside struct 'b' through struct 'a' ?
anyone ?
The initial allocation of a is only allocating 4 bytes (in a 32-bit architecture). It should be:
aq = (a*)malloc(sizeof(a));
And wakata has not been initialized: Maybe this:
aq->wakata = (b*)malloc(sizeof(b));
And it will need a corresponding free as well prior to the free of aq.
free(aq->wakata);
And since you have pointers to the integers, those would also need to be allocated (you and me). But it is not clear if that is your goal. You probably should remove the * from the int declarations so that they are simply int members rather than the pointers to int.
Looks like you have a few mistakes here. See the code below.
In general a few things to keep in mind. You can't access memory before you malloc it. Also, there is a difference between memory and pointers e.g. int and int *
#include <stdio.h>
#include <stdlib.h>
typedef struct a
{
int aa;
int bb;
struct b *wakata;
}a;
typedef struct b
{
int you;
int me;
}b;
int main()
{
a * aq = malloc(sizeof(a));
aq->wakata = malloc(sizeof(b))
aq->wakata->you = 1;
aq->wakata->me = 2;
free(aq->wakata)
free(aq);
return 0;
}
wakata isn't pointing to any valid memory. You have to malloc memory for it, and then also for wakata->you and wakata->me
Pointers do not contain data. They point at data. That is why they are called pointers.
When you malloc enough space to store an a instance named aq, you allocate space for the pointers contained in that structure. You do not cause them to point at anything, nor do you allocate space to contain the things that they would point at.
You're not allocating space for b in struct a. You have defined 'a' as holding pointers, not structs. Also, I think malloc(sizeof(a*)) should be malloc(sizeof(a))
aq = (a*)malloc(sizeof(a)); // You should probably use calloc here
aq->wakata = (b*)malloc(sizeof(b));
you and me don't seem to need to be pointers, just normal ints
You have some problems with your code.
When you allocate memory for the struct a, you should do
aq = (a*)malloc(sizeof(a));
You now allocated memory for the struct a, but not for the struct b pointed by the wakata member, so you need to do
aq->wakata = (b*)malloc(sizeof(b));
Finally, in the struct b there should not be int* members, but int members. This way, you'll be able to correctly assign a value to them.
Remember that you should check for the correct allocation of memory by checking if the malloc return value is not NULL.

Resources