It seems wrong to me

Discussion in 'C++' started by Stub, Nov 9, 2003.

  1. Stub

    Stub Guest

    I read the code in a book and it seems not quite right to me. The possible
    mistake seems in the dummy::getData(). It returns a local pointer, void*
    data, back to its caller. Am I right about this?

    class dummy
    {
    public:
    void* getData();
    void *first;
    };


    void* dummy::getData(){
    void *data;
    data = first;
    return data;
    }
    Stub, Nov 9, 2003
    #1
    1. Advertising

  2. Stub

    John Carson Guest

    "Stub" <> wrote in message
    news:Xlgrb.220358$
    > I read the code in a book and it seems not quite right to me. The
    > possible mistake seems in the dummy::getData(). It returns a local
    > pointer, void* data, back to its caller. Am I right about this?
    >
    > class dummy
    > {
    > public:
    > void* getData();
    > void *first;
    > };
    >
    >
    > void* dummy::getData(){
    > void *data;
    > data = first;
    > return data;
    > }


    There is no error. It would be simpler for getData to just return first, but
    since data has the same value as first, returning data has the same effect.

    Whether or not the pointer is a local variable is irrelevant, just as it is
    irrelevant when a function returns an int. What matters is whether the
    memory pointed to is "local memory" or "global memory".


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, Nov 9, 2003
    #2
    1. Advertising

  3. Stub

    Dirk Feytons Guest

    Stub wrote:

    > I read the code in a book and it seems not quite right to me. The possible
    > mistake seems in the dummy::getData(). It returns a local pointer, void*
    > data, back to its caller. Am I right about this?


    No.

    > class dummy
    > {
    > public:
    > void* getData();
    > void *first;
    > };
    >
    >
    > void* dummy::getData(){
    > void *data;


    This declares the variable named 'data' of type 'void*'. It's a variable
    that can hold a void pointer. At this point, the pointer points to
    god-knows-what. It is said to be uninitialized.

    > data = first;


    Here the pointer gets a value. More specific, the pointer now points to
    the same thing as 'first' points to.

    > return data;


    So what is actually returned, is 'first'.
    In fact, the implementation of getData() can be simplified to a simple
    'return first'.

    > }



    --
    Dirk

    (PGP keyID: 0x448BC5DD - http://www.gnupg.org - http://www.pgp.com)
    Dirk Feytons, Nov 9, 2003
    #3
  4. Stub wrote:
    > I read the code in a book and it seems not quite right to me. The possible
    > mistake seems in the dummy::getData(). It returns a local pointer, void*
    > data, back to its caller. Am I right about this?


    >
    > class dummy
    > {
    > public:
    > void* getData();
    > void *first;
    > };
    >
    >
    > void* dummy::getData(){
    > void *data;
    > data = first;
    > return data;
    > }
    >


    #include <stdcaution> // I'm certainly not an expert in C++

    You're right about the fact that it returns a local variable, but this
    is not a problem by itself (well, this may be a problem for the caller
    if dummy::first is not initialised before).

    Returning a local variable is ok, the caller gets a copy of it. What
    would be wrong would be to return *the address of* a local variable.

    What seems more problematic to me is that
    1/ the local variable 'data' in getData() is useless. This code is the
    same as :

    void* dummy::getData(){
    void *data = first; // useless
    return first;
    }

    which in turn would be better written :
    void* dummy::getData(){
    return first;
    }

    2/ since it's a pointer that is returned, and not a copy of the value
    pointed, any change made to the value pointed will affect dummy::first

    3/ this function by itself is actually of useless, since dummy::first is
    public...

    4/ ... - which is a Bad Thing(tm).

    5/ void pointers are probably the worst way to write generic code in C++
    anyway.

    Well... I don't know which book this come from, but I suspect the paper
    it's made of would be more useful elsewhere...

    (gurus please correct me if I said some stupidities)

    Bruno
    Bruno Desthuilliers, Nov 9, 2003
    #4
  5. Stub

    Rolf Magnus Guest

    Stub wrote:

    > I read the code in a book and it seems not quite right to me. The
    > possible mistake seems in the dummy::getData(). It returns a local
    > pointer, void* data, back to its caller. Am I right about this?


    You're right in that it returns a local pointer, but that's not a
    problem. It seems you're confusing it with a pointer to a local
    variable, which would be what you call "not quite right". In your
    example, you return the pointer "by value", which means that the
    pointer itself is copied and the caller of the function receives a copy
    that points to the same address as 'data'.
    The following example would be wrong:

    class dummy
    {
    public:
    int* getData();
    int x;
    };


    int* dummy::getData(){
    int data;
    data = x;
    return &data;
    }

    Now you return a pointer that contains the address of a local variable.
    Since that variable ceases to exist before the caller can access it,
    the returned pointer is invalid and must not be dereferenced.
    Rolf Magnus, Nov 9, 2003
    #5
    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. Edward
    Replies:
    0
    Views:
    817
    Edward
    Dec 4, 2003
  2. Edwin Knoppert
    Replies:
    0
    Views:
    399
    Edwin Knoppert
    Jan 19, 2006
  3. windandwaves

    validation seems wrong

    windandwaves, Aug 28, 2005, in forum: HTML
    Replies:
    36
    Views:
    984
    Neredbojias
    Aug 30, 2005
  4. Michael Mair

    Typecast long double->double seems to go wrong

    Michael Mair, May 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    642
    Michael Mair
    May 24, 2004
  5. RoSsIaCrIiLoIA

    undefined behaviour seems wrong and a big error

    RoSsIaCrIiLoIA, Feb 12, 2005, in forum: C Programming
    Replies:
    12
    Views:
    504
    Lawrence Kirby
    Feb 16, 2005
Loading...

Share This Page