Sign in to follow this  
kooooo

c++ array troubles

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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];

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:

Arrays are evil. Use std::vector. It allows dynamic sizes of stuff.


I don't want to take this off-topic, but I have to disagree. I would say use the array unless you have a good reason to use the std::vector. If your design needs the dynamic array then use it, otherwise steer clear of it.

Just for the heck of being able to add elements on the fly is not a suitable justification for using std::vector. Your design should be the only reason you use any data structure.


Share this post


Link to post
Share on other sites
Quote:
Original post by FReY
Just for the heck of being able to add elements on the fly is not a suitable justification for using std::vector. Your design should be the only reason you use any data structure.


The C++ FAQ lite does a good job of explaining why you should be using the standard container classes rather than arrays.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this