-1
// testing1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>
#include <iostream>
#include<conio.h>

using namespace std;


struct Data_point {
  double  x;
  double  y;
};


    void PlotThis(unsigned int n ) 
{
    Data_point graph[n];  //shows error here 

    //do something else, dont worry about that  
}


int main ()

{

unsigned int nSamples;  
cout << "Please enter nSamples: ";
cin >> nSamples;
PlotThis(nSamples);
return 0; 

}

This shows error when compiling:

Error   1   error C2057: expected constant expression testing1.cpp  23

Error   2   error C2466: cannot allocate an array of constant size 0 testing1.cpp   23

Error   3   error C2133: 'graph' : unknown size  testing1.cpp   23

Line 23 is Data_point graph[n]; //shows error here

It is showing unknown size even though I am passing it the value from main(). It is asking for the value (the size of graph i.e n) at the compile time. Does that mean array size allocation takes place at compile time? How to solve this

user2799508
  • 844
  • 1
  • 15
  • 41
  • No error when i compiling with g++ 4.6.3 on ubuntu 12 – Vink Jul 02 '14 at 12:48
  • @Vink, Compile with `-pedantic` (or `-pedantic-errors` to actually match your statement). – chris Jul 02 '14 at 12:49
  • 1
    Related question that provide more details on what would be considered a constant here: [Does “int size = 10;” yield a constant expression?](http://stackoverflow.com/questions/21273829/does-int-size-10-yield-a-constant-expression/21273849#21273849). – Shafik Yaghmour Jul 02 '14 at 12:49

2 Answers2

3

C++ doesn't support arrays with size determined at run-time. You could switch to using the Vector class instead.

std::vector<Data_point > graph;

To follow the logic you're using: In PlotThis you could use std::vector::resize to resize the container to contain n elements.

void PlotThis(unsigned int n){
    graph.resize(n); // Resize Vector container
    ...

Also with std::vector:

"compared to arrays, vectors consume more memory in exchange for 
 the ability to manage storage and grow dynamically in an efficient way."

This means you have the option to not worry about specifying the size of the vector and just adding elements as you need to. So you could have a loop determine how many elements are added (n) in your method - possible to use std::vector::push_back. If you do this then just make sure to clear the vector at some point so you don't re-use old data - possible to use std::vector::clear.

Andrew_CS
  • 2,542
  • 1
  • 18
  • 38
0

You can use some containers, for example vector, deque etc...

void PlotThis(unsigned int n) 
{
    std::vector<Data_point> graph(n);

    //do something else, dont worry about that  
}

Or allocate memory for array dynamically:

//C++
void PlotThis(unsigned int n) 
{
    Data_point* graph = new Data_point[n];

    //do something else, dont worry about that  

    //Remember to free memory
    delete graph;
}


//C
void plot_this(unsigned int n) 
{
    Data_point* graph = (Data_point*) malloc(sizeof(Data_point) * n);

    //do something else, dont worry about that  

    //Remember to free memory
    free(graph);
}
Dakorn
  • 883
  • 6
  • 11