CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Sizing a (stack based) array at run time

    Quote Originally Posted by John E View Post
    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

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Sizing a (stack based) array at run time

    Yes, assuming that buf_size > 0:
    Code:
    read_samples(&buffer[0], buf_size);
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Sizing a (stack based) array at run time

    Quote Originally Posted by John E View Post
    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

  9. #9
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Sizing a (stack based) array at run time

    Quote Originally Posted by John E View Post
    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

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Sizing a (stack based) array at run time

    Quote Originally Posted by John E View Post
    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

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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
  •  





Click Here to Expand Forum to Full Width

Featured