×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

pointer

pointer

(OP)
hi,

void f(MyStruct* p)
{
   p = new MyStruct;
}

 is called like this : f(ptr1);

with ptr1 declared as MyStruct* ptr1;

I think passing the pointer as above is incorrect 'cause

p has a correct value but ptr1 is 0 after the function

call.

so what's wrong ?

thanks in advance

RE: pointer

The new MyStruct is local to f(), so the structure is lost when the function returns.  However, since the pointer isn't deleted inside the function, you'll have a memory leak.

If you want to use the new MyStruct, you'll need to write something like this:

CODE

MyStruct* f(MyStruct* p)
{
   p = new MyStruct;
   return p;
}

Lee

RE: pointer

you could also pass in a reference of the pointer:

[code]
void f(MyStruct &p)
{
   p = new MyStruct;
}
[\code]

either way works.

RE: pointer

When you pass a pointer, you're actually passing a copy of a pointer, unless you pass the pointer as a reference like this:

CODE

void f(MyStruct*& p)
{
   p = new MyStruct;
}
but some people find that syntax a bit confusing or they prefer a more standard C way of doing it, which is to pass a pointer to the pointer you want to modify:

CODE

void f(MyStruct** p)
{
   *p = new MyStruct;
}

RE: pointer

(OP)
thank you guys,

I tried it, it works !
 

RE: pointer

cpjust, so then

CODE

void f(MyStruct &p);

creates a new "version" of the pointer by reference?

and

CODE

void f(MyStruct *&p);

actually references the same memory of the original pointer without creating a new object pointer?
 

RE: pointer

Your first example is to a structure named p, and is NOT a pointer to the structure.  If you pass that function a pointer, you'll probably get an error when you compile unless you dereference the pointer when you call the function.

Lee

RE: pointer

troll,
is that directed to me?

RE: pointer

Yes, SDowd.  Your first example passes the reference to a MyStruct structure, where the second pases a reference to a MyStruct pointer.

Lee

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close