C beginer

C

CBegin

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

Marc Boyer

Le 08-11-2005 said:
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
 
C

CBegin

Marc said:
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?
 
M

Marc Boyer

Le 08-11-2005 said:
Marc said:
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
 
R

Richard Bos

CBegin said:
Marc said:
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
 
S

Steffen Fiksdal

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
 
M

Marc Boyer

Le 08-11-2005 said:
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



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
 
C

CBegin

Richard said:
CBegin said:
Marc said:
Le 08-11-2005, CBegin <[email protected]> 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.....
 
M

Marc Boyer

Le 08-11-2005 said:
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
 
P

pete

CBegin said:
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.
 
P

pete

Marc said:
Le 08-11-2005, Steffen Fiksdal <steffen

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

Marc Boyer

Le 08-11-2005 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top