C beginer

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

    #1

    C beginer

    I am trying out a few things, need ur help to clear a doubt:
    I have a character pointer initialised as below:
    char *ptr = "TestString ";
    When i print this ptr thru printf , it prints the string properly.
    But when i delete this ptr using delete ptr, it throws assertion.
    following are my questions:
    1) When i do char *ptr = "TestString "; does it allocate sufficient
    memory?
    2) How did the printf accessed the value properly? did the above
    assignment copy the values in heap?

    Thanks for any light thrown into this.

  • Marc Boyer

    #2
    Re: C beginer

    Le 08-11-2005, CBegin <standard.r@gma il.com> a écrit :[color=blue]
    > I am trying out a few things, need ur help to clear a doubt:
    > I have a character pointer initialised as below:
    > char *ptr = "TestString ";[/color]

    ptr is a pointer that point on the automatic read-only
    char array "TestString ".
    [color=blue]
    > When i print this ptr thru printf , it prints the string properly.
    > But when i delete this ptr using delete ptr, it throws assertion.[/color]

    Notice that delete is a C++ keyword, not C one.
    But the error would be quite the same with free.
    [color=blue]
    > following are my questions:
    > 1) When i do char *ptr = "TestString "; does it allocate sufficient
    > memory?[/color]

    Yes. But allocation is done in the memory of automatic variables,
    and free must be called only on dynamic memory (obtained with
    malloc, calloc, realloc).
    [color=blue]
    > 2) How did the printf accessed the value properly? did the above
    > assignment copy the values in heap?[/color]

    printf get a pointer on the array "TestString ". So, it could
    access it. The array is not copied.

    Marc Boyer

    Comment

    • CBegin

      #3
      Re: C beginer


      Marc Boyer wrote:
      [color=blue]
      > Le 08-11-2005, CBegin <standard.r@gma il.com> a écrit :[color=green]
      > > I am trying out a few things, need ur help to clear a doubt:
      > > I have a character pointer initialised as below:
      > > char *ptr = "TestString ";[/color]
      >
      > ptr is a pointer that point on the automatic read-only
      > char array "TestString ".[/color]
      Thanks a lot,

      Is this whats happening internally:
      char *ptr = "TestString ";
      gets expanded to:
      const char temp[] = "TestString ";
      ptr = temp; // ptr holds the address of read only buffer
      //
      so when i try free p, it actually tries to clean the memory assigned to
      temp variable, which is not stored in heap, but on stack.

      Am i correct?[color=blue]
      >[color=green]
      > > When i print this ptr thru printf , it prints the string properly.
      > > But when i delete this ptr using delete ptr, it throws assertion.[/color]
      >
      > Notice that delete is a C++ keyword, not C one.
      > But the error would be quite the same with free.
      >[color=green]
      > > following are my questions:
      > > 1) When i do char *ptr = "TestString "; does it allocate sufficient
      > > memory?[/color]
      >
      > Yes. But allocation is done in the memory of automatic variables,
      > and free must be called only on dynamic memory (obtained with
      > malloc, calloc, realloc).
      >[color=green]
      > > 2) How did the printf accessed the value properly? did the above
      > > assignment copy the values in heap?[/color]
      >
      > printf get a pointer on the array "TestString ". So, it could
      > access it. The array is not copied.
      >
      > Marc Boyer[/color]

      Comment

      • Marc Boyer

        #4
        Re: C beginer

        Le 08-11-2005, CBegin <standard.r@gma il.com> a écrit :[color=blue]
        > Marc Boyer wrote:[color=green]
        >> Le 08-11-2005, CBegin <standard.r@gma il.com> a écrit :[color=darkred]
        >> > I am trying out a few things, need ur help to clear a doubt:
        >> > I have a character pointer initialised as below:
        >> > char *ptr = "TestString ";[/color]
        >>
        >> ptr is a pointer that point on the automatic read-only
        >> char array "TestString ".[/color]
        > Thanks a lot,
        >
        > Is this whats happening internally:
        > char *ptr = "TestString ";
        > gets expanded to:
        > const char temp[] = "TestString ";
        > ptr = temp; // ptr holds the address of read only buffer[/color]

        You are right (except perhaps the 'const': this is the right
        idea, but I am not sure this is exactly the same, this is
        why I have use 'read-only' instead of 'const').
        [color=blue]
        > //
        > so when i try free p, it actually tries to clean the memory assigned to
        > temp variable, which is not stored in heap, but on stack.
        >
        > Am i correct?[/color]

        Yes, except that, to my knowledge, there is no stack in the C standart.
        But, there is one in a lot of implementations .

        Marc Boyer

        Comment

        • Richard Bos

          #5
          Re: C beginer

          "CBegin" <standard.r@gma il.com> wrote:
          [color=blue]
          > Marc Boyer wrote:
          >[color=green]
          > > Le 08-11-2005, CBegin <standard.r@gma il.com> a =E9crit :[color=darkred]
          > > > I am trying out a few things, need ur help to clear a doubt:
          > > > I have a character pointer initialised as below:
          > > > char *ptr = "TestString ";[/color]
          > >
          > > ptr is a pointer that point on the automatic read-only
          > > char array "TestString ".[/color]
          > Thanks a lot,
          >
          > Is this whats happening internally:
          > char *ptr = "TestString ";
          > gets expanded to:
          > const char temp[] = "TestString ";
          > ptr = temp; // ptr holds the address of read only buffer
          > //
          > so when i try free p, it actually tries to clean the memory assigned to
          > temp variable,[/color]

          Almost. String literals have static duration (i.e., they do not get
          deallocated when you leave the block where you "define" them (scare
          quotes around "define" because it's clearly not a real definition as,
          ahem, defined in the C Standard, but the thing comes into existence
          nevertheless and you asked for it to exist)); and string literals are
          not modifiable, but (for reasons of convenience) not acutally const; so
          the definition of temp should actually be

          static char temp[] = "TestString ";

          but you still can't write to it.
          Apart from that, yes, that's more or less what happens behind the
          scenes.
          [color=blue]
          > which is not stored in heap, but on stack.[/color]

          No. First, there is no guarantee that allocated objects are stored in a
          structure called (or even necessarily describable as) "the heap"; and
          there is no guarantee that the area in which automatic objects are
          stored is called "the stack", although it _is_ very unlikely not to be
          arranged as one.
          Second, as I wrote above, string literals have static duration. This is
          a third category, separate from objects with allocated duration ("the
          heap") and objects with automatic duration ("the stack"). It is often
          called something like "global memory".

          Basically, allocated memory (memory you get from malloc() and friends)
          exists from the point at which you ask for it, until the point you
          free() it (or, AFAIK, in C++, delete it). Automatic memory, which is all
          normal objects defined within a function, exists from the start of the
          block it is declared in until the end of that block. Static memory,
          which is all objects declared outside a function as well as all objects
          declared static, _and_ your string literal, exists as long as the
          program runs.
          (C99 adds some subtleties to this, involving things like compound
          literals and variable length arrays, but you need not concern yourself
          with these - yet.)

          The effect on your program, though, is the same. You cannot free()
          static memory any more than you can free() automatic memory. You can
          only call free() on memory you have received from malloc(), calloc() or
          realloc(), and even then only on the base pointer of the block, not on
          any pointer inside it; and only once per memory block.

          Richard

          Comment

          • Steffen Fiksdal

            #6
            Re: C beginer

            [color=blue]
            > Le 08-11-2005, CBegin <standard.r@gma il.com> a écrit :[color=green]
            >> I am trying out a few things, need ur help to clear a doubt:
            >> I have a character pointer initialised as below:
            >> char *ptr = "TestString ";[/color]
            >
            > ptr is a pointer that point on the automatic read-only
            > char array "TestString ".
            >[/color]

            Are you sure? I thought that the "TestString " value will be put in
            static storage even though the static keyword is omitted, so that it will
            remain as long as the program is running.

            char ptr[]= "TestString " is automatic, I believe.

            The reason free() failes, is because the memory pointer received by free
            is not a valid for freeing on the heap ?

            Best Regards
            Steffen

            Comment

            • Marc Boyer

              #7
              Re: C beginer

              Le 08-11-2005, Steffen Fiksdal <steffenf@ulrik .uio.no> a écrit :[color=blue]
              > This message is in MIME format. The first part should be readable text,
              > while the remaining parts are likely unreadable without MIME-aware tools.
              >
              > ---259979764-50747693-1131441565=:298 07
              > Content-Type: TEXT/PLAIN; charset=iso-8859-1; format=flowed
              > Content-Transfer-Encoding: 8BIT
              >
              >[color=green]
              >> Le 08-11-2005, CBegin <standard.r@gma il.com> a écrit :[color=darkred]
              >>> I am trying out a few things, need ur help to clear a doubt:
              >>> I have a character pointer initialised as below:
              >>> char *ptr = "TestString ";[/color]
              >>
              >> ptr is a pointer that point on the automatic read-only
              >> char array "TestString ".[/color]
              >
              > Are you sure? I thought that the "TestString " value will be put in
              > static storage even though the static keyword is omitted, so that it will
              > remain as long as the program is running.[/color]

              Yes, but I was trying to give not too much details. But you are
              right.
              [color=blue]
              > char ptr[]= "TestString " is automatic, I believe.[/color]

              Yes, and is not read-ondly.
              [color=blue]
              > The reason free() failes, is because the memory pointer received by free
              > is not a valid for freeing on the heap ?[/color]

              To be really accurate, there is no "heap" in C. free must
              called on a pointer obtained with malloc/calloc/realloc (and
              perhaps some others...)

              Marc Boyer

              Comment

              • CBegin

                #8
                Re: C beginer


                Richard Bos wrote:
                [color=blue]
                > "CBegin" <standard.r@gma il.com> wrote:
                >[color=green]
                > > Marc Boyer wrote:
                > >[color=darkred]
                > > > Le 08-11-2005, CBegin <standard.r@gma il.com> a =E9crit :
                > > > > I am trying out a few things, need ur help to clear a doubt:
                > > > > I have a character pointer initialised as below:
                > > > > char *ptr = "TestString ";
                > > >
                > > > ptr is a pointer that point on the automatic read-only
                > > > char array "TestString ".[/color]
                > > Thanks a lot,
                > >
                > > Is this whats happening internally:
                > > char *ptr = "TestString ";
                > > gets expanded to:
                > > const char temp[] = "TestString ";
                > > ptr = temp; // ptr holds the address of read only buffer
                > > //
                > > so when i try free p, it actually tries to clean the memory assigned to
                > > temp variable,[/color]
                >
                > Almost. String literals have static duration (i.e., they do not get
                > deallocated when you leave the block where you "define" them (scare
                > quotes around "define" because it's clearly not a real definition as,
                > ahem, defined in the C Standard, but the thing comes into existence
                > nevertheless and you asked for it to exist)); and string literals are
                > not modifiable, but (for reasons of convenience) not acutally const; so
                > the definition of temp should actually be
                >
                > static char temp[] = "TestString ";
                >
                > but you still can't write to it.
                > Apart from that, yes, that's more or less what happens behind the
                > scenes.
                >[color=green]
                > > which is not stored in heap, but on stack.[/color]
                >
                > No. First, there is no guarantee that allocated objects are stored in a
                > structure called (or even necessarily describable as) "the heap"; and
                > there is no guarantee that the area in which automatic objects are
                > stored is called "the stack", although it _is_ very unlikely not to be
                > arranged as one.[/color]
                Much clear now.
                again coming back to this declaration:
                char* p = "TestString "; //
                p does point to some location where this temp static var is stored.
                I am using MSDEV IDE, there in the memory view, i can see that p points
                to
                a location which contains text specified. (This was also evident from
                the printf statement, which executed properly).
                But When I do *(p+1) = 'M'; It should be replacing the existing text in
                address pointed by p+1 -> location of character 'e' . but it again
                gives an exception. If it was a read only buffer it should have given a
                compile time error. Would like to understand more on this temp
                variables usage.....

                [color=blue]
                > Second, as I wrote above, string literals have static duration. This is
                > a third category, separate from objects with allocated duration ("the
                > heap") and objects with automatic duration ("the stack"). It is often
                > called something like "global memory".
                >
                > Basically, allocated memory (memory you get from malloc() and friends)
                > exists from the point at which you ask for it, until the point you
                > free() it (or, AFAIK, in C++, delete it). Automatic memory, which is all
                > normal objects defined within a function, exists from the start of the
                > block it is declared in until the end of that block. Static memory,
                > which is all objects declared outside a function as well as all objects
                > declared static, _and_ your string literal, exists as long as the
                > program runs.
                > (C99 adds some subtleties to this, involving things like compound
                > literals and variable length arrays, but you need not concern yourself
                > with these - yet.)
                >
                > The effect on your program, though, is the same. You cannot free()
                > static memory any more than you can free() automatic memory. You can
                > only call free() on memory you have received from malloc(), calloc() or
                > realloc(), and even then only on the base pointer of the block, not on
                > any pointer inside it; and only once per memory block.
                >
                > Richard[/color]

                Comment

                • Marc Boyer

                  #9
                  Re: C beginer

                  Le 08-11-2005, CBegin <standard.r@gma il.com> a écrit :[color=blue]
                  > Richard Bos wrote:[color=green]
                  >> "CBegin" <standard.r@gma il.com> wrote:[color=darkred]
                  >> > Marc Boyer wrote:[/color]
                  >> arranged as one.[/color]
                  > Much clear now.
                  > again coming back to this declaration:
                  > char* p = "TestString "; //
                  > p does point to some location where this temp static var is stored.
                  > I am using MSDEV IDE, there in the memory view, i can see that p points
                  > to
                  > a location which contains text specified. (This was also evident from
                  > the printf statement, which executed properly).
                  > But When I do *(p+1) = 'M'; It should be replacing the existing text in
                  > address pointed by p+1 -> location of character 'e' . but it again
                  > gives an exception. If it was a read only buffer it should have given a
                  > compile time error.[/color]

                  You make confusion between 'const' and 'read-only'.
                  Violating const gives you a warning. But litteral strings
                  are not const, there are read-only...

                  It is almost like if you where doing:
                  static const char no_name[]= "TestString ";
                  char* p= (char*) no_name;

                  Manipulating "TestString ", through p gives you access to
                  a 'read-only' object, but without the 'const' compilation
                  checking.

                  Marc Boyer

                  Comment

                  • pete

                    #10
                    Re: C beginer

                    CBegin wrote:
                    [color=blue]
                    > temp static[/color]

                    Those two words don't go together well.
                    Objects with static duration are initialized before
                    main starts to execute and they exist until the program ends.

                    --
                    pete

                    Comment

                    • pete

                      #11
                      Re: C beginer

                      Marc Boyer wrote:[color=blue]
                      >
                      > Le 08-11-2005, Steffen Fiksdal <steffen[/color]
                      [color=blue][color=green][color=darkred]
                      > >> Le 08-11-2005, CBegin <standard.r@gma il.com> a écrit :
                      > >>> I am trying out a few things, need ur help to clear a doubt:
                      > >>> I have a character pointer initialised as below:
                      > >>> char *ptr = "TestString ";
                      > >>
                      > >> ptr is a pointer that point on the automatic read-only
                      > >> char array "TestString ".[/color]
                      > >
                      > > Are you sure? I thought that the "TestString " value will be put in
                      > > static storage even though the static keyword is omitted,
                      > > so that it will
                      > > remain as long as the program is running.[/color]
                      >
                      > Yes, but I was trying to give not too much details.[/color]

                      Leaving out the word "automatic" ,
                      would have been a better way to give not too much details.

                      --
                      pete

                      Comment

                      • Marc Boyer

                        #12
                        Re: C beginer

                        Le 08-11-2005, pete <pfiland@mindsp ring.com> a écrit :[color=blue]
                        > Marc Boyer wrote:[color=green]
                        >> Le 08-11-2005, Steffen Fiksdal <steffen[color=darkred]
                        >> >> Le 08-11-2005, CBegin <standard.r@gma il.com> a écrit :
                        >> >>> I am trying out a few things, need ur help to clear a doubt:
                        >> >>> I have a character pointer initialised as below:
                        >> >>> char *ptr = "TestString ";
                        >> >>
                        >> >> ptr is a pointer that point on the automatic read-only
                        >> >> char array "TestString ".
                        >> >
                        >> > Are you sure? I thought that the "TestString " value will be put in
                        >> > static storage even though the static keyword is omitted,
                        >> > so that it will
                        >> > remain as long as the program is running.[/color]
                        >>
                        >> Yes, but I was trying to give not too much details.[/color]
                        >
                        > Leaving out the word "automatic" ,
                        > would have been a better way to give not too much details.[/color]

                        Yes.
                        Each try is not a success ;-)

                        Marc Boyer

                        Comment

                        Working...