Re: I'm going insane
- From: jt@xxxxxxxxxxx (Jens Thoms Toerring)
- Date: 13 Jan 2007 12:51:26 GMT
xvart <xvart@xxxxxxxxxxxxx> wrote:
Code is changed to the following and now works with "gcc version 4.1.1
20060525", the question is are the pages locked even when "l" is
defined as static and should then be on the heap. I thought static
meant on the heap, if so what is the difference between a static heap
allocation and one created with strdup or malloc? should I really ask
the gcc boys?
A line like
char *l = "hello;world;this;is;a;test;";
creates a pointer, pointing to a so-called "string literal".
And string literals can't be changed, neither in C or C++.
It doesn't matter where the compiler puts them, all you
need to know is that trying to modify a string literal is
forbidden - it can be in write protected memory. Putting
a 'static' in front of the pointer won't change anything
about it, that only changes some properties of the pointer
variable, not what it's pointing to. What you could put in
front of it instead is 'const', that would enable the com-
piler to complain if you try to change what 'l' is pointing
to. BTW, some implementations don't enforce that literal
strings can't be changed, so people get away with it and
start believing that it would be ok. But they are in for
a bad surprise if they ever try to compile their programs
with a different compiler or on a different system...
On the other hand, with
char *l = sstrdup( "hello;world;this;is;a;test;" );
you get a pointer to a copy of the literal string, where
the copy is now in memory you own (it has freshly been
allocated for you) and you can change that to your hearts
desires, that's why your new version works.
defined as static and should then be on the heap. I thought static
meant on the heap, if so what is the difference between a static heap
allocation and one created with strdup or malloc? should I really ask
You're looking at this the wrong way round. There are
certain rules the language imposes and putting some-
thing on the heap or somewhere else may be a way to
implement these rules the compiler writers chose. But
by using such arguments you are trying to deduce the
rules of the language from the way you imagine it's
implemented in a certain case, and that probably won't
get you very far (even if your assumption should be
correct).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.
- Follow-Ups:
- Re: I'm going insane
- From: matevzb
- Re: I'm going insane
- From: Stephane CHAZELAS
- Re: I'm going insane
- References:
- I'm going insane
- From: xvart
- Re: I'm going insane
- From: Nikos Chantziaras
- Re: I'm going insane
- From: xvart
- I'm going insane
- Prev by Date: Re: I'm going insane
- Next by Date: Re: I'm going insane
- Previous by thread: Re: I'm going insane
- Next by thread: Re: I'm going insane
- Index(es):
Relevant Pages
|