[Noob]String memory allocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DJP

    #1

    [Noob]String memory allocation

    Hi,

    I had sort of a noob question on memory allocation for strings.

    char *str1 = "Hello World!";
    char str2[] = "Hello World!";

    In the above bit are both str1 & str2 stack allocated or heap allocated
    or otherwise? Also, unless explicitly malloc'ing a string buffer are we
    not required to free it? Or are there other circumstances in which we
    are required to free?

    Hope to hear from someone out there.

    Thanks.

    DJP

  • Random832

    #2
    Re: [Noob]String memory allocation

    2006-11-15 <1163550441.082 592.27940@m7g20 00cwm.googlegro ups.com>,
    DJP wrote:
    Hi,
    >
    I had sort of a noob question on memory allocation for strings.
    >
    char *str1 = "Hello World!";
    char str2[] = "Hello World!";
    >
    In the above bit are both str1 & str2 stack allocated or heap allocated
    or otherwise?
    Well - the off-topic answer is, it depends on whether this is inside
    a function or not. They will never be "heap allocated" in the sense you
    find on typical implementations , only malloc (and functions that call
    malloc, some of which can be surprising) does that.

    (mostly back to on-topic) In either case, the first one is in a static
    nonwritable area of storage. Outside a function, the latter is in static
    writable storage, but inside a function, it is in "automatic" storage
    (that is, what you might call the stack)
    Also, unless explicitly malloc'ing a string buffer are we
    not required to free it? Or are there other circumstances in which we
    are required to free?
    You are never required to, nor permitted to, free() or realloc()
    any pointer whose value did not originate from a malloc(), calloc(), or
    realloc() call that you yourself made. [OT] or certain non-standard
    functions which are documented to call malloc [/OT]
    Hope to hear from someone out there.

    Comment

    • Default User

      #3
      Re: [Noob]String memory allocation

      DJP wrote:
      Hi,
      >
      I had sort of a noob question on memory allocation for strings.
      >
      char *str1 = "Hello World!";
      char str2[] = "Hello World!";
      >
      In the above bit are both str1 & str2 stack allocated or heap
      allocated or otherwise?
      These are not portable terms. The only really important thing is
      storage duration. Where things are stored is implementation specific.

      In the first case, the address of the first character of an anonymous
      array is stored in a pointer. That array itself has static storage
      duration, which means that it is around for the entire program. It is
      undefined behavior to modify a string literal, and it would be better
      to make str1 a const pointer.

      The second form depends on how it's declared. If it is outside of any
      function, or is declared with the "static" keyword, then it will also
      have static storage duration. Otherwise, it has automatic, which means
      it ceases to exist when the program leaves the enclosing scope.
      Also, unless explicitly malloc'ing a string
      buffer are we not required to free it?
      Not usually.
      Or are there other
      circumstances in which we are required to free?
      Unless it's a libary function that returns allocated memory.
      Hope to hear from someone out there.




      Brian

      Comment

      • DJP

        #4
        Re: [Noob]String memory allocation

        (mostly back to on-topic) In either case, the first one is in a static
        nonwritable area of storage. Outside a function, the latter is in static
        writable storage, but inside a function, it is in "automatic" storage
        (that is, what you might call the stack)
        Thanks for your response. Also, can you elaborate a little bit about
        the "static nonwritable area of storage" and the "static writable
        storage" that you mention? Thanks.

        Comment

        • Random832

          #5
          Re: [Noob]String memory allocation

          2006-11-15 <1163552325.119 783.122810@i42g 2000cwa.googleg roups.com>,
          DJP wrote:
          >(mostly back to on-topic) In either case, the first one is in a static
          >nonwritable area of storage. Outside a function, the latter is in static
          >writable storage, but inside a function, it is in "automatic" storage
          >(that is, what you might call the stack)
          >
          Thanks for your response. Also, can you elaborate a little bit about
          the "static nonwritable area of storage" and the "static writable
          storage" that you mention? Thanks.
          It means that it doesn't go away until you exit the program and you
          cannot*, or, for writable, can, write to it. [OT] On some systems, these will
          be loaded into memory from disk when the program is started in different
          'segments' or 'sections', and the nonwritable areas will be marked as
          such in the memory protection [/OT]

          *Note that your implementation won't necessarily stop you from trying.
          Instead, bad things might happen later on at an unrelated point in the
          program, or when you try to port it to another system.

          Comment

          • Simon Biber

            #6
            Re: [Noob]String memory allocation

            Default User wrote:
            DJP wrote:
            >Also, unless explicitly malloc'ing a string
            >buffer are we not required to free it?
            >
            Not usually.
            >
            >Or are there other
            >circumstance s in which we are required to free?
            >
            Unless it's a libary function that returns allocated memory.
            The only standard library functions that return allocated memory are
            malloc, calloc and realloc.

            Any other C function in a library may of course return a pointer that
            must be freed[1], but that pointer must have been returned by a malloc,
            calloc or realloc call within the library[2].

            [1] Or a pointer to somewhere within a memory block that must be freed.
            It may not be a pointer to the first byte of that memory block.

            [2] Or within another library or other code that is linked into the
            final program.

            --
            Simon.

            Comment

            • Default User

              #7
              Re: [Noob]String memory allocation

              Simon Biber wrote:
              Default User wrote:
              DJP wrote:
              Also, unless explicitly malloc'ing a string
              buffer are we not required to free it?
              Not usually.
              Or are there other
              circumstances in which we are required to free?
              Unless it's a libary function that returns allocated memory.
              >
              The only standard library functions that return allocated memory are
              malloc, calloc and realloc.
              I don't see the word the word "standard" in my sentence above.
              Any other C function in a library may of course return a pointer that
              must be freed[1], but that pointer must have been returned by a
              malloc, calloc or realloc call within the library[2].
              True, but as library routines are often opaque to the user, a new user
              won't necessarily know that. From their point of view, some library
              routine *cough* strdup() *cough* could return allocated memory, they
              will need to check.





              Brian

              Comment

              • Default User

                #8
                Re: [Noob]String memory allocation

                DJP wrote:
                (mostly back to on-topic) In either case, the first one is in a
                static nonwritable area of storage. Outside a function, the latter
                is in static writable storage, but inside a function, it is in
                "automatic" storage (that is, what you might call the stack)
                >
                Thanks for your response. Also, can you elaborate a little bit about
                the "static nonwritable area of storage" and the "static writable
                storage" that you mention? Thanks.
                Besides the information you received elsewhere, you may want to review
                the FAQ on the subject:


                <http://c-faq.com/decl/strlitinit.html >


                In fact, reviewing all the string, array, and pointer entries would be
                a good idea.





                Brian

                Comment

                Working...