Thread: Automatically make new elements of an array

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    37

    Automatically make new elements of an array

    I'm working on a huge (by my standards) address book program, and it consists of the array of objects of a class Entry. Each element is one entry in the book.
    Please help me by telling-
    1.How to automatically add an element to the array of objects.
    2.How to automatically change the element of the array, first checking that all the preceeding elements (or maybe a unique value of each entry) are not null.
    -regards,
    ultrabot90

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you want to have a "dynamic array".

    You could use a std::vector.

    If you want to "do it yourself" [e.g. because this is a school project, or you want to understand the basics of how this sort of thing works], then the principle is:
    1. You have a pointer to your items.
    2. You keep a "number of items" in the array.
    3. You keep size of the array. If number of items is bigger than size, then you need to allocate a new array [increase size by at least one], and copy the content of the old array into the new one.

    Another option is to use a linked list (or a binary tree, or similar).

    Or a "hybrid", where you use a linked list [or similar] to keep "chunks" with some number of items in each chunk, and when you need more space, you create a new chunk and link it in.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Hey, yeah, I googled std::vector and found a guide (http://www.codeguru.com/cpp/cpp/cpp_...cle.php/c4071/) and linked lists are what they're using in class, I think (linked lists google find - http://richardbowles.tripod.com/cpp/...t/linklist.htm).
    Indeed, it is a school project, (which others will be neatly copying) which I wanted to do myself.
    Thanks, sir!
    Last edited by ultrabot90; 10-31-2007 at 07:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum all odd array elements?
    By Marth_01 in forum C Programming
    Replies: 9
    Last Post: 11-04-2008, 10:47 PM
  2. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 07:53 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Array elements values not being assigned
    By Sardien in forum C Programming
    Replies: 4
    Last Post: 02-11-2002, 01:45 PM