pointer reference problem

Discussion in 'C++' started by Marcelo, Jan 18, 2006.

  1. Marcelo

    Marcelo Guest

    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;
    }
     
    Marcelo, Jan 18, 2006
    #1
    1. Advertising

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

    Best regards,

    Tom
     
    Thomas Tutone, Jan 18, 2006
    #2
    1. Advertising

  3. Marcelo

    Mike Wahler Guest

    "Marcelo" <> wrote in message
    news:43cec91b$...
    > 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
     
    Mike Wahler, Jan 18, 2006
    #3
  4. Marcelo

    Heinz Ozwirk Guest

    "Marcelo" <> schrieb im Newsbeitrag
    news:43cec91b$...
    > 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
     
    Heinz Ozwirk, Jan 18, 2006
    #4
  5. Marcelo

    Marcelo Guest

    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());
    }
     
    Marcelo, Jan 18, 2006
    #5
  6. 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
     
    Thomas Tutone, Jan 18, 2006
    #6
  7. Marcelo

    Guest

    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;
    > }
     
    , Jan 19, 2006
    #7
  8. Marcelo

    Earl Purple Guest

    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*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.
     
    Earl Purple, Jan 19, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam pal
    Replies:
    3
    Views:
    584
    E. Robert Tisdale
    Jul 16, 2003
  2. Replies:
    10
    Views:
    731
    Chris Torek
    Feb 4, 2005
  3. Replies:
    4
    Views:
    407
    Andre Kostur
    Dec 24, 2007
  4. mathieu
    Replies:
    8
    Views:
    523
    Juha Nieminen
    Aug 31, 2008
  5. A
    Replies:
    7
    Views:
    649
Loading...

Share This Page