Thread: varying size array

  1. #1
    Registered User
    Join Date
    Jun 2002

    varying size array


    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 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
    The edge of the known universe
    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
    Registered User
    Join Date
    Jun 2002
    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
    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
    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, 07:39 AM
  2. size of array
    By goran00 in forum C Programming
    Replies: 38
    Last Post: 04-02-2008, 09: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, 03:06 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM