-
December 24th, 2009, 05:12 AM
#1
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?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 24th, 2009, 05:19 AM
#2
Re: Sizing a (stack based) array at run time
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.
-
December 24th, 2009, 05:27 AM
#3
Re: Sizing a (stack based) array at run time
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++.
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
-
December 24th, 2009, 06:40 AM
#4
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?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 24th, 2009, 06:57 AM
#5
Re: Sizing a (stack based) array at run time
Yes, assuming that buf_size > 0:
Code:
read_samples(&buffer[0], buf_size);
-
December 24th, 2009, 07:55 AM
#6
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.
-
December 24th, 2009, 08:01 AM
#7
Re: Sizing a (stack based) array at run time
Thanks guys. Drinks all round!! (well, ratings all round anyway )
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 24th, 2009, 08:04 AM
#8
Re: Sizing a (stack based) array at run time
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
-
August 29th, 2021, 07:54 AM
#9
Re: Sizing a (stack based) array at run time
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
Or is it just a "gcc thing" ?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
August 29th, 2021, 02:21 PM
#10
Re: Sizing a (stack based) array at run time
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
Or is it just a "gcc thing" ?
I guess it is.
Victor Nijegorodov
-
August 30th, 2021, 03:19 AM
#11
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|