0

As per my understanding, There are four part of memory.

  • Data Segment (BSS, Non BSS)
  • Code Segment
  • Heap Segment
  • Stack Segment

Stack segment will store all the local variables declared inside the function. Dynamically created variable will go inside heap. Initialized global variable will inside data segment - BSS area. Non-Initialized global variable will inside data segment - Non BSS area. All const variable & macro will go inside code area.

We can instruct the compiler to store particular variable at particular location inside the memory using #pragma.

I don't know where does const pointer and pointers get stored. is it inside data segment?

Please let me know my understanding is correct or not?

I have code:

#include <stdio.h>
#include <stdlib.h>

#define MAX 10
enum color{
    RED,
    GREEN,
    BLUE
};

int a;
int b=5;
static int c;
static int d=10;
extern int e;
const int f;
const int g = 15;
const int *ptr1;
int *ptr2;

int main(void) {
    // your code goes here
    int h;
    int i=20;
    static int j;
    static int k=25;
    const int l;
    const int m = 30;
    int *ptr3;
    const int *ptr4;

    return 0;
}

In this code, where does these variable a to m will get stored?

NAND
  • 663
  • 8
  • 22
kapil.thakar
  • 117
  • 4
  • Even if it is a homework question, then what? It's an interesting one, and you clearly did some research. +1 – user4520 Jun 10 '15 at 09:32
  • Regarding the heap, unless you are programming for some small embedded system with a real-time operating system, there is no heap "segment", it's all handled run-time by the standard library and operating system. Also, the actual pointer *variables* are stored either in the data, bss (which is combined with the data segment in memory) or on the stack (depending on where it was defined and how it was initialized). The memory assigned to a pointer variable may be allocated on the heap though. – Some programmer dude Jun 10 '15 at 09:38
  • You've got `bss` and `data` wrong. `data` contains arbitrarily initialized variables, `bss` contains zero- (or NULL-) initialized variables. Also, some variables may be in `rodata` (read-only-data) on some systems. – EOF Jun 10 '15 at 09:39

0 Answers0