stack

How do C and C++ store large objects on the stack?

I am trying to figure out how C and C++ store large objects on the stack. Usually, the stack is the size of an integer, so I don't understand how larger objects are stored there. Do they simply take up multiple stack "slots"? ...

C# Out parameter question: How does Out handle value types?

UPDATE So totally pulled a tool moment. I really meant by reference versus Out/Ref. Anything that says 'ref' I really meant by reference as in SomeMethod(Object someObject) Versus SomeMethod(out someObject) Sorry. Just don't want to change the code so the answers already make sense. Far as I understand, unlike ref where it "cop...

C++ calling delete on variable allocated on the stack

Ignoring programming style and design, is it "safe" to call delete on a variable allocated on the stack? i.e. int nAmount; delete &nAmount; or class sample { public: sample(); ~sample() { delete &nAmount;} int nAmount; } ...

Java: Knuth shuffle on a Stack?

Hi, For a programming class I am creating a blackjack program for the first homework assignment. The professor has given us a sample Card class, which includes the method to add them to a deck. For her deck, she uses an ArrayList, which you can easily Knuth Shuffle with the Collections.shuffle() method. That method does not work for St...

new on stack instead of heap (like alloca vs malloc)

Is there a way to use the new keyword to allocate on the stack (ala alloca) instead of heap (malloc) ? I know I could hack up my own but I'd rather not ...

What is the best approach for a tail-optimized function for calculating the length of a list?

Here is an example that a forum poster gave, I can't tell if this tail optimized. Also, could someone give a laymans description of how a tail optimized version would trump the normal version. (defun mylength (s) (labels ((mylength-inner (s x) (if (car s) (mylength-inner (cdr s) (+ x 1)) x))) (mylength-inner s ...

Heap versus Stack allocation implications (.NET)

From a SO answer about Heap and Stack, it raised me a question: Why it is important to know where the variables are allocated? At another answer someone pointed that the stack is faster. Is this the only implication? Could someone give a code example where a simple allocation location change could solve a problem (eg. performance)? Not...

Another C# question about references/collections/value types

I have the following code: public class Test { public static void Main() { List<Person> list = new List<Person>(); Person person = new Person() { Name="Chris" }; list.Add(person); person = new Person(){ Name="Wilson the cat" }; list.Add(person); Console.WriteLine(list[0].Name); Console.WriteL...

Creating a FIFO queue in C

Is it possible to create a FIFO 'stack' in C without using 2 stacks? Thanks! (Sorry to those who answered the previous one. I was thinking LIFO and meaning FIFO.) ...

How do I implement a generic stack in C#?

I'm using Visual C# to program an RPN calculator using Stack. Problem is I don't know how to do this. I'm using System.Collections.Generic, but Stack s = new Stack(); generates the error "Using the generic type 'System.Collections.Generic.Stack' requires '1' type arguments" I'm pretty clueless here. Thanks for the help. ...

How can I view the local variables on the evaluation stack when debugging a .NET CLR application?

I'm using Windbg (with the sos extention) and trying to debug a crashed application. I was able to dump the IL of the call that threw the exception and by examining the code, it seems like I could obtain the information that I need if I could dump the contents of the evaluation stack. Is it possible to do what with WinDbg & sos ? Here...

What does it mean to duplicate a stack?

I'm reading The C Programming Language and learned how to make a reverse Polish calculator using a stack. Here is one of the exercises that follow it: Exercise 4-4. Add the commands to print the top elements of the stack without popping, to duplicate it, and to swap the top two elements. Add a command to clear the stack. What do ...

Does std::stack expose iterators?

Does the stack in the C++ STL expose any iterators of the underlying container or should I use that container directly? ...

How are variables on the stack accessed?

Suppose we have these local variables: int a = 0; int b = 1; int c = 2; int d = 3; As far as I know, these will be allocated on the system stack, like this: | | | 3 | d | 2 | c | 1 | b |_0_| a Does this mean that in order to get the value of a, the values of d, c and b must first be popped out of the stack? If so, where do these ...

When do structs not live on the stack?

I'm reading through Jon Skeet's book reviews and he is going over the numerous inaccuracies of Head First C#. One of them caught my eye: [Under Errors Section] Claiming that structs always live on the stack. In what situations would structs not live on the stack? This goes contrary to what I thought I knew about structs. ...

Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?

Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point? Imagine the following scenario: You have a workflow engine where workflows can be scripted in Python with some constructs (e.g. branching, waiting/joining) that are calls to the workflow engine. A ...

How does the stack work in assembly language?

I'm currently trying to understand how the stack works, so I've decided teach myself some assembly language, I'm using this book: http://savannah.nongnu.org/projects/pgubook/ I'm using Gas and doing my development on Linux Mint. I'm a bit confused by something: As far as I was aware a stack is simply a data structure. So I assumed i...

Checking the value of a Lua stack item from C++

Hi, how do I check the value of the top of the stack in Lua? I have the following C++ code: if (luaL_loadfile(L, filename) == NULL) { return 0;// error.. } lua_pcall(L,0,0,0); // execute the current script.. lua_getglobal(L,"variable"); if (!lua_isstring(L,-1)){ // fails this check.. lua_pop(L,1); retu...

Getting a stack overflow exception when declaring a large array

The following code is generating a stack overflow error for me int main(int argc, char* argv[]) { int sieve[2000000]; return 0; } How do I get around this? I am using Turbo C++ but would like to keep my code in C EDIT: Thanks for the advice. The code above was only for example, I actually declare the array in a function and...

Is there a cheaper way to find the depth of the call stack than using backtrace()?

My logging code uses the return value of backtrace() to determine the current stack depth (for pretty printing purposes), but I can see from profiling that this is a pretty expensive call. I don't suppose there's a cheaper way of doing this? Note that I don't care about the frame addresses, just how many of them there are. edit: These...