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 Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures Flutter git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense Multimedia - Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized User Interface Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question