stack

Copy std::stack into an std::vector

喜你入骨 提交于 2019-12-20 18:33:07
问题 Is the following code guaranteed by the standard to work(assuming st is not empty)? #include <vector> #include <stack> int main() { extern std::stack<int, std::vector<int> > st; int* end = &st.top() + 1; int* begin = end - st.size(); std::vector<int> stack_contents(begin, end); } 回答1: Yes. std::stack is just a container adapter. You can see that .top() is actually (§23.3.5.3.1) reference top() { return c.back(); } Where c is the container, which in this case is a std::vector Which means that

C program to find direction of stack growth

泄露秘密 提交于 2019-12-20 10:57:22
问题 How do I find in C whether a stack is progressing in forward or reverse direction ? WIll this work? int j = 0; int k = 0; if (&k > &j) printf ("Stack is growing in forward direction"); else if (&k < &j) printf ("Stack is growing in reverse direction"); 回答1: To be reliable, one would have to find the difference between two function calls. void func(int *p) { int i; if (!p) func(&i); else if (p < &i) printf("Stack grows upward\n"); else printf("Stack grows downward\n"); } func(NULL); Note that

Beginner's confusion about x86 stack

醉酒当歌 提交于 2019-12-20 10:54:52
问题 First of all, I'd like to know if this model is an accurate representation of the stack "framing" process. I've been told that conceptually, the stack is like a Coke bottle. The sugar is at the bottom and you fill it up to the top. With this in mind, how does the Call tell the EIP register to "target" the called function if the EIP is in another bottle (it's in the code segment, not the stack segment)? I watched a video on YouTube saying that the "Code Segment of RAM" (the place where

how can i see how much of the stack space is currently used in my delphi app?

独自空忆成欢 提交于 2019-12-20 10:44:12
问题 how can i see how much of the stack space is currently used in my delphi app? i had a very strange error that sounds like stack trouble. i'd like to add it to my app's log to get some idea how much stack space is in use/remaining. using the debugger is probably not so great because the routine can be called many times. thank you! 回答1: {$IFDEF MSWINDOWS} function currentStackUsage: NativeUInt; //NB: Win32 uses FS, Win64 uses GS as base for Thread Information Block. asm {$IFDEF WIN32} mov eax,

Android: How to make launcher always open the main activity instead of child activity? (or otherwise)

旧时模样 提交于 2019-12-20 10:43:31
问题 I have activities A and B. The A is the one with LAUNCHER intent-filter (i.e. the activity that is started when we click the app icon on home screen). A launches B using startActivity(new Intent(A.this, B.class)) . When the user has the B activity open, and then put my application into the background, and later my application's process is killed, when the user starts my application again, B is opened instead of A. This caused a force close in my app, because A is the activity that initializes

Where are variables in a closure stored - stack or heap?

二次信任 提交于 2019-12-20 10:33:36
问题 Like the following codes: var foo = function() { var a = 1; // closure var return function() { // closure fun console.log(a); } }; var bar = foo(); When foo exits(or say, returns), we know that the variable a will not be destroyed and remains in memory(that's why closure works). So my problem is where does the the variable a store, stack or heap? 回答1: A closure is just an evolution of the concept of the stack. The stack is used to separate/isolate scope when functions are called. When a

C# - Garbage Collection

亡梦爱人 提交于 2019-12-20 08:50:12
问题 Ok so I understand about the stack and the heap (values live on the Stack, references on the Heap). When I declare a new instance of a Class, this lives on the heap, with a reference to this point in memory on the stack. I also know that C# does it's own Garbage Collection (ie. It determines when an instanciated class is no longer in use and reclaims the memory). I have 2 questions: Is my understanding of Garbage Collection correct? Can I do my own? If so is there any real benefit to doing

How do Haskell compilers decide whether to allocate on the heap or the stack?

谁都会走 提交于 2019-12-20 08:49:33
问题 Haskell doesn't feature explicit memory management, and all objects are passed by value, so there's no obvious reference counting or garbage collection either. How does a Haskell compiler typically decide whether to generate code that allocates on the stack versus code that allocates on the heap for a given variable? Will it consistently heap or stack allocate the same variables across different call sites for the same function? And when it allocates, how does it decide when to free memory?

Set stack size with setrlimit() and provoke a stack overflow/segfault

こ雲淡風輕ζ 提交于 2019-12-20 08:47:00
问题 In the given example below I try to set the stacksize to 1kb. Why is it now possible to allocate an array of ints on the stack with size 8kb in foo() ? #include <stdio.h> #include <sys/resource.h> void foo(void); int main() { struct rlimit lim = {1024, 1024}; if (setrlimit(RLIMIT_STACK, &lim) == -1) return 1; foo(); return 0; } void foo() { unsigned ints[2048]; printf("foo: %u\n", ints[2047]=42); } 回答1: The limit is set immediately but only checked when trying to allocate a new stack or

How does one implement a “stackless” interpreted language?

梦想的初衷 提交于 2019-12-20 08:39:23
问题 I am making my own Lisp-like interpreted language, and I want to do tail call optimization. I want to free my interpreter from the C stack so I can manage my own jumps from function to function and my own stack magic to achieve TCO. (I really don't mean stackless per se, just the fact that calls don't add frames to the C stack. I would like to use a stack of my own that does not grow with tail calls). Like Stackless Python, and unlike Ruby or... standard Python I guess. But, as my language is