 |
|
 |
|
01-13-07 12:20 PM
WTF am I missing this just segfaults at the *pch=0; line, am I going
insane or what???
#include <iostream>
using namespace std;
#include <string.h>
int main()
{
char *sptr;
char *pch;
char *l="hello;world;this;is;a;test;";
cout<<"Starting"<<endl;
for(sptr=l,pch=l;(strchr(pch, ';')?pch = strchr(pch, ';'):pch =
strchr(pch, '\r')) > 0;sptr=++pch) {
*pch=0;
cout<<sptr<<endl;
}
cout<<"Ending"<<endl;
return 1;
}
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
01-13-07 12:20 PM
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?
int main()
{
char *sptr;
char *pch;
char *l=strdup("hello;world;this;is;a;test;");
sptr=l;
pch=l;
while ( strchr(pch, ';')?(pch=strchr(pch, ';')) pch=strchr(pch,
'\r')) ) {
*pch=0;
printf("%s\n",sptr);
sptr=++pch;
}
free(l);
return 1;
}
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
01-13-07 12:20 PM
On Jan 13, 10:13 am, "xvart" <x...@languid.me.uk> 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?
You might want to read comp.lang.c faq 1.32
Or as Nikos pointed it out, use std::string if you're into C++.
> should I really ask the gcc boys?
Probably not.
--
Pierre
[ Post a follow-up to this message ]
|
|
|
 |
|
|
01-13-07 12:20 PM
Hi,
xvart wrote:
> char *l=strdup("hello;world;this;is;a;test;");
> free(l);
You've not allocated a memory block to l, so using free is going to cause
undefined behaviour.
TTFN
Paul
--
Sie können mich aufreizen und wirklich heiß machen!
[ Post a follow-up to this message ]
|
|
|
 |
|
|
01-13-07 12:20 PM
"Paul F. Johnson" <paul@all-the-johnsons.co.uk> writes:
> Hi,
>
> xvart wrote:
>
>
> You've not allocated a memory block to l, so using free is going to cause
> undefined behaviour.
strdup() allocates memory with malloc(), so free() is perfectly in
order there.
--
Måns Rullgård
mru@inprovide.com
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
01-13-07 12:20 PM
"xvart" <xvart@languid.me.uk> writes:
> WTF am I missing this just segfaults at the *pch=0; line, am I going
> insane or what???
>
> #include <iostream>
> using namespace std;
> #include <string.h>
> int main()
> {
> char *sptr;
> char *pch;
> char *l="hello;world;this;is;a;test;";
> cout<<"Starting"<<endl;
> for(sptr=l,pch=l;(strchr(pch, ';')?pch = strchr(pch, ';'):pch =
> strchr(pch, '\r')) > 0;sptr=++pch) {
> *pch=0;
> cout<<sptr<<endl;
> }
> cout<<"Ending"<<endl;
> return 1;
> }
String constants are stored in the read-only data section, as
modifying them rarely is what you want.
--
Måns Rullgård
mru@inprovide.com
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
01-13-07 12:20 PM
xvart <xvart@languid.me.uk> 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@toerring.de
\__________________________ http://toerring.de
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
01-13-07 06:15 PM
Thanks all, found the part of the spec sec 6.1.4 that covers string
literals.. yep means string const.
Stephane CHAZELAS wrote:
> 2007-01-13, 14:16(+00), Paul F. Johnson:
ause[vbcol=seagreen]
ring[vbcol=seagreen]
hing,[vbcol=seagreen]
>
> ???
>
> There, malloc allocated 300 bytes and returns a pointer to
> that which is stored into the m variable. Then you override the
> value of m with the pointer returned by strdup. So the original
> value returned by malloc is now lost, so you'll have no way to
> free it.
>
o be[vbcol=seagreen]
> [...]
>
> No, strdup does allocate enough space to hold a copy of the
> input string, copies the input string there an returns a pointer
> to the new allocated space.
>
> You can then use free(3) on the value returned by strdup.
>=20
> --=20
> St=E9phane
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
01-13-07 06:15 PM
On Jan 13, 1:51 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
> 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.
No, it isn't forbidden. In C this falls under "undefined behaviour", so
a compiler could put string literals in read-only or in read-write
memory. Usually there's a compiler option to control this, like
-fwritable-strings for gcc.
--
WYCIWYG
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 04:28 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|