Friday, July 4th 2008, 9:02pm UTC+1

You are not logged in.

  • Login
  • Register

1

Wednesday, April 23rd 2008, 12:39pm

pointer

hi,

the function f(Mystruct* p) { p = new Mystruct }

is called f(ptr1) with ptr1 declared as Mystruct* ptr1.

but ptr1 is 0 whereas p has correct value.

what's wrong
thanks in advance
  • Go to the top of the page

stinos

Intermediate

2

Wednesday, April 23rd 2008, 12:56pm

passing a ointer results in a copy being pushed on the stack.
you don't want the copy, so pass the argument by reference:

Source code

1
2
3
4
5
template< class T >
void my_new( T*& p ){ p = new T(); }

template< class T >
void my_delete( T*& p ){ delete p; p = 0; }
madinsjamania
  • Go to the top of the page

3

Wednesday, April 23rd 2008, 1:46pm

thanx
  • Go to the top of the page

Rate this thread