Memory layout / Address space of variables in C program

 $ vim address_space.c 
#include <stdio.h>
int bss_var; /* Uninitialized global variable */
int data_var = 1; /* Initialized global variable */
int main(int argc, char **argv) {
        void *stack_var;
        /* Local variable on the stack */
        stack_var = (void *)main;
        /* Don't let the compiler */
        /* optimize it out */
        printf("Hello, World! Main is executing at %p\n", stack_var);
        printf("This address (%p) is in our stack frame\n", &stack_var);
        /* bss section contains uninitialized data */
        printf("This address (%p) is in our bss section\n", &bss_var);
        /* data section contains initializated data */
        printf("This address (%p) is in our data section\n", &data_var);
        return 0;
}
 $ gcc -o address_space address_space.c 
 $ ./address_space
Hello, World! Main is executing at 0x804846b
This address (0xbffccbb8) is in our stack frame
This address (0x804a028) is in our bss section
This address (0x804a020) is in our data section

Refer https://www.geeksforgeeks.org/memory-layout-of-c-program/ for more details.

Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply