char*

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

    #1

    char*

    This defines a local variable placed on the stack which will be
    cleaned up when it goes out of scope, right?
    {
    char czText[] = "Hello world";
    // code

    } // out of scope

    What about this definition?
    {
    const char * czText2 = "Hello world";
    // code

    }
    Do I need to clean up this variable by using free() ? Thought I read
    somewhere that free should only be used on variables that have been
    malloc'ed which isnt the case here.

    Appreciate any input.

  • Richard Bos

    #2
    Re: char*

    RedLars <Liverpool1892@ gmail.comwrote:
    This defines a local variable placed on the stack which will be
    cleaned up when it goes out of scope, right?
    You don't know that. All you know is that it _can_ be cleaned up when it
    goes out of scope, not that it is.
    {
    char czText[] = "Hello world";
    // code
    >
    } // out of scope
    >
    What about this definition?
    {
    const char * czText2 = "Hello world";
    // code
    >
    }
    Subtle difference. cz[urgh]Text2 itself goes out of scope and may, not
    must, be cleaned up or removed. The string it points at is a string
    literal, which has no scope, but which does have static duration. This
    means that all pointers you made point anywhere within that string
    literal remain valid as long as _they_ are in scope (and still point
    within that string literal, of course).
    Do I need to clean up this variable by using free() ?
    Definitely not.
    Thought I read somewhere that free should only be used on variables that
    have been malloc'ed which isnt the case here.
    That is correct. It should also be used on calloc()ed and realloc()ed
    areas of memory. (If you use realloc(), take care to free() that memory
    only once!)

    Richard

    Comment

    • RedLars

      #3
      Re: char*

      Thanks for the reply

      On 27 Sep, 08:34, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
      RedLars <Liverpool1...@ gmail.comwrote:
      This defines a local variable placed on the stack which will be
      cleaned up when it goes out of scope, right?
      >
      You don't know that. All you know is that it _can_ be cleaned up when it
      goes out of scope, not that it is.
      Given the method below, say for some strange reason this was called
      every minute within an application, would this cause a memory leak?
      void foo()
      {
      int x = 2;
      char y[] = "hello world";
      char * z = "Bye bye";
      }
      {
      char czText[] = "Hello world";
      // code
      >
      } // out of scope
      >
      What about this definition?
      {
      const char * czText2 = "Hello world";
      // code
      >
      }
      >
      Subtle difference. cz[urgh]Text2 itself goes out of scope and may, not
      must, be cleaned up or removed. The string it points at is a string
      literal, which has no scope, but which does have static duration. This
      means that all pointers you made point anywhere within that string
      literal remain valid as long as _they_ are in scope (and still point
      within that string literal, of course).
      So the string literal "Hello world" would continue to live throughout
      the lifetime of the application then? Shouldn't I do something about
      that? What can I do about it?
      Do I need to clean up this variable by using free() ?
      >
      Definitely not.
      >
      Thought I read somewhere that free should only be used on variables that
      have been malloc'ed which isnt the case here.
      >
      That is correct. It should also be used on calloc()ed and realloc()ed
      areas of memory. (If you use realloc(), take care to free() that memory
      only once!)
      >
      Richard

      Comment

      • Joachim Schmitz

        #4
        Re: char*

        "RedLars" <Liverpool1892@ gmail.comschrie b im Newsbeitrag
        news:1190877122 .177714.313450@ 22g2000hsm.goog legroups.com...
        Thanks for the reply
        >
        On 27 Sep, 08:34, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
        >RedLars <Liverpool1...@ gmail.comwrote:
        This defines a local variable placed on the stack which will be
        cleaned up when it goes out of scope, right?
        >>
        >You don't know that. All you know is that it _can_ be cleaned up when it
        >goes out of scope, not that it is.
        >
        Given the method below, say for some strange reason this was called
        every minute within an application, would this cause a memory leak?
        void foo()
        {
        int x = 2;
        char y[] = "hello world";
        char * z = "Bye bye";
        }
        No, it won't leak memory
        {
        char czText[] = "Hello world";
        // code
        >>
        } // out of scope
        >>
        What about this definition?
        {
        const char * czText2 = "Hello world";
        // code
        >>
        }
        >>
        >Subtle difference. cz[urgh]Text2 itself goes out of scope and may, not
        >must, be cleaned up or removed. The string it points at is a string
        >literal, which has no scope, but which does have static duration. This
        >means that all pointers you made point anywhere within that string
        >literal remain valid as long as _they_ are in scope (and still point
        >within that string literal, of course).
        >
        So the string literal "Hello world" would continue to live throughout
        the lifetime of the application then?
        Yes
        Shouldn't I do something about that?
        No need
        What can I do about it?
        you could use:
        const char czText2[] = "Hello world";
        But it won't buy you much.

        Bye, Jojo


        Comment

        • RedLars

          #5
          Re: char*

          On 27 Sep, 10:04, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
          wrote:
          "RedLars" <Liverpool1...@ gmail.comschrie b im Newsbeitragnews :1190877122.177 714.313450@22g2 000hsm.googlegr oups.com...
          >
          >
          >
          Thanks for the reply
          >
          On 27 Sep, 08:34, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
          RedLars <Liverpool1...@ gmail.comwrote:
          This defines a local variable placed on the stack which will be
          cleaned up when it goes out of scope, right?
          >
          You don't know that. All you know is that it _can_ be cleaned up when it
          goes out of scope, not that it is.
          >
          Given the method below, say for some strange reason this was called
          every minute within an application, would this cause a memory leak?
          void foo()
          {
          int x = 2;
          char y[] = "hello world";
          char * z = "Bye bye";
          }
          >
          No, it won't leak memory
          Wouldn't running foo() multiple time create multipe string literal
          "Bye bye" that are in fact not reference by any code, hence not used.
          So the literal is of no use once the method is finsihed yet continue
          to exist. So running foo() 1000 time would create 1000 string literal
          in memory. This seem sort of pointless.

          Sorry about the silly example I'm using atm.


          Comment

          • Joachim Schmitz

            #6
            Re: char*


            "RedLars" <Liverpool1892@ gmail.comschrie b im Newsbeitrag
            news:1190893329 .341315.175760@ d55g2000hsg.goo glegroups.com.. .
            On 27 Sep, 10:04, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
            wrote:
            >"RedLars" <Liverpool1...@ gmail.comschrie b im
            >Newsbeitragnew s:1190877122.17 7714.313450@22g 2000hsm.googleg roups.com...
            >>
            >>
            >>
            Thanks for the reply
            >>
            On 27 Sep, 08:34, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
            >RedLars <Liverpool1...@ gmail.comwrote:
            This defines a local variable placed on the stack which will be
            cleaned up when it goes out of scope, right?
            >>
            >You don't know that. All you know is that it _can_ be cleaned up when
            >it
            >goes out of scope, not that it is.
            >>
            Given the method below, say for some strange reason this was called
            every minute within an application, would this cause a memory leak?
            void foo()
            {
            int x = 2;
            char y[] = "hello world";
            char * z = "Bye bye";
            }
            >>
            >No, it won't leak memory
            >
            Wouldn't running foo() multiple time create multipe string literal
            "Bye bye" that are in fact not reference by any code, hence not used.
            No. Only once.
            So the literal is of no use once the method is finsihed yet continue
            to exist. So running foo() 1000 time would create 1000 string literal
            in memory.
            No, only once...

            Bye, Jojo
            Sorry about the silly example I'm using atm.
            >
            >

            Comment

            • RedLars

              #7
              Re: char*

              On 27 Sep, 13:45, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
              wrote:
              "RedLars" <Liverpool1...@ gmail.comschrie b im Newsbeitragnews :1190893329.341 315.175760@d55g 2000hsg.googleg roups.com...
              >
              >
              >
              On 27 Sep, 10:04, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
              wrote:
              "RedLars" <Liverpool1...@ gmail.comschrie b im
              Newsbeitragnews :1190877122.177 714.313450@22g2 000hsm.googlegr oups.com...
              >
              Thanks for the reply
              >
              On 27 Sep, 08:34, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
              RedLars <Liverpool1...@ gmail.comwrote:
              This defines a local variable placed on the stack which will be
              cleaned up when it goes out of scope, right?
              >
              You don't know that. All you know is that it _can_ be cleaned up when
              it
              goes out of scope, not that it is.
              >
              Given the method below, say for some strange reason this was called
              every minute within an application, would this cause a memory leak?
              void foo()
              {
              int x = 2;
              char y[] = "hello world";
              char * z = "Bye bye";
              }
              >
              No, it won't leak memory
              >
              Wouldn't running foo() multiple time create multipe string literal
              "Bye bye" that are in fact not reference by any code, hence not used.
              >
              No. Only once.
              >
              So the literal is of no use once the method is finsihed yet continue
              to exist. So running foo() 1000 time would create 1000 string literal
              in memory.
              >
              No, only once...
              So the second time foo() is called the string literal constructed
              during the first iteration is re-used? How are string literal stored
              and how is this managed? I mean, does the runtime library loop through
              some static string literal array when it sees:
              const char * x = <text>
              to see if it can find <textalready defined?

              Thanks for the help.


              Comment

              • Richard Bos

                #8
                Re: char*

                RedLars <Liverpool1892@ gmail.comwrote:
                On 27 Sep, 13:45, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
                "RedLars" <Liverpool1...@ gmail.comschrie b im
                On 27 Sep, 10:04, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
                >"RedLars" <Liverpool1...@ gmail.comschrie b im
                Given the method below, say for some strange reason this was called
                every minute within an application, would this cause a memory leak?
                void foo()
                {
                int x = 2;
                char y[] = "hello world";
                char * z = "Bye bye";
                }
                >No, it won't leak memory
                Wouldn't running foo() multiple time create multipe string literal
                "Bye bye" that are in fact not reference by any code, hence not used.
                No. Only once.
                >
                So the second time foo() is called the string literal constructed
                during the first iteration is re-used?
                No. There is only one string literal object per source code string
                literal. It is created, like all static scoped objects, when the program
                starts, and destroyed only when it exits. The only thing that happens
                when you reach the above code is that x, y and z are created; the value
                2 is assigned to x; the contents of the pre-existing "hello world"
                object are copied to y; z is pointed at the start of the pre-existing
                "Bye bye" object; and at the end of the block, x, y, and z are
                destroyed, and both string objects are left in existence so they can be
                re-used next time.

                Richard

                Comment

                • Barry Schwarz

                  #9
                  Re: char*

                  On Thu, 27 Sep 2007 04:42:09 -0700, RedLars <Liverpool1892@ gmail.com>
                  wrote:
                  >On 27 Sep, 10:04, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
                  >wrote:
                  >"RedLars" <Liverpool1...@ gmail.comschrie b im Newsbeitragnews :1190877122.177 714.313450@22g2 000hsm.googlegr oups.com...
                  >>
                  >>
                  >>
                  Thanks for the reply
                  >>
                  On 27 Sep, 08:34, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                  >RedLars <Liverpool1...@ gmail.comwrote:
                  This defines a local variable placed on the stack which will be
                  cleaned up when it goes out of scope, right?
                  >>
                  >You don't know that. All you know is that it _can_ be cleaned up when it
                  >goes out of scope, not that it is.
                  >>
                  Given the method below, say for some strange reason this was called
                  every minute within an application, would this cause a memory leak?
                  void foo()
                  {
                  int x = 2;
                  char y[] = "hello world";
                  char * z = "Bye bye";
                  }
                  >>
                  >No, it won't leak memory
                  >
                  >Wouldn't running foo() multiple time create multipe string literal
                  >"Bye bye" that are in fact not reference by any code, hence not used.
                  No. String literals have static duration. This means that they are
                  created when your program loads (in most systems I would expect they
                  are created at compile time, not execution time, but that is an
                  implementation detail), not when the function in invoked. They remain
                  until your program terminates. On the other hand, z is created each
                  time the function is invoked (just like x and y) but it will always
                  point to the same place for any given execution of your program.
                  >So the literal is of no use once the method is finsihed yet continue
                  >to exist. So running foo() 1000 time would create 1000 string literal
                  >in memory. This seem sort of pointless.
                  No, only one copy of the literal but possibly a thousand pointers to
                  it.


                  Remove del for email

                  Comment

                  • RedLars

                    #10
                    Re: char*

                    On 28 Sep, 03:41, Barry Schwarz <schwa...@doezl .netwrote:
                    On Thu, 27 Sep 2007 04:42:09 -0700, RedLars <Liverpool1...@ gmail.com>
                    wrote:
                    >
                    >
                    >
                    >
                    >
                    On 27 Sep, 10:04, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
                    wrote:
                    "RedLars" <Liverpool1...@ gmail.comschrie b im Newsbeitragnews :1190877122.177 714.313450@22g2 000hsm.googlegr oups.com...
                    >
                    Thanks for the reply
                    >
                    On 27 Sep, 08:34, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                    RedLars <Liverpool1...@ gmail.comwrote:
                    This defines a local variable placed on the stack which will be
                    cleaned up when it goes out of scope, right?
                    >
                    You don't know that. All you know is that it _can_ be cleaned up when it
                    goes out of scope, not that it is.
                    >
                    Given the method below, say for some strange reason this was called
                    every minute within an application, would this cause a memory leak?
                    void foo()
                    {
                    int x = 2;
                    char y[] = "hello world";
                    char * z = "Bye bye";
                    }
                    >
                    No, it won't leak memory
                    >
                    Wouldn't running foo() multiple time create multipe string literal
                    "Bye bye" that are in fact not reference by any code, hence not used.
                    >
                    No. String literals have static duration. This means that they are
                    created when your program loads (in most systems I would expect they
                    are created at compile time, not execution time, but that is an
                    implementation detail), not when the function in invoked. They remain
                    until your program terminates. On the other hand, z is created each
                    time the function is invoked (just like x and y) but it will always
                    point to the same place for any given execution of your program.
                    >
                    So the literal is of no use once the method is finsihed yet continue
                    to exist. So running foo() 1000 time would create 1000 string literal
                    in memory. This seem sort of pointless.
                    >
                    No, only one copy of the literal but possibly a thousand pointers to
                    it.
                    >
                    Remove del for email- Skjul sitert tekst -
                    >
                    - Vis sitert tekst -
                    Thank's for explaining.

                    Comment

                    Working...