Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

String literal

ulkaulka Posts: 17Member
Recently i was asked

a local pointer initialised by a string literal. where the string is stored

stack heap or somewhere else ???

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : Recently i was asked
    :
    : a local pointer initialised by a string literal. where the string is
    : stored
    :
    : stack heap or somewhere else ???

    [color=Blue]
    If you mean that:
    [/color]
    [code]
    char* pszText = "Hello!";
    [/code]
    [color=Blue]
    Then the location of the string is in static memory - a simple global variable. A 32-bit compiler will most likely to tag this memory as a read only, so if you try this:
    [/color]
    [code]
    pszText [1] = 'A';
    [/code]
    [color=Blue]
    The program crashes in this case, because the memory where "Hello!" is stored is read-only - can't modify that memory.
    [/color]
  • ulkaulka Posts: 17Member
    : : Recently i was asked
    : :
    : : a local pointer initialised by a string literal. where the string is
    : : stored
    : :
    : : stack heap or somewhere else ???
    :
    : [color=Blue]
    : If you mean that:
    : [/color]
    : [code]:
    : char* pszText = "Hello!";
    : [/code]:
    : [color=Blue]
    : Then the location of the string is in static memory - a simple
    : global variable. A 32-bit compiler will most likely to tag this
    : memory as a read only, so if you try this:
    : [/color]
    : [code]:
    : pszText [1] = 'A';
    : [/code]:
    : [color=Blue]
    : The program crashes in this case, because the memory where "Hello!"
    : is stored is read-only - can't modify that memory.
    : [/color]



    My question is somewhat ambiguous! Let me rephrase.

    I i have a statement say.

    char *ptr="abcdefg";

    that is written within a funcion. Where shall be the

    1. ptr allocated ?? As it (may)get distroyed and recreated with every call to the function.

    2. for string literal, I still assume static allocation.

    case 2 :if the same statement is in main().

    in this case where will be the pointer and string literal will be allocated. ??

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : My question is somewhat ambiguous! Let me rephrase.
    :
    : I i have a statement say.
    :
    : char *ptr="abcdefg";
    :
    : that is written within a funcion. Where shall be the
    :
    : 1. ptr allocated ?? As it (may)get distroyed and recreated with
    : every call to the function.
    :

    ptr will most likely be on the stack, though it's possible that it's stored in a processor register. But for simplicity: the stack.

    : 2. for string literal, I still assume static allocation.
    :

    This is what AsmGuru said: it's either in the program's heap memory, or in a piece of static (read-only) memory.

    : case 2 :if the same statement is in main().
    :
    : in this case where will be the pointer and string literal will be
    : allocated. ??
    :

    I'll simply answer this by stating that main() is a function.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • LundinLundin Posts: 3,711Member
    There are roughly 5 kinds of memory:

    1) Statically allocated
    2) Automatically allocated (stack/registers)
    3) Dynamically allocated (heap)
    4) Read-only memory
    5) Program memory


    1) All global variables and all variables declared with the keyword static. ANSI C/C++ states that they will be initialized to zero before the program starts. These are the only variables that have a known value at program start, without the programmer explicitly initializing them. RAM.

    2) All local variables. (Local constants might end up here too, though that depends on the type of system.) RAM.

    3) Memory explicity allocated by the programmer with malloc or new. RAM.

    4) All global constants. Strings end up here since they are a special case treated as constant character arrays. On a PC: RAM. Non-PC systems: non-volatile read-only memory.

    6) The code itself, as well as constant numeric values written in the code (#defines etc). On a PC: RAM. Non-PC systems: non-volatile read-only memory.
Sign In or Register to comment.