I'll keep this short and sweet. I'll explain what I am trying to do and (hopefully) someone here has some advice. Sound like a deal?
Ok, I want to start off with a pointer. For the sake of simplicity, let's just say:
int *array = NULL;
int count = 0;
Now, when my program starts, I don't know how many ints I needs, but I will only need more, not less. Say I am initializing array in a function like in the pointless function that follows: (just assume array and count are global)
void Happy(int i)
{
count ++;
array = new int[count];
array[count - 1] = i;
}
Now, in my code I want something like this.
Happy(1);
Happy(3);
Happy(5);
and so forth;
Will this work? Is this good coding? I believe that I have tried to use new twice in a row and it works, but does this annoy the compiler or cause memory leaks or something? Am I better off just counting the number of times I plan to call Happy() and just allocate the memory before I start assigning values? Keep in mind that I will be doing this only once at the beginning of the program. Then only accessing the data there after. Based on the nature of what I am trying to do, if I didn't have to allocate at the beginning, but rather as I go, it would be more easier. Also, I assume someone is going to suggest I use link lists or some other technique for this, but I am looking for a strictly non link list answer.
Thanks for answering so quickly and accurately
,
Briar LoDeran
Oh, I just thought of something. In the Happy() function, would the following code be more helpful?
void Happy(int i)
{
if(array)
{
delete[] array;
array = NULL;
}
count ++;
array = new int[count];
array[count - 1] = i;
}
I am pretty sure that delele just deletes the pointer, not the actual data, so if I just allocate again I should get back what I had before plus one new int, right? Maybe?
Edited by - BriarLoDeran on 6/5/00 9:32:08 PM