Variable Length Arrays: How to create a buffer with variable size in C++

  • A+
Category:Languages

I am currently writing a moving average class.

The goal is to be able to specify the buffer size as part of the constructor when a new object of class Running_Average is created.

#include <iostream> #include "Complex.h" #include <cmath> #include<stdio.h> #include<stdlib.h> #include <windows.h>  using namespace std;  class Running_Average { public:     double sum = 0;     double average = 0;     int i;      double Average(void); // Member functions declaration     void AddSample(double);     Running_Average(int); };   Running_Average::Running_Average(int size) {     int buffersize = size;     double buffer[buffersize] = { 0 }; }  void Running_Average::AddSample(double val)  //Add new values to buffer {     for (i = 9; i>0; i--)     {         buffer[i] = buffer[i-1];     }     buffer[0] = val; }  double Running_Average::Average(void) //Calculate Average of current values in buffer {     for (i = 0; i<buffersize; i++)     {         cout << buffer[i] << endl;         sum += buffer[i];     }     average = sum / buffersize;     sum = 0;      return average; }  int main() {     double value;     int i;     int f = 0;     Running_Average test;      for (i = (40); i < (50); i++)     {         test.AddSample(i);     }      while (1)      {         i = rand() % 100;         test.AddSample(i);         value = test.Average();         cout << endl;         cout << value << endl;         cout << endl;          Sleep(1000);     }  } 

However, the constructor is giving me grief:

Running_Average::Running_Average(int size) {     int buffersize = size;     double buffer[buffersize] = { 0 }; } 

Specifically:

buffer[buffersize] 

throws an error in visual studio saying:

expression must have a constant size.

I want the user to specify what buffer size they want to work with when they create a new object by passing their value to the constructor.

How can I make this work without it throwing an error?

Thanks for your help!


Standard C++ doesn't have variable length arrays. (Why?) The size of an array must be a constant expression. Some compilers have non-standard extensions that allow VLAs, but you shouldn't rely on them. Use std::vector when you need an array which can have a variable length and can be resized.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: