[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Simple pointer/function question

Posted on 1999-11-16
4
Medium Priority
?
178 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:boomerang061797
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
allym earned 150 total points
ID: 2213313
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
 
LVL 1

Expert Comment

by:allym
ID: 2213322
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
 
LVL 6

Expert Comment

by:graham_k
ID: 2213323
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
 

Author Comment

by:boomerang061797
ID: 2213391
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

640 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