1,056 questions
3
votes
1
answer
179
views
Memory Layout of C Programs: How is size of stack-heap space determinded?
I was reading an article on the Memory Layout of C Programs; https://www.geeksforgeeks.org/c/memory-layout-of-c-program/.
It mentioned, under "4. Stack Segment":
When the stack and heap ...
1
vote
2
answers
198
views
Call Stack - What is stored in the call stack between the declaration of a caller function's last variable and a callee function's first variable?
I am using an Ubuntu 16.04 a 32-bit Virtual Machine OS.
I executed
sysctl -w kernel.randomize_va_space=0
to disable ASLR in root prior to gcc.
I have also compiled this using the command:
gcc -g -fno-...
17
votes
3
answers
1k
views
Stack memory in a freestanding environment
I'm reading a GNU as introductory book for programs running on top of an OS. I'm at the stack memory part and I was curious how the stack looks like in a freestanding environment.
I'm guessing that ...
2
votes
3
answers
267
views
How to detect whether a variable is allocated on the stack or the heap in C?
I’m working on a C project where I need to know if a pointer refers to memory allocated on the stack or the heap.
For example:
int x = 42; // stack variable
int *y = malloc(sizeof(...
1
vote
1
answer
245
views
What are the proper conventions for x64 assembly functions on Windows?
I've been messing around with assembly and compilers & have gotten a lot of conflicting information in how I should be using registers, the stack / spill over registers, and to what extent ...
1
vote
1
answer
61
views
Confused about stack, and ESP register ADD instruction after function call [duplicate]
I have disassembled a basic function into assembly language. The function is the 'main' function in a console application for Windows. It simply calls another function (named 'a') which does nothing, ...
0
votes
1
answer
86
views
Function parameter memory address assignment [closed]
I want to understand how memory is allocated to function params in c. I compiled the same program twice with gcc -g except I interchanged the first and second params of a function on_menu for the ...
3
votes
0
answers
165
views
Is there a way to determine the stack size limit / end point within a running UEFI application?
I have a fairly large UEFI application (2.1MB UPX compressed) that has a number of functions that allocate reasonably large data structures on the stack (kilobytes). I'd like to add some debug code ...
1
vote
1
answer
90
views
Is it valid to initialize a struct with const members allocated on the stack with alloca?
Considering that the type A is defined like this:
typedef struct a { const int a; } A;
I know that this code is valid:
A * foo = malloc(sizeof *foo); // Allocates uninitialized memory with no ...
1
vote
0
answers
77
views
Why does the compiler allocate too much memory?
I have a bit of C code for an ARM Cortex-M0 chip, for which I was investigating the disassembly:
int func2(unsigned a) {
int h[a];
for (unsigned i = 0; i < a; i++) {
h[i] = 0;
}
int ch;...
2
votes
0
answers
129
views
Why rsp is subracted with 4096 bytes (page size)? [duplicate]
I know each segment in the executable file need to be aligned, before loaded into ram there were aligned to minimum page size of 4096 bytes and if there not enough fit within 4096 bytes they span ...
1
vote
1
answer
147
views
Is arr in stack or heap?
I want to understand if arr here is in stack or heap. Since obj is dynamically allocated is the arr in heap? What if i do not have obj2, if i just allocate obj dynamically in main?
#include <...
0
votes
0
answers
67
views
Are local variables always volatile? [duplicate]
Are local variables always volatile in C#?
In other words, is the volatility of num the same in these two classes:
public sealed class ThreadSafeObjectA
{
private volatile int num;
public int ...
-1
votes
1
answer
111
views
(ARM assembly) how to address elements on the stack relative to the sp
How can I address elements on the stack that are not on top of it? I am talking about for instance first executing stmfd sp!, {r0-r12} and then push {lr}. Now I want to work with the first address of ...
1
vote
4
answers
161
views
Why can the stack collide with the heap if they are located in a virtual space that is very large in size?
If each process has its own address space and it is much larger than the physical memory of the computer, why can’t we just place the stack at the end of this address space and the heap at the ...
1
vote
1
answer
688
views
Understanding Stack Frames and Stack Layout in Function Calls on x86 Systems
I'm currently exploring stack frames and how they work in C programs, specifically on unprotected 32-bit x86 systems (no ASLR, stack canaries, or DEP). I'm not primarily a CS Student — I'm a physics ...
1
vote
0
answers
103
views
Assembly x64: Issue with Function Calls and Stack Alignment in Assembler-Disassembler Tool on Windows
I'm working on an assembler-disassembler tool in x64 assembly on Windows, and I'm encountering a strange issue related to function calls and stack alignment. The program is supposed to support basic ...
1
vote
0
answers
66
views
Inconsistent usage of static/stack/heap memory
I've been messing around with assembly for a while and I found things which seems inconsistent. After some research I have some questions about stack / static (initialized/uninitialized) / heap memory....
1
vote
1
answer
75
views
How can I store a value on the stack at a constant address in the memory in Webassembly?
I have a value on the stack, and I want to store it at a constant address in memory. Would the operation take the address from the stack first, I could put the address on the stack and store the value ...
0
votes
1
answer
104
views
How does array padding works in Stack?
The following question refers to x86 assembly, and little endianness.
Suppose I have the following code in C:
unsigned char myID[10] = "211866744";
How will this array be saved in memory?
...
-3
votes
1
answer
155
views
How is Numpy able to get an array size at run-time?
In C++ the size of an array must be determined at the compile-time.
I want to write a simple code in C++, say, to do a naive matrix multiplication with itself (for a matrix that is square in size) and ...
2
votes
2
answers
143
views
If the stack grows downwards, how does it not overlap with other stuff in the address space?
I realized I never really thought of this.
If I made a large enough recursive call chain, wouldn't the stack eventually grow down enough that it will overlap with other things, like shared libraries (...
0
votes
1
answer
265
views
Stack size in relation to virtual memory
In our Operating Systems class we mentioned virtual memory as a mechanism that abstracts physical memory to a process, and that it looks something like this (per process):
The stack grows down the ...
5
votes
1
answer
454
views
Why does the Stack Pointer in MIPS Typically Start at 0x7FFFFFFC but not 0x80000000?
According to Patterson & Hennessy's Computer Organization and Design (MIPS Edition),
the stack pointer $sp is typically initialized to 0x7FFFFFFC.
the stack pointer $sp is always pointing at the ...
0
votes
2
answers
134
views
Does Stack being limited in size mean i can only get limited pointers to objects in heap?
if stack size is 1MB, does that mean i can only get less than 1000000/8 pointers to allocate ints in heap? (considering 1MB free stack) yeah i know you might not want to make that many individual ints ...
-1
votes
2
answers
314
views
Why is there no time cost to large stack allocations
I tried this quick-bench test and I'm finding that it's the same cost timewise to allocate 200 bytes as it is to allocate 2000000 bytes.
How could that possibly be?
0
votes
0
answers
47
views
With what part of the program are the stack and heap associated?
I understanding the stack, the LIFO working principle, memory allocation on the heap and stuff. My question is, where does this exists ?
Is there a dedicated region on the ram for the stack ? Or is it ...
0
votes
3
answers
516
views
Get stack trace from ELF and stack hex
I'm working on a core dump mechanism for STM32 mcus running FreeRTOS.
I managed to extract the stack of the running tasks, and transmit it to a server where a python script writes it into a hex file. ...
0
votes
0
answers
95
views
"Symbol not defined : @STACK " error in ASM code for 8086. Compiled using DOSBOX ,MASM
This is a code to add all numbers between 50 and 150 and display the result in decimal form.I have created the stack segment .STACK 32 to store the remainders to convert the hex result to decimal ...
-4
votes
1
answer
100
views
C-Dynamic Memory Allocation
typedef struct {
double x;
double y;
} point;
point p1;
point* p2 = malloc(sizeof(point));
In this code where the variables p1,p2,p1.x, and p2->x are stored in stack or heap memory?
Where ...
0
votes
0
answers
53
views
Why doesn't pushing a character to the stack without an explicit nul-char look like an underfined behaviour? [duplicate]
The following snippet comes from the lesson 7 on asmtutor.com :
;------------------------------------------
; void sprintLF(String message)
; String printing with line feed function
sprintLF:
call ...
0
votes
1
answer
82
views
Buffer Overflow: Why does buffer assignment impact other variables?
void foo() {
int value = 0;
char buf[4];
buf[4] = 1;
printf("value: %d\n", value);
}
int main() {
foo();
return 0;
}
Why does 'value' print 1?
I believe this has to ...
1
vote
1
answer
516
views
Aarch64 is there a Red Zone on Linux, If so 16 or 128 bytes?
There doesn't seem to be any mention of a "Red Zone" for Aarch64 in the ABI, but Microsoft makes reference to a 16-byte red zone for Aarch64, Apple claims a 128-byte red zone in Writing ...
0
votes
0
answers
29
views
Issue with Assembly Code: Program Crashes at movb store to stack memory (%esp) [duplicate]
I am currently learning assembly and I am trying to write a simple program. I have the following code:
.global do_main
.section .text
do_main:
sub $4, %esp
movb $'H', (%esp)
movb $'e', 1(%...
-3
votes
1
answer
113
views
Placing value on stack using alloca function or static keyword
I have an abstract "x vs y" question. In C programming language if I have some small amount of data which I want to store somewhere in RAM, I suppose typical options are the next ones:
to ...
0
votes
1
answer
141
views
How objects are created and initialized in detail
I don't understand the logic of why the code results in stackOverFlow error:
public class Human {
private String name;
private int age = 30;
private Human human = new Human();
...
0
votes
0
answers
47
views
Use of Stack/Heap outside of programming
If I understand correctly, the RAM is virtually divided into stack and heap. Stack takes primitive types/functions etc and Heap deals with the reference types and objects. Stack follows the LIFO ...
1
vote
1
answer
233
views
How to do pattern matching with boxed enum? [duplicate]
i have an enum where i have to perform pattern matching. But since i am running the program on VM which has limited stack memory ( < 4Kb ), i allocated the enum on the heap using Box. But while ...
0
votes
0
answers
171
views
How to allocate a separate stack for a function manually in C++?
I am trying to allocate a new stack on the heap in C++ for a function using VirtualAlloc. While debugging, I noticed that the rsp value for my function can either increase or decrease relative to the ...
0
votes
0
answers
98
views
I can't use RSP to reference the end of the stack
in my system (x86_64), when I'm using GDB, both RBP and RSP point to the same memory address after pushing a new stack frame, therefore I can't reference the end of the stack with the register RSP ...
1
vote
0
answers
500
views
Uncaught RangeError: Maximum call stack size exceeded (functionjs.js)
I'm trying to build a recruitment management website (nodejs), but when I'm almost done I get this error:
Uncaught RangeError: Maximum call stack size exceeded. functionjs.js
Here is my functionjs.js ...
2
votes
2
answers
148
views
How to pass big data from a factory to a constructor with neither dynamic memory nor unnecessary copies?
Runge-Kutta schemes consist of an algorithm, implemented in Scheme, and a piece of data, called Table (Butcher tableau).
For the construction of a scheme, we want consumers to use the syntax
Scheme s =...
1
vote
0
answers
43
views
How to get address range of a process stack in MacOS?
I'm debugging a program that has many alloca and triggers EXC_BAD_ACCESS (with -msanitize=address) or crashes __chkstk_darwin (without compiler sanitizer). I want to know the valid stack address range ...
0
votes
3
answers
109
views
How does my OS know if a defined variable is initialized or not?
At the moment I am messing around a little bit with Sorting Algorithms in C. In the course of this I have run into the following problem:
I have defined an int array with
int array[LENGTH];
where ...
3
votes
2
answers
120
views
How can I declare an array of pointers with blocks of NULL elems
I'm using an array of functions pointers, and I'd like to use it directly with their associated event IDs.
Problem is, event IDs start from 0x10 to 0x1C, and from 0x90 to 0xA5.
I don't want to write ...
1
vote
2
answers
613
views
STM32F0 - question about memory (Stack, heap)
I am bit confused about the stack and heap memory of STM32 microcontrolers (cortex M0).
Firstly, they are part of RAM, but are they part of RAM size described in the datasheet ?
Secondly, if I reduce ...
-1
votes
1
answer
315
views
Why are some smaller embedded devices unable to run an operating system (and what exactly do they run off of instead)? [closed]
For context, this was taken from an excerpt in a book:
Finally, sometimes you can't even use heap memory! If you are programming in Rust for a small embedded device, you are going to have to use only ...
0
votes
1
answer
195
views
Is there a way to calculate the bytes allocated to the stack frame of a function?
I have been given this code in C and I need to calculate the bytes allocated to the stack frame of function arith. I looked everywhere for a way to do it but everyone has a different answer.
long ...
0
votes
0
answers
103
views
Problem with displaying stack values in a loop
rephrased the question
Getting segmentation fault right after last printf after iterating and displaying stack values (2 iterations for simplicity purposes).
Just trying to display hex value on the ...
0
votes
1
answer
392
views
Memory Organization: Where Are Classes and Methods Stored?
I'm looking to understand the organization of memory in my programming language, which consists of four primary areas: stack, heap, code, and data. However, I'm unclear about where the programming ...