Allocate memory inside function ??
-
Hello I want to allocate memory for a void* pointer inside a function; i tried to do that by using malloc to the pointer passed as an argument ; the memory space was not kept after function return; how kan i do that thanks
please show a brief piece of the code you wrote for that...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Hello I want to allocate memory for a void* pointer inside a function; i tried to do that by using malloc to the pointer passed as an argument ; the memory space was not kept after function return; how kan i do that thanks
fx9200 wrote:
the memory space was not kept after function return;
You need to pass your pointer by reference. Otherwise it will still point at the previous memory location.
Cédric Moonen Software developer
Charting control [v1.1] -
please show a brief piece of the code you wrote for that...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
the code is too long but : #define STRUCT struct mystruct void function (void *input){ .... pointer = (STRUCT*) malloc (sizeof(STRUCT)); //filling in input attributes... input = pointer; } main(){ void *ptr; function (ptr); printf("%...", ptr->..........); } this work only if i allocate the memory for ptr in the main program
-
the code is too long but : #define STRUCT struct mystruct void function (void *input){ .... pointer = (STRUCT*) malloc (sizeof(STRUCT)); //filling in input attributes... input = pointer; } main(){ void *ptr; function (ptr); printf("%...", ptr->..........); } this work only if i allocate the memory for ptr in the main program
fx9200 wrote:
pointer = (STRUCT*) malloc (sizeof(STRUCT));
why don't you directly write into
input
?input = (STRUCT*) malloc (sizeof(STRUCT));
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
fx9200 wrote:
pointer = (STRUCT*) malloc (sizeof(STRUCT));
why don't you directly write into
input
?input = (STRUCT*) malloc (sizeof(STRUCT));
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
the code is too long but : #define STRUCT struct mystruct void function (void *input){ .... pointer = (STRUCT*) malloc (sizeof(STRUCT)); //filling in input attributes... input = pointer; } main(){ void *ptr; function (ptr); printf("%...", ptr->..........); } this work only if i allocate the memory for ptr in the main program
Change this to,
void function (void **input){
....
*pointer = (STRUCT*) malloc (sizeof(STRUCT));//filling in input attributes...
input = pointer;
}main(){
void *ptr;
function (&ptr);
printf("%...", ptr->..........);
}Prasad Notifier using ATL | Operator new[],delete[][^]
-
fx9200 wrote:
the memory space was not kept after function return;
You need to pass your pointer by reference. Otherwise it will still point at the previous memory location.
Cédric Moonen Software developer
Charting control [v1.1] -
fx9200 wrote:
the memory space was not kept after function return;
You need to pass your pointer by reference. Otherwise it will still point at the previous memory location.
Cédric Moonen Software developer
Charting control [v1.1]he seems to doing C, not C++... so no reference available (in the sense of C++ understands it). but pointers remain good
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
he seems to doing C, not C++... so no reference available (in the sense of C++ understands it). but pointers remain good
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
fx9200 wrote:
yes so ?
so, prasad_som[^] gave you an answer... hve you read it ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Hello I want to allocate memory for a void* pointer inside a function; i tried to do that by using malloc to the pointer passed as an argument ; the memory space was not kept after function return; how kan i do that thanks
I tried this it works , but i cannot fix the allocated emory size by a no-static way void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st; }
-
Though, its not applicable in
C
context. You need to understand theory behind Cedric's suggesstion. In you original code, you was passing pointer argument. Though its a pointer, it will be passed by value, and inside that function, memory allocated will be not at address you expected. In that case you need to pass pointer to pointer, as shown in my previous reply.Prasad Notifier using ATL | Operator new[],delete[][^]
-
fx9200 wrote:
yes so ?
so, prasad_som[^] gave you an answer... hve you read it ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Though, its not applicable in
C
context. You need to understand theory behind Cedric's suggesstion. In you original code, you was passing pointer argument. Though its a pointer, it will be passed by value, and inside that function, memory allocated will be not at address you expected. In that case you need to pass pointer to pointer, as shown in my previous reply.Prasad Notifier using ATL | Operator new[],delete[][^]
-
Which code you are talking about ?
Prasad Notifier using ATL | Operator new[],delete[][^]
-
I tried this it works , but i cannot fix the allocated emory size by a no-static way void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st; }
void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st;
-
Which code you are talking about ?
Prasad Notifier using ATL | Operator new[],delete[][^]
void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st; }
-
void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st; }
Here, though you are allocating memory for
ch
member ofmystruct
inside the function, you have passedmystruct*
as parameter and , for that memory is already allocated, before calling this function. And bitwise(shallow) copy happens in this case. p.s. Dont's create multiple threads for same code.Prasad Notifier using ATL | Operator new[],delete[][^]
-
Here, though you are allocating memory for
ch
member ofmystruct
inside the function, you have passedmystruct*
as parameter and , for that memory is already allocated, before calling this function. And bitwise(shallow) copy happens in this case. p.s. Dont's create multiple threads for same code.Prasad Notifier using ATL | Operator new[],delete[][^]