kuist
asked on
function arg & pointer
Hi!
I made a function to initialize my program's setting (reading the config file and putting it into a struct)..
and the prototype is:
void InitializeSoft(config *myConfig, int *err);
config being my struct..
within InitializeSoft, a call like:
myConfig = readConfig();
which return a pointer of config type (prototype: config *readConfig(); )
now.. when InitializeSoft return to the calling function (main), I can't access myConfig's member..
I have an utility function that prints config to the screen .. and when
myConfig = readConfig();
returns within the InitializeSoft, if I call
PrintConfig(myConfig);
it works just well.. but if I do that in main (when InitializeSoft returned)
it displays "null"
my bet is that I probably pass it the wrong way to InitializeSoft.. maybe I should put ** instead of * ? if so, I'd appreciate the explication of why is that.. if not.. I'd appreciate the correct way & its explication :)
Thanks
I made a function to initialize my program's setting (reading the config file and putting it into a struct)..
and the prototype is:
void InitializeSoft(config *myConfig, int *err);
config being my struct..
within InitializeSoft, a call like:
myConfig = readConfig();
which return a pointer of config type (prototype: config *readConfig(); )
now.. when InitializeSoft return to the calling function (main), I can't access myConfig's member..
I have an utility function that prints config to the screen .. and when
myConfig = readConfig();
returns within the InitializeSoft, if I call
PrintConfig(myConfig);
it works just well.. but if I do that in main (when InitializeSoft returned)
it displays "null"
my bet is that I probably pass it the wrong way to InitializeSoft.. maybe I should put ** instead of * ? if so, I'd appreciate the explication of why is that.. if not.. I'd appreciate the correct way & its explication :)
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> a pointer is always on the stack because it a value like
> any int.. just that the pointer's value point to a
> defined place into memory, right ?
Yeah. When you call by reference, the value of the pointer still has to be pushed into the stack. The called function takes it from the stack like any other argument.
> any int.. just that the pointer's value point to a
> defined place into memory, right ?
Yeah. When you call by reference, the value of the pointer still has to be pushed into the stack. The called function takes it from the stack like any other argument.
Just to reinforce what has been said, repeat after me...
C ALWAYS passes by VALUE, and NEVER by REFERENCE.
Doing so has helped me greatly and will help you too <grin>.
So, while you can pass a pointer to a function, you are, in truth, not "passing by reference" as some people like to say, but you are simply passing a pointer. A pointer is a variable and the same rules apply.
C ALWAYS passes by VALUE, and NEVER by REFERENCE.
Doing so has helped me greatly and will help you too <grin>.
So, while you can pass a pointer to a function, you are, in truth, not "passing by reference" as some people like to say, but you are simply passing a pointer. A pointer is a variable and the same rules apply.
ASKER
ok..
but to make something clear into my mind.. in my problem mentionned above..
Will I need to use the * in anyplace in the code??
but to make something clear into my mind.. in my problem mentionned above..
Will I need to use the * in anyplace in the code??
observing the previous conversation friends i had a query if
readconfig() is returning a pointer to some variable or memory location which it doesn't accept as parameter...don't u feel that this is a memory leak waiting to explode!
i mean...
if
config **myconfig
...
readconfig(myconfig) //this statement would have been
//better
instead of
*myconfig = readconfig();
this is just a query i have forgotten these issues..i just happened to notice it...if anyone knows pls enlighten me...
thanks
cyrptosid
readconfig() is returning a pointer to some variable or memory location which it doesn't accept as parameter...don't u feel that this is a memory leak waiting to explode!
i mean...
if
config **myconfig
...
readconfig(myconfig) //this statement would have been
//better
instead of
*myconfig = readconfig();
this is just a query i have forgotten these issues..i just happened to notice it...if anyone knows pls enlighten me...
thanks
cyrptosid
ASKER
euh.. actually .. it's the same thing.. you still need to free the memory up after usage.
well no actually u didn't get my point...i read a chapters and found this..
what i am saying is that if you return a pointer to a variable or memory location from a function there is always a danger of memory leaks...
for example if you return a pointer to a variable within the function...but then as soon as the function exits the variable goes out of scope and hence is removed from the stack so what happens is that the pointer that is returned by the function is an invalid pointer which points to a junk location this is an example of Memory Leak...
Similarly if you allocate memory in a function and then return a pointer to that memory location...the pointer that is returned should be handled cautiously and should be FREED without fail..or else one can face unexpected problems...
cheers
cryptosid
what i am saying is that if you return a pointer to a variable or memory location from a function there is always a danger of memory leaks...
for example if you return a pointer to a variable within the function...but then as soon as the function exits the variable goes out of scope and hence is removed from the stack so what happens is that the pointer that is returned by the function is an invalid pointer which points to a junk location this is an example of Memory Leak...
Similarly if you allocate memory in a function and then return a pointer to that memory location...the pointer that is returned should be handled cautiously and should be FREED without fail..or else one can face unexpected problems...
cheers
cryptosid
> don't u feel that this is a memory leak waiting to explode!
According to returning a pointer, there are 2 valid patterns and 1 invalid pattern.
Pattern 1 (valid)
-----------------
void* called()
{
void *mem = malloc(..); // DYNAMIC ALLOCATION
return mem;
}
caller()
{
void *mem1;
mem1 = called();
free(mem1); // HAVE TO FREE IT
}
Pattern 2 (valid)
-----------------
void* called()
{
static char buffer[1000]; // STATIC ALLOCATION
...
return buffer;
}
caller()
{
void *mem1;
mem1 = called();
// DON'T FREE IT
}
Pattern 3 (INVALID)
-----------------
void* called()
{
char buffer[1000]; // STACK ALLOCATION
...
return buffer;
}
caller()
{
void *mem1;
mem1 = called();
// YOU GET JUNKS HERE
}
According to returning a pointer, there are 2 valid patterns and 1 invalid pattern.
Pattern 1 (valid)
-----------------
void* called()
{
void *mem = malloc(..); // DYNAMIC ALLOCATION
return mem;
}
caller()
{
void *mem1;
mem1 = called();
free(mem1); // HAVE TO FREE IT
}
Pattern 2 (valid)
-----------------
void* called()
{
static char buffer[1000]; // STATIC ALLOCATION
...
return buffer;
}
caller()
{
void *mem1;
mem1 = called();
// DON'T FREE IT
}
Pattern 3 (INVALID)
-----------------
void* called()
{
char buffer[1000]; // STACK ALLOCATION
...
return buffer;
}
caller()
{
void *mem1;
mem1 = called();
// YOU GET JUNKS HERE
}
>> myConfig = readConfig();
>> which return a pointer of config type (prototype: config *readConfig(); )
If it points to a location allocated by malloc (), then its fine, otherwise if it points to a local variable of the function, then the memory will be deallocated when the funciton finishes its execution.
Mayank.
>> which return a pointer of config type (prototype: config *readConfig(); )
If it points to a location allocated by malloc (), then its fine, otherwise if it points to a local variable of the function, then the memory will be deallocated when the funciton finishes its execution.
Mayank.
> If it points to a location allocated by malloc (), then its fine, otherwise if it points to a local variable of the function, then the memory will be deallocated when the funciton finishes its execution
Don't you read my third pattern ?
Don't you read my third pattern ?
I wrote that for kuist.... actually, there were so many comments that I didn't go through all of them so carefully.
Mayank.
Mayank.
ASKER
Thanks to everyone that posted comments on this question :)
ASKER
Thanks.