Hello, To be clear: there is no such thing as stack/heap in C and C++, there are automatic variable and dynamically allocated variables, the former having their lifetime known "statically" and the latter not...
Whether a particular compiler chooses to use the stack or heap for either is its free choice, as long as it maintains the "as-if" rule. In this case, I have never heard of automatically moving an automatic variable to the heap, however LLVM routinely uses the stack for dynamically allocated variables if it can prove their lifetime (probably restricted to fixed-size variables below a certain threshold). Regarding "Variable Length Arrays" (C99), they are not valid in C++, and yes they are traditionally implemented using alloc, for better or worse. -- Matthieu On Fri, Nov 28, 2014 at 4:40 AM, Manish Goregaokar <manishsm...@gmail.com> wrote: > C++/C has a lot of "features" which seem tantalizing at first; but end up > being against the point of a systems language. > > Putting large arrays on the heap (not sure if C++ does this, but it sounds > like something C++ would do) is one -- there are plenty of cases where you > explicitly want stack-based arrays in systems programming. > > Another is the alloca-like behavior of dynamically sized stack-based > arrays (just learned about this recently). > > You always want to be clear of what the compiler is doing. Such > optimizations can easily be implemented as a library :) > > -Manish Goregaokar > > On Thu, Nov 27, 2014 at 10:20 PM, Diggory Hardy <li...@dhardy.name> wrote: > >> Shouldn't the compiler automatically put large arrays on the heap? I >> thought this was a common thing to do beyond a certain memory size. >> >> >> On Thursday 27 November 2014 04:28:03 Steven Fackler wrote: >> >> The `nums` array is allocated on the stack and is 8 MB (assuming you're >> on a 64 bit platform). >> >> On Wed Nov 26 2014 at 8:23:08 PM Ben Wilson <benwilson...@gmail.com> >> wrote: >> >> Hey folks, I've started writing some rust code lately and run into weird >> behavior when benchmarking. When running >> >> >> https://gist.github.com/benwilson512/56f84ffffd4625f11feb >> >> #[bench] >> >> fn test_overflow(b: &mut Bencher) { >> >> let nums = [0i, ..1000000]; >> >> b.iter(|| { >> >> let mut x = 0i; >> >> for i in range(0, nums.len()) { >> >> x = nums[i]; >> >> } >> >> }); >> >> } >> >> >> I get "task '<main>' has overflowed its stack" pretty much immediately when >> running cargo bench. Ordinarily I'd expect to see that error when doing >> recursion, but I can't quite figure out why it's showing up here. What am I >> missing? >> >> >> Thanks! >> >> >> - Ben >> >> _______________________________________________ >> Rust-dev mailing list >> Rust-dev@mozilla.org >> https://mail.mozilla.org/listinfo/rust-dev >> >> >> >> >> _______________________________________________ >> Rust-dev mailing list >> Rust-dev@mozilla.org >> https://mail.mozilla.org/listinfo/rust-dev >> >> > > _______________________________________________ > Rust-dev mailing list > Rust-dev@mozilla.org > https://mail.mozilla.org/listinfo/rust-dev > >
_______________________________________________ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev