Array of pointers for dynamically allocating class instances

This is a discussion on Array of pointers for dynamically allocating class instances within the C++ Programming forums, part of the General Programming Boards category; I'm using an array of pointers to dynamically allocate class instances of a class and it's derived classes. I'm doing ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    17

    Array of pointers for dynamically allocating class instances

    I'm using an array of pointers to dynamically allocate class instances of a class and it's derived classes. I'm doing great so far, except when I have to copy an object. I'm getting bugs in my program and I think I know why. I just don't know how to solve the problem.

    I declare the array of pointers like this:

    class CObject {
    ...
    ...
    }

    CObject *templates[MAXOBJS];
    CObject *pointers[MAXOBJS];

    Here's where the problem lies. I want to copy a *templates object to a *pointers object. If I use:

    pointers[50] = templates[50];

    This just copies the pointer, and not the data at the pointer's address, correct? So if I were to:

    pointers[50].String = "Hello";

    Now I have just modified templates[50].String as well? How do I actually copy the data vs. the address?

    (Yes I'm aware both source and destination pointers must point to an object created with the new keyword)
    Last edited by sh0x; 09-10-2001 at 11:30 PM.
    -sh0x

  2. #2
    Unregistered
    Guest
    >Now I have just modified templates[50].String as well? How do I
    >actually copy the data vs. the address?

    >(Yes I'm aware both source and destination pointers must point >to an object created with the new keyword)

    If you actually allocate memory for CObject* template and *pointer using new, you shouldn't have that problem.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    17

    Please clarify

    So what you're saying is, if after declaring the array of pointers and creating two instances of the class using the new keyword, one in *templates[50], and one in *pointers[50], if I use the statement:

    pointers[50] = templates[50]

    The data will be copied, and not just the pointer? And if so, then how would I copy just the pointer? Pointers have been a real stumbling block for me. Thanks for your input.

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you use the statement -

    pointers[50] = templates[50]

    then the 51st element of 'templates' will be copied into the 51st element of 'pointers'. As you only have arrays with fifty elements this isn't what you want. You'll have to loop through the 'templates' (0-49) and copy them into the 'pointers'(0-49) - This assumes that you have already allocated memory to store the instances pointed to by 'pointers'.

    If you're class deals with dynamic memory internally you'll have to overload the = operator and then copy the internally allocated memory into newly allocated memory in 'pointers' (deleting any previously allocated memory), otherwise your internal pointers will point to the same location.

    If you want a pointer to any of the templates you can take the address of its element - &template[25]. But you have to be careful not to assign this address to a pointer thats pointing elsewhere on the heap otherwise you'll get a memory leak.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    17
    Originally posted by zen
    If you use the statement -

    pointers[50] = templates[50]

    then the 51st element of 'templates' will be copied into the 51st element of 'pointers'. As you only have arrays with fifty elements this isn't what you want. You'll have to loop through the 'templates' (0-49) and copy them into the 'pointers'(0-49) - This assumes that you have already allocated memory to store the instances pointed to by 'pointers'.

    If you're class deals with dynamic memory internally you'll have to overload the = operator and then copy the internally allocated memory into newly allocated memory in 'pointers' (deleting any previously allocated memory), otherwise your internal pointers will point to the same location.

    If you want a pointer to any of the templates you can take the address of its element - &template[25]. But you have to be careful not to assign this address to a pointer thats pointing elsewhere on the heap otherwise you'll get a memory leak.
    Actually my array size is 100 (0-99), and I only wanted to copy arbitary class instances one at a time, but I found your answer regarding overloading the = operator and avoiding memory leaks very enlightening. Thank you very much Zen.
    -sh0x

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. static array of function pointers within class
    By Yarbles in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2005, 02:10 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21