C beginer

Discussion in 'C Programming' started by CBegin, Nov 8, 2005.

  1. CBegin

    CBegin Guest

    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.
     
    CBegin, Nov 8, 2005
    #1
    1. Advertising

  2. CBegin

    Marc Boyer Guest

    Le 08-11-2005, CBegin <> 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".

    > When i print this ptr thru printf , it prints the string properly.
    > But when i delete this ptr using delete ptr, it throws assertion.


    Notice that delete is a C++ keyword, not C one.
    But the error would be quite the same with free.

    > following are my questions:
    > 1) When i do char *ptr = "TestString"; does it allocate sufficient
    > memory?


    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).

    > 2) How did the printf accessed the value properly? did the above
    > assignment copy the values in heap?


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

    Marc Boyer
     
    Marc Boyer, Nov 8, 2005
    #2
    1. Advertising

  3. CBegin

    CBegin Guest

    Marc Boyer wrote:

    > Le 08-11-2005, CBegin <> 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".

    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?
    >
    > > When i print this ptr thru printf , it prints the string properly.
    > > But when i delete this ptr using delete ptr, it throws assertion.

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

    >
    > 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).
    >
    > > 2) How did the printf accessed the value properly? did the above
    > > assignment copy the values in heap?

    >
    > printf get a pointer on the array "TestString". So, it could
    > access it. The array is not copied.
    >
    > Marc Boyer
     
    CBegin, Nov 8, 2005
    #3
  4. CBegin

    Marc Boyer Guest

    Le 08-11-2005, CBegin <> a écrit :
    > Marc Boyer wrote:
    >> Le 08-11-2005, CBegin <> 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".

    > 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


    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').

    > //
    > 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?


    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
     
    Marc Boyer, Nov 8, 2005
    #4
  5. CBegin

    Richard Bos Guest

    "CBegin" <> wrote:

    > Marc Boyer wrote:
    >
    > > Le 08-11-2005, CBegin <> 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".

    > 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,


    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.

    > which is not stored in heap, but on stack.


    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
     
    Richard Bos, Nov 8, 2005
    #5

  6. > Le 08-11-2005, CBegin <> 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.

    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
     
    Steffen Fiksdal, Nov 8, 2005
    #6
  7. CBegin

    Marc Boyer Guest

    Le 08-11-2005, Steffen Fiksdal <> a écrit :
    > 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=:29807
    > Content-Type: TEXT/PLAIN; charset=iso-8859-1; format=flowed
    > Content-Transfer-Encoding: 8BIT
    >
    >
    >> Le 08-11-2005, CBegin <> 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.


    Yes, but I was trying to give not too much details. But you are
    right.

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


    Yes, and is not read-ondly.

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


    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
     
    Marc Boyer, Nov 8, 2005
    #7
  8. CBegin

    CBegin Guest

    Richard Bos wrote:

    > "CBegin" <> wrote:
    >
    > > Marc Boyer wrote:
    > >
    > > > Le 08-11-2005, CBegin <> 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".

    > > 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,

    >
    > 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.
    >
    > > which is not stored in heap, but on stack.

    >
    > 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.

    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.....


    > 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
     
    CBegin, Nov 8, 2005
    #8
  9. CBegin

    Marc Boyer Guest

    Le 08-11-2005, CBegin <> a écrit :
    > Richard Bos wrote:
    >> "CBegin" <> wrote:
    >> > Marc Boyer wrote:

    >> arranged as one.

    > 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.


    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
     
    Marc Boyer, Nov 8, 2005
    #9
  10. CBegin

    pete Guest

    CBegin wrote:

    > temp static


    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
     
    pete, Nov 8, 2005
    #10
  11. CBegin

    pete Guest

    Marc Boyer wrote:
    >
    > Le 08-11-2005, Steffen Fiksdal <steffen


    > >> Le 08-11-2005, CBegin <> 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.

    >
    > Yes, but I was trying to give not too much details.


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

    --
    pete
     
    pete, Nov 8, 2005
    #11
  12. CBegin

    Marc Boyer Guest

    Le 08-11-2005, pete <> a écrit :
    > Marc Boyer wrote:
    >> Le 08-11-2005, Steffen Fiksdal <steffen
    >> >> Le 08-11-2005, CBegin <> 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.

    >>
    >> Yes, but I was trying to give not too much details.

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


    Yes.
    Each try is not a success ;-)

    Marc Boyer
     
    Marc Boyer, Nov 8, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. My and I

    Beginer need helppp!!

    My and I, Aug 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    489
    clintonG
    Aug 24, 2004
  2. Lloyd Dupont

    dummy beginer's question

    Lloyd Dupont, Nov 20, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    446
    clintonG
    Nov 21, 2005
  3. ArMa

    Beginer question

    ArMa, Apr 13, 2005, in forum: Java
    Replies:
    0
    Views:
    484
  4. Darko Lapanje

    beginer

    Darko Lapanje, Sep 9, 2003, in forum: C++
    Replies:
    3
    Views:
    450
    Stuart Golodetz
    Sep 9, 2003
  5. vee_kay
    Replies:
    7
    Views:
    395
Loading...

Share This Page