Allocate memory inside function ??
-
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[][^]
-
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; }
What I think you need to understand after having read your previous posts, is that when you call a function the arguments passed are copies on the stack. Have a look here[^] if you're not sure what the stack is and how it is used. Let's examine this with some code examples. A function called
AddOne
is used for increasing a variable by the value of 1.void AddOne( int nTheVariable ) { nTheVariable = nTheVariable + 1; } void main() { int nMyValue = 0; printf( "The value is %d.\n", nMyValue ); // Prints "The value is 0." AddOne( nMyValue ); printf( "The value is %d.\n", nMyValue ); // Also prints "The value is 0." }
When
AddOne()
is declared as above, the value ofnMyValue
is copied to the stack and read byAddOne()
. When the function returns is has indeed increased its local copy (nTheVariable
) by one, but nMyValue still remains the same since it was a copy of it that was passed to the function. If you want to alter the value ofnMyValue
, you have to pass its location as argument toAddOne()
. "Location" in this aspect means "the address of". In code it would look like this:void AddOne( int* pnTheVariable ) { // Here we've got a copy of the address of the variable so // we can modify it at its original location *pnTheVariable = *pnTheVariable + 1; } void main() { int nMyValue = 0; printf( "The value is %d.\n", nMyValue ); // Prints "The value is 0." AddOne( &nMyValue ); // Pass a copy of the address of nMyValue printf( "The value is %d.\n", nMyValue ); // Prints "The value is 1." }
In your case you want to allocate memory and you always assign it to a pointer. To be able to alter the address assigned to a pointer, you have to pass a copy of the address of the pointer to the function. This is still the same as above, but now you want to alter the value of a pointer. In code it would look something like this if you want to allocate memory for three
int
s:void Allocate( int** ppnTheMemory ) { *ppTheMemory = (int*)malloc( 3 * sizeof( int ) ); } void main() { int* pnMyValues = NULL; Allocate( &pnMyValue ); // Pass the address of the pointer if( pnMyValues ) { free( pnMyValues );
-
fx9200 wrote:
how to pass reference(pointer) by reference ? please give example
See here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb