Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

1 )Heap and free Store & 2) memory for const var.

himsymcpphimsymcpp Member Posts: 4
Hi friends,

1)
I want to know whether there is any dfference between Heap and free Store in C++ (dynamic memory) or they are the same i.e just the synonyms used?

2)
One more thing I want to know is that where do, const variables (i.e constants or literals get memory) , is there is separate area for them or they are allocated in the data region only.

3)
One more thing i want to know is that where can i get the "C++ standard docs"?

Thanks,
himanshu k.
Symbian Application Developer.

Comments

  • LundinLundin Member Posts: 3,711
    : Hi friends,
    :
    : 1)
    : I want to know whether there is any dfference between Heap and free
    : Store in C++ (dynamic memory) or they are the same i.e just the
    : synonyms used?

    "Free store" and dynamic memory allocation are just synonyms for the same thing. The heap is the name for the memory area where the dynamic memory is stored.


    : 2)
    : One more thing I want to know is that where do, const variables (i.e
    : constants or literals get memory) , is there is separate area for
    : them or they are allocated in the data region only.

    This is very much system-specific. Const variables can be allocated in three different places: in ROM, in RAM or in code ROM. On a RAM-based system such as a PC, the ROM is just another segment of the RAM, as there is no physical ROM.

    - The variable ends up in ROM if you declare it as a global constant.
    - The variable ends up in RAM if you declare it as a local constant or a parameter to a function.
    - The variable ends up in code ROM if the compiler optimizes the expression where the variable is used. It can also end up in ROM if you use numeric constants or strings:

    char x = 5; /* 5 is allocated in code ROM */
    char* ptr =
  • himsymcpphimsymcpp Member Posts: 4
    hi Sir,

    Thanks for your valuable reply.

    1) what do you mean by system specific
    does it mean 1) Underlying OS (Windows XP).
    2) C++ Compiler(Microsoft Visual Studio).

    If these is what i have, can i get the exact memory map of all the variables.

    2) As you said, 'Constants' can be allocated in three different places: in 1] ROM,
    2] RAM,
    3] Code ROM.

    Here are 4 sections or areas in memory where memory allocation takes place.

    1) STACK SEGMENT : local CONSTANT.
    THEY WIL GET MEMORY IN RAM.

    2) DATA SEGMENT : static and global CONSTANT.
    THEY WIL GET MEMORY IN ROM.

    3) HEAP SEGMENT : dynamic CONSTANT.
    const int *p = new int(10);
    THEY WIL GET MEMORY IN RAM.

    4) CODE SEGMENT : literals.
    int x=5;
    THEY WIL GET MEMORY IN CODE ROM.(for ex 5).

    This is what i have understood from explanation.
    please verify.

    Thanks,
    himanshu k.
    Symbian Application Developer.

  • LundinLundin Member Posts: 3,711
    : hi Sir,
    :
    : Thanks for your valuable reply.
    :
    : 1) what do you mean by system specific
    : does it mean 1) Underlying OS (Windows XP).
    : 2) C++ Compiler(Microsoft Visual Studio).

    The OS doesn't matter much, a PC and a Unix machine are quite similiar since they are both RAM-based systems. A PC doesn't have any real ROM, except for a few hardware registers. On a microcontroller or a DSP on the other hand, there is real ROM on the chip, where the code will end up. On such systems the code is executed straight from ROM, while on a PC the whole program is copied to RAM and executed from there.

    : If these is what i have, can i get the exact memory map of all the
    : variables.

    On an embedded system as those I mentioned, yes you can. On a PC it isn't meaningful to know where a certain variable is allocated, since it will be a different RAM address each time the program is executed.


    : Here are 4 sections or areas in memory where memory allocation takes
    : place.
    :
    : 1) STACK SEGMENT : local CONSTANT.
    : THEY WIL GET MEMORY IN RAM.
    :
    : 2) DATA SEGMENT : static and global CONSTANT.
    : THEY WIL GET MEMORY IN ROM.
    :
    : 3) HEAP SEGMENT : dynamic CONSTANT.
    : const int *p = new int(10);
    : THEY WIL GET MEMORY IN RAM.
    :
    : 4) CODE SEGMENT : literals.
    : int x=5;
    : THEY WIL GET MEMORY IN CODE ROM.(for ex 5).
    :
    : This is what i have understood from explanation.
    : please verify.


    Yes, the above is correct.
Sign In or Register to comment.