Pointer problem

Discussion in 'C++' started by Thomas Baier, Sep 9, 2003.

  1. Thomas Baier

    Thomas Baier Guest

    Hi there,

    I've got a class with an array of pointers to objects of the same class. I
    thought of doing it like that:
    class test {
    test **array_var;
    ...
    };
    void method::test(test object)
    {
    children = (Baum**) realloc(children, sizeof(Baum**));
    children[elements] = (Baum*) malloc(sizeof(Baum*));
    children[elements] = &obj;
    }

    test a;
    test b;
    a.method(b);

    I tried to access b like:
    a.children[0].var_of_b;
    but it did not work. So how can I manage it?


    Thanks for help

    Thomas
    Thomas Baier, Sep 9, 2003
    #1
    1. Advertising

  2. Well if you really want to...
    But look into the STL containers instead...

    #include <iostream>
    class X{
    public:
    X():number(10){};
    int number;
    };
    typedef X* XPtr;

    int main(int argc, char* argv[])
    {

    XPtr* Arr = new XPtr[10];

    Arr[0] = new X;

    std::cout << "this should say 10 : " << Arr[0]->number << std::endl;
    return 0;
    }




    "Thomas Baier" <> wrote in message
    news:3f5e3fde$0$2868$-online.net...
    > Hi there,
    >
    > I've got a class with an array of pointers to objects of the same class. I
    > thought of doing it like that:
    > class test {
    > test **array_var;
    > ...
    > };
    > void method::test(test object)
    > {
    > children = (Baum**) realloc(children, sizeof(Baum**));
    > children[elements] = (Baum*) malloc(sizeof(Baum*));
    > children[elements] = &obj;
    > }
    >
    > test a;
    > test b;
    > a.method(b);
    >
    > I tried to access b like:
    > a.children[0].var_of_b;
    > but it did not work. So how can I manage it?
    >
    >
    > Thanks for help
    >
    > Thomas
    >
    Jesper Madsen, Sep 9, 2003
    #2
    1. Advertising

  3. Thomas Baier wrote:

    > Hi there,
    >
    > I've got a class with an array of pointers to objects of the same class.


    This does not seem to be what you have at all, so either your
    description is off or your code is. I'm not entirely sure which.

    > I
    > thought of doing it like that:
    > class test {
    > test **array_var;


    This is, of course, not an array at all, but a pointer. Now, if I assume
    the description you gave is accurate, then I don't think you want
    'array_var' to be of type 'test **', but rather of type 'test *'.

    > ...
    > };
    > void method::test(test object)


    You haven't given any information about this 'method' class (or namespace?).

    > {
    > children = (Baum**) realloc(children, sizeof(Baum**));


    A few problems here:

    1) This is an unsafe way of using realloc. If the reallocation fails,
    you lose the original memory. The only safe way to use realloc is to
    store the resulting value in a new temporary pointer, then assign it to
    the original pointer if and only if the allocation succeeds.

    2) Please don't use realloc in C++. It's dangerous, error-prone, won't
    work correctly for non-POD types, and there's a better alternative:
    std::vector.

    Also, having no idea what 'children' or 'Baum' are, I can't properly
    diagnose problems with this code.

    > children[elements] = (Baum*) malloc(sizeof(Baum*));
    > children[elements] = &obj;


    One of the problems here should be obvious. On one line you store a
    pointer to some new memory. On the next you overwrite that pointer. You
    have leaked the memory you allocated.

    But again, I can't properly diagnose problems due to lack of information.

    > }
    >
    > test a;
    > test b;
    > a.method(b);


    More stuff I don't know about: the 'method' member of test.

    >
    > I tried to access b like:
    > a.children[0].var_of_b;


    And again: the 'children' member of test.

    > but it did not work. So how can I manage it?
    >


    I really don't know what you are doing, so I can't say. Honestly, I
    suspect the code you posted is far from the code you are actually
    attempting to use. It kind of looks like you've changed some variable
    names and transposed some identifiers.

    Post real code, a minimum complete program, and we can probably help.
    With what you've given us... there's basically nothing we can do.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Sep 9, 2003
    #3
    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. Replies:
    10
    Views:
    663
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    813
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,218
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    620
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    437
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page