c++ array troubles

Started by
10 comments, last by Will F 18 years, 5 months ago
I'm trying to create an array of floats where the user decides how many elements there are in the array. My code for this is: cin >> len1; float fArrayOne[len1]; but it doesn't work. I get three errors per array I try and setup in this way: error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0 error C2133: 'fArrayOne' : unknown size So apparently you can't setup arrays this way... Is there any workaround or way to do this? Or do I need to create an array with a constant size and just fill it up to where the user says?
kooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
Advertisement
Arrays are evil. Use std::vector. It allows dynamic sizes of stuff.
What you need is dynamic memory allocation. Anytime you don't know ahead of time how much memory you are going to need, you'll need to allocate memory dynamically.

So what you need to do is this:

cin >> num

float *array= new float[num]; //allocates memory for num float locations
I tried the dynamic allocation, and it reduced the number of errors per array to 1. For each array I get:

error C2440: 'initializing' : cannot convert from 'float *' to 'float []'

and my code is:

cin >> len1;
float fArrayOne[] = new float[len1];
kooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
I just changed it above to a float *, look at my previous post.
Thanks it works perfectly now!
kooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
and don't forget to free the allocated memory. In C++ you use the delete instruction, but in your case, use the array version, so:

delete [] array;
array = NULL;

it's a good practise to always do it because when you reach some complex projects you may find some memory problems and segmentation faults.. Doing so prevents expending debuggin time.

Cheers
what is the application that you want to use this array. Because maybe if you just want to store data, and then keep adding data without any limit you should consider using linked lists. But if you want to actually want a specialized array class that can handle resizing then I think there is some libraries that have such solutions.
But definedly in C++ you cannot put a variable on the array size. At least that i know of.
Marco Tapia
Quote:Original post by Anonymous Poster
Arrays are evil. Use std::vector. It allows dynamic sizes of stuff.


Seconded. If you are coding in C++ use std::vector unless you have a good reason not to. Here's a tutorial that might help you get started.
Quote:Original post by Anonymous Poster
Arrays are evil. Use std::vector. It allows dynamic sizes of stuff.


Thirded.

So something like:
std::vector<float> fArrayOne;int len1;cin >> len1;for(int i = 0; i < len1; i++){    fArrayOne.push_back(0.0f);}


Now you don't have to worry about deleting the array, because the vector manages the memory for you.

-----------------------------Play Stompy's Revenge! Now!

This topic is closed to new replies.

Advertisement