stack

When and how to use GCC's stack protection feature?

Hello, I have enabled the -Wstack-protector flag when compiling the project I'm working on (a commercial multi-platform C++ game engine, compiling on Mac OS X 10.6 with GCC 4.2). This flag warns about functions that will not be protected against stack smashing even though -fstack-protector is enabled. GCC emits some warnings when buildi...

Checking heap integrity and stack size in C#

Hi, I'm trying to track down a crash that happens when I stress my C# code and run in low memory conditions. However, in some cases, instead of getting OutOfMemoryException, my program will simply crash and exit. This is usually caused by memory corruption from overrunning a buffer or because of stack overflow (or corruption). So, is th...

How are things stored in stack?

So I have been learning assembly and came to the topic of stack, storing local, static and global variable and stuff. But I'm having a hard time imagining it in my head. Bottom of memory but top of stack, :S whaa?? The thing that got me confused is, every time something gets push(ed) into stack, stack pointer gets subtracted. Shouldn'...

How do I write to a different thread's stack in C/C++?

I know this is a bad idea! Certainly for safe programming the stack for a given thread should be considered private to that thread. But POSIX at least guarantees that all of a thread's memory is shared and writeable by other threads, which means the stack of one thread can (in theory) be written to by another thread. So I'm curious how o...

C - Pointer to int to get elements in stack

Hello! I wanted to write a standard stack in C but I am not sure if my stk_size() function could work on other platforms except for my 32bit pc. I read that its not good to cast a pointer to int. But what could be a better implementation? I dont want to add a "size"-variable because its redundant in my eyes. Here are some parts of the ...

How to calculate the memory size of a program?

Lets say I have a c program where I use only stack variables, no dynamic variables (malloc, ...) Is it possible to calculate how much memory my program will take during run time? ...

Does stack size grow during runtime?

I wonder if stack size can grow like heap does during runtime? ...

Do threads share the heap?

As far as I know each thread gets a distinct stack when the thread is created by the OS. I wonder if each thread has a heap distinct to itself also? ...

"Down the stack"

If I'm trying to find a bug that's being called lower in the call stack, that'd be "down" the stack, right? ...

Does stack grow upward or downward?

I have this piece of code in c: int q=10; int s=5; int a[3]; printf("Address of a: %d\n",(int)a); printf("Address of a[1]: %d\n",(int)&a[1]); printf("Address of a[2]: %d\n",(int)&a[2]); printf("Address of q: %d\n",(int)&q); printf("Address of s: %d\n",(int)&s); The output is: Address of a: 2293584 Address of a[1]: 2...

When is stack space allocated for local variables?

I have a question about the following C code: void my_function() { int i1; int j1; // Do something... if (check_something()) { int i2; int j2; // Do something else... } // Do some more stuff... } Are there any guarantees about when stack space is allocated/deallocated for i2 and ...

Aligning a class to a class it inherits from? Force all stack alignment? Change sizeof?

I want to have a base class which dictates the alignment of the objects which inherit from it. This works fine for the heap because I can control how that gets allocated, and how arrays of it get allocated in a custom array template. However, the actual size of the class as far as C++ is concerned doesn't change at all. Which is to say i...

Stack of photos AS3 - stuck on somthing simple

Hi there I found a tutorial on creating a stack of photos in flash using AS3 (http://designreviver.com/tutorials/create-an-interactive-stack-of-photos/). I have been trying to make a dynamic XML version of the photo stack and I have a problem (obviously :) I have a class called Polaroid, and im using a loop to add multiple instances o...

Exception Handling in C - What is the use of setjmp() returning 0?

I have a few questions relating to setjmp/longjmp usage - What is the use of setjmp(jmp___buf stackVariables) returning 0. It is a default, which we cannot influence. Is the only significance of setjmp(stackVariables) is to push the stack in stackVariables. And basically 0 tells us if the stack was pushed on stack_variables success...

How to determine return address on stack ?

I know that if I am inside some fuction foo() which is called somewhere from bar() function, then this return address is pushed on stack. #include <stdio.h> void foo() { unsigned int x; printf("inside foo %x \n", &x ); } int main() { foo(); printf("in main\n")...

Is there any way to determine the available stack space at run time?

I know that stack size is fixed. So we can not store large objects on stack and we shift to dynamic allocations (e.g. malloc). Also, stack gets used when there is nesting of function calls so we avoid recursive functions as well for this reason. Is there any way at runtime to determine how much stack memory is used so far and how much is...

negative number in the stack

Hello everyone, I am a new student in the compilers world ^_^ and I want to know is legal represent negative number in the stack. For example: infix: 1-5=-4 postfix: 15- The statements are: push(1) push(5) x=pop() y=pop() t=sub(y,x) push(t) The final result in the stack will be (-4) How can i represent this if it is legal?? ...

Stack memory fundamentals

Consider this code: char* foo(int myNum) { char* StrArray[5] = {"TEST","ABC","XYZ","AA","BB"}; return StrArray[4]; } When I return StrArray[4] to the caller, is this supposed to work? Since the array is defined on the stack, when the caller gets the pointer, that part of memory has gone out of scope. Or will this code work...

iPhone Device/Simulator memory oddities using Objective-C++

I am porting a project to the iPhone (from Windows Mobile) and sharing much of the generic C and C++ code as possible, using Objective-C++. However, during testing I have stumbled upon a curious and troublesome problem that only manifests when running on the device. I have distilled the problem code in to a new project to prove reprodu...

Prolog parse postfix math expressions

I solved this my self. I'll post the solution when were past due date for my homework. Okay, I'm going to build a parser or an evaluator. The de facto standard when parsing with prefix notation is to just use a stack. Add to the stack if input is a number, if it is an operator you can pop twice apply operator and put the result back on...