Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointer reference problem

Reply
Thread Tools

pointer reference problem

 
 
Marcelo
Guest
Posts: n/a
 
      01-18-2006
Hi everybody,

I don't understand why I am having a problem in this code.
The problem is that my pointer *phist in main method, it is declared.
Then I send the pointer to my method, and this method creates a new object (a
Matrix) for it.

I suppose that after the new operator, my pointer is pointing to an object, so
when the method has finished, the very first pointer is still poitint to the
created method; however this is not working, I have a message as if the pointer
hasn't been initialized...

Do you have an idea?
thanks a lot for your help,

Marcelo


------
int main(){
Matrix<uchar> *mgrey;
mgrey = new Matrix<uchar>(5,5);

for(unsigned i=0; i < (*mgrey).getH() * (*mgrey).getW(); i++){
*((*mgrey).val + i) = i*10;
}

Matrix<float> *phist;
Matrix<uchar> *mtest;

//here i send my pointer
mtest = method(phist, mgrey);

//my pointer has not been initialized...
printf("%d ",(*phist).size());
}

Matrix<float>* method(Matrix<float> *ptr, Matrix<uchar> *mgrey){
..blah ...blah

ptr = new Matrix<float>(3,3);

blah..blah
return 0;
}
 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      01-18-2006
Marcelo wrote:
> Hi everybody,
>
> I don't understand why I am having a problem in this code.
> The problem is that my pointer *phist in main method, it is declared.
> Then I send the pointer to my method, and this method creates a new object (a
> Matrix) for it.
>
> I suppose that after the new operator, my pointer is pointing to an object, so
> when the method has finished, the very first pointer is still poitint to the
> created method; however this is not working, I have a message as if the pointer
> hasn't been initialized...


This may help:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Best regards,

Tom

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-18-2006

"Marcelo" <(E-Mail Removed)> wrote in message
news:43cec91b$(E-Mail Removed)...
> Hi everybody,
>
> I don't understand why I am having a problem in this code.
> The problem is that my pointer *phist in main method, it is declared.
> Then I send the pointer to my method, and this method creates a new object
> (a Matrix) for it.
>
> I suppose that after the new operator, my pointer is pointing to an
> object, so when the method has finished, the very first pointer is still
> poitint to the created method; however this is not working, I have a
> message as if the pointer hasn't been initialized...
>
> Do you have an idea?
> thanks a lot for your help,
>
> Marcelo
>
>
> ------
> int main(){
> Matrix<uchar> *mgrey;
> mgrey = new Matrix<uchar>(5,5);
>
> for(unsigned i=0; i < (*mgrey).getH() * (*mgrey).getW(); i++){
> *((*mgrey).val + i) = i*10;
> }
>
> Matrix<float> *phist;
> Matrix<uchar> *mtest;
>
> //here i send my pointer
> mtest = method(phist, mgrey);
>
> //my pointer has not been initialized...
> printf("%d ",(*phist).size()); }
>
> Matrix<float>* method(Matrix<float> *ptr, Matrix<uchar> *mgrey){


'ptr' is a *copy* of 'phist', it's not 'phist' itself.
This function cannot modify 'phist'. When this function
returns, the object 'ptr' is destroyed.

> ..blah ...blah
>
> ptr = new Matrix<float>(3,3);
>
> blah..blah
> return 0;
> }


Arguments to functions are passed by value (this does
not change even if the argument is a pointer). The means
that the called function receives a *copy* of the argument,
it has no access to the caller's object. If you
want a function to change the caller's object, you need
to pass a pointer or (better) a reference to it.

void f(int i) // the object 'i' is an independent
// object, whose scope and lifetime
// are limited to this function's body
{
++i;
}

void g()
{
int x = 42;
cout << x; // prints 42
f(i); // this will not change 'x'
cout << x; // prints 42
}

void h(int& i) // receives argument by reference
// 'i' is not an object, but an
// alternate name for the callers'
// object.
{
++i; // increments caller's object's value
}

void k()
{
int y = 99;
cout << '\y'; // prints 99
h(y); // h() modifies 'y' (via reference)
cout << '\y'; // prints 100
}

void m(int *i) // parameter is pointer{
++(*i); // dereference pointer 'i' and modify
// what it points to
}

void n()
{
int z(25);
cout << z; // prints 25
m(&z); // pass address of 'z' (a pointer) to 'm()'
cout << z; // prints 26
}

int i; // an integer
int *i; // a pointer to an integer
int **i // a pointer to a pointer to an integer

Having said all that, I recommend you try to avoid pointers
and use references when a called function needs direct
access to its caller's arguments.

-Mike


 
Reply With Quote
 
Heinz Ozwirk
Guest
Posts: n/a
 
      01-18-2006
"Marcelo" <(E-Mail Removed)> schrieb im Newsbeitrag
news:43cec91b$(E-Mail Removed)...
> Hi everybody,
>
> I don't understand why I am having a problem in this code.
> The problem is that my pointer *phist in main method, it is declared.
> Then I send the pointer to my method, and this method creates a new object
> (a Matrix) for it.


You don't "send" the pointer, you pass its (undefined) value to your
function. On entry to the function a local variable is initialized with this
(undefined) pointer value. Then you create a new object and assign its
address to the local variable. This value will be lost when the function
returns (and you'll have a memory leak). The variable which's value you
passed into the function will remain unchanged (and still undefined). Its
the same as

#include <iostream>
void foo(int bar)
{
bar = 42;
}
int main()
{
int baz;
foo(baz);
std::cout << baz << std::endl;
}

You can solve the problem if you change the first parameter of your function
to Matrix<float>** or Matrix<float>*&. Any decent book about C++ will
describe that in more detail.

HTH
Heinz


 
Reply With Quote
 
Marcelo
Guest
Posts: n/a
 
      01-18-2006
Thanks, I will try to be more specific.

Matrix : personal template class for a matrix

An external method initializes the pointer, for example:

method(matrix<float> *phist){
phist = new Matrix<float>(3,3);
}

but, when the method has finished, the pointer doesn't point to the created
object. I don't understand why. Can you help me?

thanks a lot,

Marcelo


----------

int main(){
...
Matrix<float> *phist;

//here i send my pointer
mtest = method(phist);

//my pointer has not been initialized...
printf("%d ",(*phist).size());
}
 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      01-18-2006
Marcelo wrote:
> Thanks, I will try to be more specific.
>
> Matrix : personal template class for a matrix
>
> An external method initializes the pointer, for example:
>
> method(matrix<float> *phist){
> phist = new Matrix<float>(3,3);
> }
>
> but, when the method has finished, the pointer doesn't point to the created
> object. I don't understand why. Can you help me?
>
> thanks a lot,
>
> Marcelo
>
>
> ----------
>
> int main(){
> ...
> Matrix<float> *phist;
>
> //here i send my pointer
> mtest = method(phist);
>
> //my pointer has not been initialized...
> printf("%d ",(*phist).size());
> }


Two other people (both apparently brighter than me) have already
answered your question. It saddens me, though, that you didn't
understand my advice to you - you need to provide use with a short,
compilable example that illustrates your problem. Even after being
asked, you didn't do that.

Anyway, the other folks have already answered your question. When you
pass a non-reference argument to a function, the function creates a
local copy, which is lost upon return. Either have the function return
the value you want to use, or use references.

And if you post again, please post a short, compilable example that
illustrates your problem. I can't cut and paste what you posted to try
to compile it myself, which makes it much harder to help you.
Fortunately for you, the other respondents figured it out anyway.

Best regards,

Tom

 
Reply With Quote
 
krishna.akella@gmail.com
Guest
Posts: n/a
 
      01-19-2006
Here is what is happening. Lets say you have two functions f(T * v) and
g( ) that calls the function f. Where T is some type.
void g()
{
T * v;
f( v );
// use v here....
delete v; // possible crash/prog mis-behavior here??
}

void f(T * p)
{
// do some thing here
p = new T; // mem leak
return;
}

in g() v is pointing to some garbage location. When you pass it to f( )
from g() a local variable p within the scope of f() is created that
also points initially to the same GARBAGE LOCATION.
------------------
v | 0xa542fca | -------------------> GARBAGE VALUE
------------------

------------------
p | 0xa882ffa | -------------------> GARBAGE VALUE
------------------

When you do new from within f() now this is the situation:


------------------
v | 0xa542fca | -------------------> GARBAGE VALUE
------------------

------------------
p | 0xa882ffa | -------------------> MEM ALLOCATED LOCATION for Type
T Constructed
------------------

once you are out of f(), p (the local variable of f() ) is destroyed.
But v inside of g() is still pointing to the same GARBAGE LOCATION !!!

So use a reference to a pointer OR a pointer to a pointer, as others
have pointed out.

- Krishna

Marcelo wrote:
> Hi everybody,
>
> I don't understand why I am having a problem in this code.
> The problem is that my pointer *phist in main method, it is declared.
> Then I send the pointer to my method, and this method creates a new object (a
> Matrix) for it.
>
> I suppose that after the new operator, my pointer is pointing to an object, so
> when the method has finished, the very first pointer is still poitint to the
> created method; however this is not working, I have a message as if the pointer
> hasn't been initialized...
>
> Do you have an idea?
> thanks a lot for your help,
>
> Marcelo
>
>
> ------
> int main(){
> Matrix<uchar> *mgrey;
> mgrey = new Matrix<uchar>(5,5);
>
> for(unsigned i=0; i < (*mgrey).getH() * (*mgrey).getW(); i++){
> *((*mgrey).val + i) = i*10;
> }
>
> Matrix<float> *phist;
> Matrix<uchar> *mtest;
>
> //here i send my pointer
> mtest = method(phist, mgrey);
>
> //my pointer has not been initialized...
> printf("%d ",(*phist).size());
> }
>
> Matrix<float>* method(Matrix<float> *ptr, Matrix<uchar> *mgrey){
> ..blah ...blah
>
> ptr = new Matrix<float>(3,3);
>
> blah..blah
> return 0;
> }


 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      01-19-2006
As a general rule, try to avoid using new where you don't need to.

Marcelo wrote:
> Hi everybody,
> ------
> int main(){
> Matrix<uchar> *mgrey;
> mgrey = new Matrix<uchar>(5,5);
>
> for(unsigned i=0; i < (*mgrey).getH() * (*mgrey).getW(); i++){
> *((*mgrey).val + i) = i*10;
> }
>
> Matrix<float> *phist;
> Matrix<uchar> *mtest;
>
> //here i send my pointer
> mtest = method(phist, mgrey);
>
> //my pointer has not been initialized...
> printf("%d ",(*phist).size());
> }
>

main could be implemented something like:

int main()
{
Matrix<uchar> mgrey( 5. 5 );
size_t height = mgrey.getH();
size_t width = mgrey.getW();
size_t dim = height * width;
for ( size_t i=0; i< dim, ++i )
{
mgrey.val[i] = i*10;
}

Matrix<float> phist;
Matrix<uchar> mtest = method( phist, mgrey );

cout << phist.size() << '\n';
}

and implement method accordingly. Note here that phist is a matrix
created with its default constructor, not a pointer to anywhere.

Not sure what you actually wanted it to be.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
reference and pointer-reference kasthurirangan.balaji@gmail.com C++ 4 12-24-2007 09:05 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments