pointer reference problem

M

Marcelo

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;
}
 
T

Thomas Tutone

Marcelo said:
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-lite/how-to-post.html#faq-5.8

Best regards,

Tom
 
M

Mike Wahler

Marcelo said:
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
 
H

Heinz Ozwirk

Marcelo said:
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
 
M

Marcelo

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());
}
 
T

Thomas Tutone

Marcelo said:
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
 
K

krishna.akella

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
 
E

Earl Purple

As a general rule, try to avoid using new where you don't need to.
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*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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top