?
Solved

function arg & pointer

Posted on 2003-03-25
13
Medium Priority
?
334 Views
Last Modified: 2010-04-15
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


0
Comment
Question by:kuist
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 5

Accepted Solution

by:
Kocil earned 500 total points
ID: 8206285
You right.
It should be
void InitializeSoft(config* *myConfig, int *err)
{
   ..
   *myConfig = readConfig();
   ..
}


and you call it from
main(){
   config* mainconfig=NULL;
   ...
   InitializeSoft(&mainconfig, &err);
   PrintConfig(mainconfig);
   ...
}


The explanation:
void InitializeSoft(config *myConfig, int *err);

the mainconfig is passed by value. It mean the myconfig itself is a variable on the stack, and you initialized it with the value of mainconfig (NULL). When you change it then myconfig is changed, but not the mainconfig


void InitializeSoft(config* *myConfig, int *err);

the mainconfig is passed by refernce. It mean the myconfig will contain the address of mainconfig (&mainconfig). When you change the it then the mainconfig changed;

0
 

Author Comment

by:kuist
ID: 8206495
lol.. that's what I thought, but I was also sure that a pointer was passed by ref and not by val... in fact, 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 ?

Thanks.

0
 

Expert Comment

by:MHuang
ID: 8206551
> 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.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:gj62
ID: 8206806
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.



0
 

Author Comment

by:kuist
ID: 8206946
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??
0
 
LVL 5

Expert Comment

by:cryptosid
ID: 8207054
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
0
 

Author Comment

by:kuist
ID: 8207225
euh.. actually .. it's the same thing.. you still need to free the memory up after usage.
0
 
LVL 5

Expert Comment

by:cryptosid
ID: 8207397
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
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8207934
> 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
}


0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8208216
>> 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.


0
 
LVL 5

Expert Comment

by:Kocil
ID: 8208257
> 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 ?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8208292
I wrote that for kuist.... actually, there were so many comments that I didn't go through all of them so carefully.

Mayank.
0
 

Author Comment

by:kuist
ID: 8211968
Thanks to everyone that posted comments on this question :)

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question