Sizing a (stack based) array at run time
I'm converting some code that was originally written for the gcc compiler. It has a couple of functions that look like this:-
Code:
void the_function (size_t buf_size)
{
float buffer[buf_size];
// Rest of function
}
VC++ doesn't like this (it needs a compile time constant for the buffer size). In fact, I'm a bit surprised that gcc accepts it, but no matter.
I realise I could just make the buffer heap based - but back in the days when I was using Borland C, there was a technique for getting around this problem by using a multidimensional array. Borland's compiler could accept a run time variable as the first size in a multidimensional array - so you could do something like this:-
Code:
float buffer[buf_size][1];
Does VC++ have any similar kludge?
Re: Sizing a (stack based) array at run time
Quote:
Originally Posted by John E
In fact, I'm a bit surprised that gcc accepts it, but no matter.
If you are compiling as C99 then this is just the variable length array language feature. Otherwise it is a compiler extension.
Re: Sizing a (stack based) array at run time
Quote:
Originally Posted by
John E
I'm converting some code that was originally written for the gcc compiler. It has a couple of functions that look like this:-
Code:
void the_function (size_t buf_size)
{
float buffer[buf_size];
// Rest of function
}
This is an extension of gcc. If that very same code was compiled with gcc using the correct ANSI switches (-Wall -pedantic), it would fail to compile just like in Visual C++ and any other ANSI C++ compiler. Basically, the code is not legal C++, even on gcc when compiled as ANSI C++.
Quote:
Does VC++ have any similar kludge?
No need for a kludge:
Code:
#include <vector>
//...
void the_function (size_t buf_size)
{
std::vector<float> buffer(buf_size);
// Rest of function
}
Regards,
Paul McKenzie
Re: Sizing a (stack based) array at run time
Thanks guys. Paul, your solution partially works - in as much as it gets rid of the original compiler warning. But further down in the code, the buffer gets filled, like so:-
Code:
std::vector<float> buffer(buf_size);
read_samples (buffer, buf_size);
where the first parameter to read_samples() is declared as Sample* (I'm assuming Sample must be defined somewhere as type float). Anyway, I can't pass the new buffer type to read_samples() (not even if I cast it as type Sample* ). Is there a way around that?
Re: Sizing a (stack based) array at run time
Yes, assuming that buf_size > 0:
Code:
read_samples(&buffer[0], buf_size);
Re: Sizing a (stack based) array at run time
While this is a perfectly good solution, note that std::vector uses heap memory internally.
There is a non-standard function alloca() available on several platforms (called _alloca() on Windows) which will allow you to dynamically allocate stack space. However, I doubt it invokes constructors, so don't use it with non-POD types.
Re: Sizing a (stack based) array at run time
Thanks guys. Drinks all round!! (well, ratings all round anyway :) )
Re: Sizing a (stack based) array at run time
Quote:
Originally Posted by
John E
Thanks guys. Paul, your solution partially works - in as much as it gets rid of the original compiler warning. But further down in the code, the buffer gets filled, like so:-
Code:
std::vector<float> buffer(buf_size);
read_samples (buffer, buf_size);
where the first parameter to read_samples() is declared as
Sample*
Just to add to what laserlight says, a std::vector has an array internally. The way you access that array is to get a pointer to the first element, and that is what laserlight shows.
So you can use std::vector<T> whenever a function requires a T* to denote an array of T.
Regards,
Paul McKenzie
Re: Sizing a (stack based) array at run time
Quote:
Originally Posted by
John E
I'm converting some code that was originally written for the gcc compiler. It has a couple of functions that look like this:-
Code:
void the_function (size_t buf_size)
{
float buffer[buf_size];
// Rest of function
}
VC++ doesn't like this (it needs a compile time constant for the buffer size).
Back when I first encountered this I was probably using VS2005 but I was surprised to find that it still isn't supported - even in VS2019 :cry:
Or is it just a "gcc thing" ?
Re: Sizing a (stack based) array at run time
Quote:
Originally Posted by
John E
Back when I first encountered this I was probably using VS2005 but I was surprised to find that it still isn't supported - even in VS2019 :cry:
Or is it just a "gcc thing" ?
I guess it is.
Re: Sizing a (stack based) array at run time
The C++ language standard states that the size of an array must be known at compile time. This restriction is unlikely to change in the short/medium term. Some compilers (with/without some specified options) allow a run-time array size to be used - as per C99. These are C++ language extensions. Whilst some language extensions are useful, they mean that the C++ code is non-portable as it is then not standard compliant.