4

Possible Duplicate:
Variable length arrays in C++?

I am trying to form an array whose size is governed by another variable fed in by the user. I am a beginner in C++ coding and I am using the visual studio 2008 professional compiler. This is the code where the compiler shows the error:

double kplus2(double a, double ks, double kr)
{
    int n = (ks-1)*100000;
    double x[n];

The error is

Error 2 error C2057: expected constant expression
Error 3 error C2466: cannot allocate an array of constant size 0
Error 4 error C2133: 'x' : unknown size

4
  • 4
    Use std::vector<double> x(n); Commented Jan 2, 2013 at 16:21
  • first google result Commented Jan 2, 2013 at 16:24
  • What's depressing is the number of truly terrible answers to this question. Commented Jan 2, 2013 at 16:25
  • I suggested using new, which I realize is wrong. Read here: stackoverflow.com/questions/381621/… Commented Jan 2, 2013 at 16:27

3 Answers 3

15

I am trying to form an array whose size is governed by another variable fed in by the user.

C++ already has this functionality. It is called std::vector and lives in the header <vector>.

double kplus2(double a, double ks, double kr)
{
    int n = (ks-1)*100000;
    std::vector<double> x(n);
Sign up to request clarification or add additional context in comments.

Comments

3

You'll need to dynamically allocate the memory, like this:

double *p = new double[n];

You can use it just like an array, since pointer syntax in C++ makes that convenient:

p[0] = 3.14;
p[n-1] = 1.14;

Once you're done using it, you must discard the memory using the delete operator:

delete [] p;

Note that we use the vector [] version of the delete operator since we used the vector version of the new operator.

Note that some compilers have an extension which allows dynamically sized local arrays like you tried to code. gcc is one such compiler. That's not a part of the language standard yet, though some day it might be.

14 Comments

-1 for new[] and delete[].
@dutt: Well, it's hideously unsafe in basically every way imaginable, for one thing. What's depressing is that anybody upvoted this terrible answer.
@dutt This will work, but it introduces undesirable risk for absolutely no reward. An std::vector<> essentially does the same thing, but with the benefit of good RAII, etc. I wouldn't down vote this, as it will technically work; but there are better solutions.
@BenjaminLindley Right before he was scooped up by the NSA.
@Arivind: Using dynamic allocation with a pointer can be just as safe as using a vector, if done correctly. The problem is that doing it correctly is a lot more work. A single delete is not enough. You need to account for exceptions that might be thrown before the delete, otherwise it will not be executed, and you have a resource leak. With all that in mind, the issue becomes maintainability and readability. The vector is safe with a single line, its declaration. The pointer requires all that boiler plate to be safe, so why bother with it?
|
2

You can use Vector. It can also be resized on run time so if you want to add more data, you won't have to worry about creating new array of desired size and copying the current data in that.

you can use vector & this is a better approach to solve your problem as it is less risky;

double kplus2(double a, double ks, double kr)
{
  int n = (ks-1)*100000;

  vector<double> x(n);  // you don't have to worry about deallocating it cuz its a container. It can grow in size at run time

  ...

 return 0;

}

However you can also do this:

double kplus2(double a, double ks, double kr)
{
  int n = (ks-1)*100000;

  double *x=new double[n]; // array of size i

  // you should also deallocate this dynamically allocated array at the end


  .....

  delete []x;

  return 0;

}

1 Comment

For your second example, what if an exception is thrown between new and delete? It's fine to teach manual memory management, but please make sure to teach how it should be done properly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.