Simple pointer/function question

I'm having a problem getting a pointer to return a valid value when called as a parameter to a function. Here goes.

MyFunction(MyStruct *p)
{
  p = APIFunctionThatReturnsPointerToADatabase();
}

MyStruct *p = 0;

MyFunction(p);

The value of p is valid inside the function but not after the function call. Why? Aren't I calling by reference?
boomerang061797Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

allymCommented:
You are probably returning a pointer to a local variable.  To be sure we need to know what happens inside APIFunctionThatReturnsPointerToADatabase();

Here's an example of wrong use of pointers:

int* ReturnAnIntPtr()
{
  int Result = rand() % 256;
  return &Result;
}

Now, Result is only valid inside the function call.  Once it goes out of scope (after the "}") the memory is no longer valid, and hence the value is no longer valid.

To get around it you can either use new to allocate memory on the heap, or have your function use storage passed in by the caller.  For my int example, you'd have either
int* ReturnAnIntPtr()
{
  int* result = new int;
  *result = rand()%256;
  return result;
}

Or
void InitialiseAnInt(int* result)
{
  *result = rand()%256;
}
And use this as
int myResult;
InitialiseAnInt(&myResult);

Or InitialiseAnInt(int& result)
{
   result = rand()%256;
}
int myResult;
InitialiseAnInt(myResult);

The disadvantage to using new is that the memory must be deleted sometime.  In this case it'll be deleted by the caller, and you may forget to do this.  This leads to memory leaks and eats system resources.

As I say, without knowing what's inside APIFunctionThatReturnsPointerToADatabase() we can't give a precise answer, but hte problem is probably similar to above and all the above solutions will work for you
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
allymCommented:
Oh,  and you said "aren't I calling by reference?".

Nope, because that refers to parameters, and your fn doesn't take any.  

Pass by value means copy the parameters (the fn gets its own copy and can't damage the originals)

void fn(int eeek)
{
  eek = 20; // han no effect to caller
}
int blah = 200;
fn(blah);
cout << blah << endl; // blah = 200

Pass by reference menas that the fn gets the original values and can modify them.  

void fn(int eeek)
{
  eek = 20; // affects callers value
}
int blah = 200;
fn(blah);
cout << blah << endl; // blah = 20

But these apply to paramters, and yoiur fn doesn't have any, only a return value.  

PS: the API takes one of your own structs, eh?
0
graham_kCommented:
nope, you are passing by value that which p points to. Try

MyFunction(MyStruct &p)
 
(I think, I'm over 40 hours without sleep now <g>)
0
boomerang061797Author Commented:
You are right and I found out just before I read your answer. :-) The problem was that I was setting my pointer to point to a local variale which contained the address from the API function. It was hard to see and I was getting put off by the fact that the API function returns a pointer to a variable type instead of a copy of the variable.

MyFunction(MyStruct &ms)
{
  APIType type = typevalue;

  MyStruct *temp_struct;
      
  temp_struct = type.GetAPIMyStructType();

  ms = *temp_struct;
}

MyStruct ms2;

MyFunction(&ms2);

I got around the problem by defining a temp_struct variable and returning its value to a functionparameter reference. Any Better solutions?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.