varying size array

This is a discussion on varying size array within the C++ Programming forums, part of the General Programming Boards category; hi, I have a program that I'm looping quite a few times, every time it loops it produces some relevant ...

  1. #1
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    varying size array

    hi,

    I have a program that I'm looping quite a few times, every time it loops it produces some relevant data that I want to keep track off, however that number of points (data) that are produced varys with each iteration of the loop, my question is :

    is there a way to declare a varying size array that will only be as big as it needs to be. I'm running 256 experiments, and during each iteration(expriment), there may be 5 points produced or none, or 10...but I don't want to just declare an encompassing arrary of size [20] or so, and not even use most of it for each iteration (experiement)...

    I'm using the complier: codewarrior... and tried having a counter to keep track of the number of points of data produced then inserting them into an array, where the size is what was produced by the counter...so that no space in the array is wasted... am I using a wrong approach?? is this not possible?? thanks much for any help...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Much better if you used a linked list

    Or if you've got the C++ STL with your compiler, consider an expanding structure like <vector>

  3. #3
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43
    thanks...I've not really used vectors much...could you give perhaps an exmaple of how vectors can be declared so that they can be of varying size... thanks much...

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    vector<int> myarray;

    vector<float> myarray;

    vector<anydatatype> myarray;

    and there is a resize function on it so that you can check the size and resize when necessary. have fun

  5. #5
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    you could use dynamic memory as well, but then you have to worry about memory leaks if the program crashes and doesn't get a chance to run the delete command, or if you forget to put in a delete command.

    PHP Code:
    int *pDynamic;
    pDynamic = new int[6]
    ...
    delete [] pDynamic

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. size of array
    By goran00 in forum C Programming
    Replies: 38
    Last Post: 04-02-2008, 10:57 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 04:06 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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