1
int main(){
    int size;
    cin >> size;
    int myArray[size];
    return 0;
}

Is myArray allocated on the stack? How so, if its size is unknown at compile time?

As an aside, is it possible to allocate a dynamically sized array on the stack?

These maybe bad practices but I'm asking if its allowed, not if its good practice or not.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
Cricketer
  • 2,391
  • 4
  • 24
  • 29

3 Answers3

4

Yes, where/when this is allowed (it's not actually allowed in C++, though gcc allows it as an extension) myArray will be allocated on the stack. The implementation is pretty simple: choose the size and subtract it from the stack pointer.

As mentioned, C++ doesn't currently allow this, but a proposal for a dynarray class that will allow it has been accepted into the working paper for C++ 14, so something similar will (probably) be allowed soon (if your compiler doesn't already -- some may easily do so, though I've never tested for it).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thanks. Is it also possible to resize this? I'm thinking this is not possible? Well, maybe possible, but extremely hard to implement and wouldn't be done right? – Cricketer Jul 10 '13 at 02:06
  • @user2380088: at least to my knowledge, there's no provision for resizing the array. Implementation wouldn't be particularly difficult, but there's nothing in the language to support it. – Jerry Coffin Jul 10 '13 at 02:08
  • Resizing them is possible only if they are currently at the top of the stack, which in practice it means that you can't resize them (it would be a mess to come up to some kind of language guarantee for this). – Matteo Italia Jul 10 '13 at 02:08
  • Technically, isn't it possible to resize even if its not on top of the stack? If its somewhere in the middle, you just move everything up or everything down. It would be terrible, but possible, right? – Cricketer Jul 10 '13 at 02:10
  • @user2380088: that would invalidate any pointer to the variables currently on the stack above the VLA (besides the other things). – Matteo Italia Jul 10 '13 at 02:13
  • @user2380088:- that would create havoc for any references and pointers to things previous placed on the stack. – Duck Jul 10 '13 at 02:14
  • What if the OS could be made responsible to make these updates in its virtual memory service? – Cricketer Jul 10 '13 at 02:17
  • @MatteoItalia: No -- you'd allocate a new chunk at the top of the stack, but leave the old chunk there as well, so the variables in between would remain at the same place. The only trick to it would be keeping track of where to adjust the SP to when you exited the function. – Jerry Coffin Jul 10 '13 at 02:19
  • @user2380088: no -- virtual memory (normally) only works in pages -- typically 4K or 8K at a time. Can't expand things in place anyway. – Jerry Coffin Jul 10 '13 at 02:20
  • @JerryCoffin: you are right, I was thinking just about *actual* resizing of the memory chunk... although, what you propose would just fall under "declare another VLA" or "call `alloca` again". :) – Matteo Italia Jul 10 '13 at 02:38
  • @JerryCoffin, everything I've read, including reports from experimental implementations in GCC reports nothing but havoc as far as supporting stack-based dynamic arrays in C++ (read: it's the non-POD types & stack unwind under exceptions that cause the issue). – Nathan Ernst Jul 10 '13 at 03:22
  • @NathanErnst: I guess I don't really see the issue, but I'll take your word for it. – Jerry Coffin Jul 10 '13 at 03:26
  • @JerryCoffin, there was a rather prolonged thread about it over at isocpp.org recently: https://groups.google.com/a/isocpp.org/forum/?fromgroups#!search/array$20variable/std-proposals/DT0Nz7AEP3g/UJngZXPqUewJ – Nathan Ernst Jul 10 '13 at 03:32
2

Yes, it's perfectly possible, though as it's been just pointed out this is only available in ISO C99 and as a GCC extension in C++. If I may quote from the GNU website:

Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++. These arrays are declared like any other automatic arrays, but with a length that is not a constant expression.

Nobilis
  • 7,310
  • 1
  • 33
  • 67
0

It looks (cin) like you want to use C++. If so, perhaps you should use std::vector.

To set the size, yes, on the stack, see How to set initial size of std::vector?

Community
  • 1
  • 1
Fred Mitchell
  • 2,145
  • 2
  • 21
  • 29