Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array with variable as index 2

Status
Not open for further replies.

edmund1978

Programmer
Jan 25, 2001
58
0
0
GB
I need to make an array with the size being determined by another variable.
eg.
int num;
...
num = 5;
int array[num];
...

Using VC++ 6

Compiler complains that this isn't constant, I understand why but don't know how to get around it.
 
edmund,

This is the same question you asked in the C forum. In C++ it is much simpler since we have the 'new' operator.

int numInts = 5;
int* myInts = new int[numInts];

// use the integer array

delete myInts; // clean up memory allocation

It is important to note that if you allocate an array of objects using this method you need to call the array delete operator to insure that each objects destructor is called, i.e.:

int numObjs = 5;
foo* myFoos = new foo[numObjs];

// use foo objects in the array
myFoos[0]->setValue(10);

delete [] myFoos; // call each foo destructor

Hope this helps
-pete
 
I've tried this before, but I've had some problems with it. Basically, i wanted to make an array that was only as big as the elements inside. ex (array[1] = "a";) and (array[2] = "ab";), but I never quite got it to work right...

Any thoughts?

 
thiefmaster,

Do you mean like this?


char * array[] = {"hello", "world"};


-pete
 
not quite.

Let me see if i can clarify:

If there is only 1 element in the array, then the array would only be of size 1.
If there is 2 elements in the array, then the array would be of size 2.
etc.

So basically, it would be an array that grows with the data put into it.

I've tried to have something like

int num = 1;
element * myelements = new element[num];

// do some stuff.

// need to add an element...
num++;
element * myelements2 = new element[num];

int i = num;
while(--i > 0)
{
myelements2 = myelements;
}

delete myelements;

There was some other code thrown in there too, but it shouldn't affect the basic function of the program. Also, i used a bool value to determine if we needed to process from myelements -> myelements2 or from myelements2 -> myelements.
It worked fine up until it had to make a couple of allocations. eg. myelements -> myelements2 -> myelements

Hopefully this clarifys what I wanted to do... Hopefully you can actually do this.
 
thiefmaster,

Hopefully this is a class assignment, if not you should not be dealing with raw arrays that need to grow dynamically in the year 2001 in the C++ language. So my response is based on the assumption that this is a class assignment.

Here a section of your code:

int j = num;
while(--j > 0)
{
myelements2 = myelements;
}

delete myelements;


First myelements2 = myelements; assigns the first pointer to be the value of the second pointer over and over in your loop. Perhaps what you wanted was:

myelements2[j] = myelements[j];

Also you have a potential memory leak:

delete myelements;


Should use the array delete operator:
delete [] myelements

Hope this helps
-pete
 
i had written myelements2 = myelements, but forgot to enclose it by the code TGML... :)

I needed to have an array that was only as large as it needed to be because it is a class... If i remember from when I did a sizeof, each one is around 100 bytes (i think)... therefore, if it has 100 elements, it is 10000 bytes, which is a lot of memory...
 
I have used code similar to the following. This array is passed as a parameter to another function (which is only run withing the scope of the original function). The code crashes, but works fine if I remove the delete myInts line. As I will be working with fairly large arrays, and the function will be called regular, I think this would cause a memory problem. Or is it somehow automatically being deleted? Error message is debug error, Damage after normal block ........ at 0x........

int numInts = 5;
int* myInts = new int[numInts];

// use the integer array

delete myInts; // clean up memory allocation
 
Remember, if you're deleting an array, you ahve to include the [] in the delete:

delete [] myInts

This is very important, as it's considered a memory leak, which is very bad, espically with Windows.

MWB.
As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top