Dynamically Resizing Arrays
i am having a problem with resizing arrays. what i am trying to do is read in a couple of values from a file, then find how many there are (which works fine), and then store them into an array. the only problem is i keep getting an error saying "expected constant expression". does anybody know of a way that will resize an array given a non-constant value?
Solution 1:
Solution 2:
Read(&vec_count)
Seek(back_to_beginning_or_whatever)
...
first two lines in solution 2 are in pseudocode.
Hope this helps,
Crispy
std::vector
or std::list
Solution 2:
Read(&vec_count)
Seek(back_to_beginning_or_whatever)
TVector3f* MyArray = new TVector3f[vec_count];
...
delete[] MyArray;
first two lines in solution 2 are in pseudocode.
Hope this helps,
Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
January 06, 2003 08:47 AM
If you are using C instead of C++, you can use malloc,
Type *variable = (Type *)malloc(size);
Type *variable = (Type *)malloc(size);
You cannot resize arrays once they have been allocated. You have to delete the current array, and create a new one of the size you require, and copy the data over. Well, you''d create a new one, copy data over, then delete the old one...
Or you could use std::vector.
Death of one is a tragedy, death of a million is just a statistic.
Or you could use std::vector.
Death of one is a tragedy, death of a million is just a statistic.

If at first you don't succeed, redefine success.
quote:
Original post by Anonymous Poster
If you are using C instead of C++, you can use malloc,
Type *variable = (Type *)malloc(size);
If I''m not mistaken, in C, you must use
malloc()
, and in C++, you should use new, delete/delete[]
.Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
I don''t know about you guys, but I prefer using malloc for large dynamic arrays, and new/delete for fixed length arrays.
char *fileData;
void readFile(char *filename)
{
char fileLength[2];
char tempData;
FILE *srcFile = fopen(filename,"rb");
if(!srcFile) return;
fread(&fileLength,sizeof(fileLength),1,srcFile);
fileData = (char*)malloc((fileLength[0] + fileLength[1])*sizeof(char));
if(!fileData){
fclose(srcFile);
return;
}
while(*fileData){
fread(&tempData,sizeof(char),1,srcFile);
*(fileData++) = tempData;
}
fclose(srcFile);
// Do what you need to with the fileData here.
free(fileData);
}
char *fileData;
void readFile(char *filename)
{
char fileLength[2];
char tempData;
FILE *srcFile = fopen(filename,"rb");
if(!srcFile) return;
fread(&fileLength,sizeof(fileLength),1,srcFile);
fileData = (char*)malloc((fileLength[0] + fileLength[1])*sizeof(char));
if(!fileData){
fclose(srcFile);
return;
}
while(*fileData){
fread(&tempData,sizeof(char),1,srcFile);
*(fileData++) = tempData;
}
fclose(srcFile);
// Do what you need to with the fileData here.
free(fileData);
}
Oh and to the guy that said that you couldn''t resize arrays, haven''t you heard of realloc()?
Yes I have, but I was assuming he''s using C++ ( because of the talk on vectors ), in which case it is advised to use new/delete. Besides, realloc probably does the same thing as what I said anyway.
Death of one is a tragedy, death of a million is just a statistic.
Death of one is a tragedy, death of a million is just a statistic.

If at first you don't succeed, redefine success.
..But there is nothing wrong with using malloc/free in c++ at all. The only reason why it is recommended that people use new/delete is because those people have made too many stupid mistakes with handling malloc.
In clear code that handles all possible memory allocation errors, malloc is fine. Just be prepared for system crashes if you don''t take care with it.
In clear code that handles all possible memory allocation errors, malloc is fine. Just be prepared for system crashes if you don''t take care with it.
Huh? new doesn''t do anything extra than malloc. Infact, malloc is called within new. new is just an easier way of doing it, because you don''t have to do any casting or anything, and you can use array notation.
Death of one is a tragedy, death of a million is just a statistic.
Death of one is a tragedy, death of a million is just a statistic.

If at first you don't succeed, redefine success.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement