PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Seg fault + string manipulation
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Seg fault + string manipulation

Réponse
 
LinkBack Outils de la discussion
Vieux 29/05/2008, 13h54   #1
kumarvis@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Seg fault + string manipulation

int main ()
{
char *str = *Aamit ;
*str='R' ;
printf("%s \n " ,str);
}

it's giving segfault
why ?????
  Réponse avec citation
Vieux 29/05/2008, 14h15   #2
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

kumarvis@gmail.com said:

> int main ()
> {
> char *str = *Aamit ;
> *str='R' ;
> printf("%s \n " ,str);
> }
>
> it's giving segfault
> why ?????


What you have written doesn't compile, so it's hard to see how it could
segfault.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Vieux 29/05/2008, 14h41   #3
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

kumarvis@gmail.com wrote:
> int main ()
> {
> char *str = *Aamit ;
> *str='R' ;
> printf("%s \n " ,str);
> }
>
> it's giving segfault
> why ?????


int main(void)
{
char *str = "Aamit" ;

*str = 'R' ;
puts(str);
return 0;
}

--
pete
  Réponse avec citation
Vieux 29/05/2008, 14h44   #4
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

kumarvis@gmail.com wrote:
> int main ()
> {
> char *str = *Aamit ;
> *str='R' ;
> printf("%s \n " ,str);
> }
>
> it's giving segfault
> why ?????

If you meant to write
char *str = "Aamit";

Then the
*str ="R";
possibly writes into read-only memory, but surely invokes undefined
behavoir.
segvault is one possible outcome of that.

Another fault (and causing undefined behavoir) is the lack of #include
<stdio.h> resp. the resulting lack of a prototype for the varadic function
printf().

Bye, Jojo


  Réponse avec citation
Vieux 29/05/2008, 16h05   #5
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

pete wrote:
> kumarvis@gmail.com wrote:
>> int main ()
>> {
>> char *str = *Aamit ;
>> *str='R' ;
>> printf("%s \n " ,str);
>> }
>>
>> it's giving segfault
>> why ?????

>
> int main(void)
> {
> char *str = "Aamit" ;


Should be:

char str[] = "Aamit" ;

instead.

> *str = 'R' ;
> puts(str);
> return 0;



--
pete
  Réponse avec citation
Vieux 29/05/2008, 16h18   #6
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

pete <pfiland@mindspring.com> writes:
> kumarvis@gmail.com wrote:
>> int main ()
>> {
>> char *str = *Aamit ;
>> *str='R' ;
>> printf("%s \n " ,str);
>> }
>> it's giving segfault
>> why ?????

>
> int main(void)
> {
> char *str = "Aamit" ;
>
> *str = 'R' ;
> puts(str);
> return 0;
> }


pete, what exactly was the point of your followup?

You corrected the OP's compilation error and presumably reproduced
something close to his actual code (which the OP should have posted
himself). Meanwhile, you haven't answered his actual question, or
said anything at all about it. You also forgot the required
"#include <stdio.h>" and replaced the OP's printf call with a puts
call, which subtly changed the behavior of the program.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 29/05/2008, 16h26   #7
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

kumarvis@gmail.com writes:
> int main ()
> {
> char *str = *Aamit ;
> *str='R' ;
> printf("%s \n " ,str);
> }
>
> it's giving segfault


No, it isn't. I'm sure the program you're actually running gives you
a seg fault, but the code you posted won't even compile.

Never re-type code when you post it here. Always copy-and-paste the
*exact* code that you're actually compiling.

You also have a few problems that your compiler might not warn you about:

If you're going to use printf, you *must* have "#include <stdio.h>".

"int main()" is ok, but "int main(void)" is better.

You should add a "return 0;" at the end of main. It's not required in
all circumstances, but it can't hurt.

The space after the "%s" in your printf format is fairly harmless, but
almost certainly useless. It causes an extra blank to be printed
after your string.

The way you format your code is odd and makes it more difficult to
read. The compiler doesn't care about whitespace between tokens, but
for the sake of your readers it's generally best to have a space after
each comma and surrounding binary operators like "=", and *not* to
have a blank preceding a semicolon or comma.

And see question 1.32 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 29/05/2008, 17h18   #8
bert
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

On 29 May, 13:54, kumar...@gmail.com wrote:
> int main ()
> {
> char *str = *Aamit ;
> *str='R' ;
> printf("%s \n " ,str);
>
> }
>
> it's giving segfault
> why ?????


Apart from the problems other posters have
already pointed out, there is a major one
which they haven't :-

printf() expects its (str) argument to
point to a zero-terminated string. Your
code modifies the first character which
str is pointing at, but does nothing about
the following ones. If they are all non-zero
junk, and you have fixed the other problems,
it is quite possible for printf() to run off
the end of available memory looking for the
zero terminator.
--
  Réponse avec citation
Vieux 29/05/2008, 22h53   #9
Chad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

On May 29, 9:18am, bert <bert.hutchi...@btinternet.com> wrote:
> On 29 May, 13:54, kumar...@gmail.com wrote:
>
> > int main ()
> > {
> > char *str = *Aamit ;
> > *str='R' ;
> > printf("%s \n " ,str);

>
> > }

>
> > it's giving segfault
> > why ?????

>
> Apart from the problems other posters have
> already pointed out, there is a major one
> which they haven't :-
>
> printf() expects its (str) argument to
> point to a zero-terminated string. Your
> code modifies the first character which
> str is pointing at, but does nothing about
> the following ones. If they are all non-zero
> junk, and you have fixed the other problems,
> it is quite possible for printf() to run off
> the end of available memory looking for the
> zero terminator.
> --



Maybe I'm not thinking this through enough, but I REALLY don't see the
difference between
m-net% more mod.c
#include <stdio.h>

int main (void)
{
char str[] = "Aamit";
*str='R';
printf("%s \n " ,str);
return 0;
}

m-net% gcc -g -Wall mod.c -o mod
m-net% ./mod
Ramit

%
m-net%


Versus something like
m-net% more mod.c
#include <stdio.h>

int main (void)
{
char str[] = "Aamit";
*str='R';
puts(str);
return 0;
}

m-net% gcc -g -Wall mod.c -o mod
m-net% ./mod
Ramit
m-net%



  Réponse avec citation
Vieux 30/05/2008, 00h31   #10
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

Chad wrote:
>

.... snip ...
>
> Maybe I'm not thinking this through enough, but I REALLY don't
> see the difference between
>

.... snip ...
>
> Versus something like
>
> #include <stdio.h>
> int main (void) {
> char str[] = "Aamit";
> *str='R';
> puts(str);
> return 0;
> }


The difference is between:

char *str = "Aamit"; /* 1 */
and
char str[] = "Aamit"; /* 2 */

in /* 1 */ str is a char pointer to a non-modifiable string. In /*
2 */ str is a fully modifiable 6 char array, holding "Aamit\0".

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
  Réponse avec citation
Vieux 30/05/2008, 01h33   #11
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

Chad wrote:

> Maybe I'm not thinking this through enough, but I REALLY don't see the
> difference between
> m-net% more mod.c
> #include <stdio.h>
>
> int main (void)
> {
> char str[] = "Aamit";
> *str='R';
> printf("%s \n " ,str);
> return 0;
> }
>
> m-net% gcc -g -Wall mod.c -o mod
> m-net% ./mod
> Ramit
>
> %
> m-net%
>
>
> Versus something like
> m-net% more mod.c
> #include <stdio.h>
>
> int main (void)
> {
> char str[] = "Aamit";
> *str='R';
> puts(str);
> return 0;
> }
>
> m-net% gcc -g -Wall mod.c -o mod
> m-net% ./mod
> Ramit
> m-net%


The printf version, outputs a space character (' ') before the newline.

--
pete
  Réponse avec citation
Vieux 30/05/2008, 02h48   #12
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

bert <bert.hutchings@btinternet.com> writes:
> On 29 May, 13:54, kumar...@gmail.com wrote:
>> int main ()
>> {
>> char *str = *Aamit ;
>> *str='R' ;
>> printf("%s \n " ,str);
>>
>> }
>>
>> it's giving segfault
>> why ?????

>
> Apart from the problems other posters have
> already pointed out, there is a major one
> which they haven't :-
>
> printf() expects its (str) argument to
> point to a zero-terminated string. Your
> code modifies the first character which
> str is pointing at, but does nothing about
> the following ones. If they are all non-zero
> junk, and you have fixed the other problems,
> it is quite possible for printf() to run off
> the end of available memory looking for the
> zero terminator.


The code as posted will not compile, so it clearly isn't the same code
that the OP actually compiled and ran. I think you're making a
different assumption than the rest of us about just how the posted
code differs from the real code.

Are you assuming that there's a declared object of type char** named
``Aamit''?

I think the rest of us assumed that ``*Aamit'' should have been the
string literal "Aamit". Given that assumption, *if* we ignore the
fact that modifying the contents of a string literal invokes undefined
behavior, then the array is already properly terminated with a '\0'.

Unfortunately, the original poster has not seen fit to clear this up
by posting his actual code. Until and unless he does so, I suggest
there's no point in pursuing this further.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 30/05/2008, 07h53   #13
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

CBFalconer wrote:
> Chad wrote:
>>

> ... snip ...
>>
>> Maybe I'm not thinking this through enough, but I REALLY don't
>> see the difference between
>>

> ... snip ...
>>
>> Versus something like
>>
>> #include <stdio.h>
>> int main (void) {
>> char str[] = "Aamit";
>> *str='R';
>> puts(str);
>> return 0;
>> }

>
> The difference is between:
>
> char *str = "Aamit"; /* 1 */
> and
> char str[] = "Aamit"; /* 2 */
>
> in /* 1 */ str is a char pointer to a non-modifiable string. In /*
> 2 */ str is a fully modifiable 6 char array, holding "Aamit\0".

Read again. Chad was using char str[] in both cases, his only difference was
printf vs. puts

Bye, Jojo


  Réponse avec citation
Vieux 30/05/2008, 07h55   #14
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

Chad wrote:
> On May 29, 9:18 am, bert <bert.hutchi...@btinternet.com> wrote:
>> On 29 May, 13:54, kumar...@gmail.com wrote:
>>
>>> int main ()
>>> {
>>> char *str = *Aamit ;
>>> *str='R' ;
>>> printf("%s \n " ,str);

>>
>>> }

>>
>>> it's giving segfault
>>> why ?????

>>
>> Apart from the problems other posters have
>> already pointed out, there is a major one
>> which they haven't :-
>>
>> printf() expects its (str) argument to
>> point to a zero-terminated string. Your
>> code modifies the first character which
>> str is pointing at, but does nothing about
>> the following ones. If they are all non-zero
>> junk, and you have fixed the other problems,
>> it is quite possible for printf() to run off
>> the end of available memory looking for the
>> zero terminator.
>> --

>
>
> Maybe I'm not thinking this through enough, but I REALLY don't see the
> difference between

Then look again.

> m-net% more mod.c
> #include <stdio.h>
>
> int main (void)
> {
> char str[] = "Aamit";
> *str='R';
> printf("%s \n " ,str);
> return 0;
> }
>
> m-net% gcc -g -Wall mod.c -o mod
> m-net% ./mod
> Ramit
>

See this linefeed?

> %
> m-net%
>
>
> Versus something like
> m-net% more mod.c
> #include <stdio.h>
>
> int main (void)
> {
> char str[] = "Aamit";
> *str='R';
> puts(str);
> return 0;
> }
>
> m-net% gcc -g -Wall mod.c -o mod
> m-net% ./mod
> Ramit

Here it is not.

> m-net%


On top you got a space before and anotzher one after the newline, but these
are 'invisible'.

Bye, Jojo


  Réponse avec citation
Vieux 30/05/2008, 09h25   #15
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

Joachim Schmitz wrote:
> CBFalconer wrote:
>> Chad wrote:
>>>

>> ... snip ...
>>>
>>> Maybe I'm not thinking this through enough, but I REALLY don't
>>> see the difference between
>>>

>> ... snip ...
>>>
>>> Versus something like
>>>
>>> #include <stdio.h>
>>> int main (void) {
>>> char str[] = "Aamit";
>>> *str='R';
>>> puts(str);
>>> return 0;
>>> }

>>
>> The difference is between:
>>
>> char *str = "Aamit"; /* 1 */
>> and
>> char str[] = "Aamit"; /* 2 */
>>
>> in /* 1 */ str is a char pointer to a non-modifiable string. In /*
>> 2 */ str is a fully modifiable 6 char array, holding "Aamit\0".

>
> Read again. Chad was using char str[] in both cases, his only
> difference was printf vs. puts


That was in his last manual copy of the original problem. The

char* = "constant";

was the actual problem.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
  Réponse avec citation
Vieux 30/05/2008, 10h05   #16
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

CBFalconer wrote:
> Joachim Schmitz wrote:
>> CBFalconer wrote:
>>> Chad wrote:
>>>>
>>> ... snip ...
>>>>
>>>> Maybe I'm not thinking this through enough, but I REALLY don't
>>>> see the difference between
>>>>
>>> ... snip ...
>>>>
>>>> Versus something like
>>>>
>>>> #include <stdio.h>
>>>> int main (void) {
>>>> char str[] = "Aamit";
>>>> *str='R';
>>>> puts(str);
>>>> return 0;
>>>> }
>>>
>>> The difference is between:
>>>
>>> char *str = "Aamit"; /* 1 */
>>> and
>>> char str[] = "Aamit"; /* 2 */
>>>
>>> in /* 1 */ str is a char pointer to a non-modifiable string. In /*
>>> 2 */ str is a fully modifiable 6 char array, holding "Aamit\0".

>>
>> Read again. Chad was using char str[] in both cases, his only
>> difference was printf vs. puts

>
> That was in his last manual copy of the original problem. The
>
> char* = "constant";
>
> was the actual problem.

the *OP's* actual problem, but not Chad's problem.


  Réponse avec citation
Vieux 30/05/2008, 15h46   #17
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

Joachim Schmitz wrote:
> Chad wrote:


>> Maybe I'm not thinking this through enough, but I REALLY don't see the
>> difference between

> Then look again.
>
>> m-net% more mod.c
>> #include <stdio.h>
>>
>> int main (void)
>> {
>> char str[] = "Aamit";
>> *str='R';
>> printf("%s \n " ,str);
>> return 0;
>> }
>>
>> m-net% gcc -g -Wall mod.c -o mod
>> m-net% ./mod
>> Ramit
>>

> See this linefeed?
>
>> %
>> m-net%
>>
>>
>> Versus something like
>> m-net% more mod.c
>> #include <stdio.h>
>>
>> int main (void)
>> {
>> char str[] = "Aamit";
>> *str='R';
>> puts(str);
>> return 0;
>> }
>>
>> m-net% gcc -g -Wall mod.c -o mod
>> m-net% ./mod
>> Ramit

> Here it is not.
>
>> m-net%

>
> On top you got a space before and anotzher one after the newline, but these
> are 'invisible'.


I think the printf version is stylistically
a little bit worse than that.

N869
7.19.2 Streams

[#2]
Data read in from a text
stream will necessarily compare equal to the data that were
earlier written out to that stream only if: the data consist
only of printing characters and the control characters
horizontal tab and new-line; no new-line character is
immediately preceded by space characters; and the last
character is a new-line character.

Whether space characters
that are written out immediately before a new-line character
appear when read in is implementation-defined.


--
pete
  Réponse avec citation
Vieux 30/05/2008, 16h01   #18
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

pete wrote:
> Joachim Schmitz wrote:
>> Chad wrote:

>
>>> Maybe I'm not thinking this through enough, but I REALLY don't see
>>> the difference between

>> Then look again.
>>
>>> m-net% more mod.c
>>> #include <stdio.h>
>>>
>>> int main (void)
>>> {
>>> char str[] = "Aamit";
>>> *str='R';
>>> printf("%s \n " ,str);
>>> return 0;
>>> }
>>>
>>> m-net% gcc -g -Wall mod.c -o mod
>>> m-net% ./mod
>>> Ramit
>>>

>> See this linefeed?
>>
>>> %
>>> m-net%
>>>
>>>
>>> Versus something like
>>> m-net% more mod.c
>>> #include <stdio.h>
>>>
>>> int main (void)
>>> {
>>> char str[] = "Aamit";
>>> *str='R';
>>> puts(str);
>>> return 0;
>>> }
>>>
>>> m-net% gcc -g -Wall mod.c -o mod
>>> m-net% ./mod
>>> Ramit

>> Here it is not.
>>
>>> m-net%

>>
>> On top you got a space before and anotzher one after the newline,
>> but these are 'invisible'.

>
> I think the printf version is stylistically
> a little bit worse than that.
>
> N869
> 7.19.2 Streams
>
> [#2]
> Data read in from a text
> stream will necessarily compare equal to the data that were
> earlier written out to that stream only if: the data consist
> only of printing characters and the control characters
> horizontal tab and new-line; no new-line character is
> immediately preceded by space characters; and the last
> character is a new-line character.
>
> Whether space characters
> that are written out immediately before a new-line character
> appear when read in is implementation-defined.

Guess that reply should have gone to Chad?

Bye, Jojo


  Réponse avec citation
Vieux 30/05/2008, 17h54   #19
Chad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

On May 30, 7:46 am, pete <pfil...@mindspring.com> wrote:
> Joachim Schmitz wrote:
> > Chad wrote:
> >> Maybe I'm not thinking this through enough, but I REALLY don't see the
> >> difference between

> > Then look again.

>
> >> m-net% more mod.c
> >> #include <stdio.h>

>
> >> int main (void)
> >> {
> >> char str[] = "Aamit";
> >> *str='R';
> >> printf("%s \n " ,str);
> >> return 0;
> >> }

>
> >> m-net% gcc -g -Wall mod.c -o mod
> >> m-net% ./mod
> >> Ramit

>
> > See this linefeed?

>
> >> %
> >> m-net%

>
> >> Versus something like
> >> m-net% more mod.c
> >> #include <stdio.h>

>
> >> int main (void)
> >> {
> >> char str[] = "Aamit";
> >> *str='R';
> >> puts(str);
> >> return 0;
> >> }

>
> >> m-net% gcc -g -Wall mod.c -o mod
> >> m-net% ./mod
> >> Ramit

> > Here it is not.

>
> >> m-net%

>
> > On top you got a space before and anotzher one after the newline, but these
> > are 'invisible'.

>
> I think the printf version is stylistically
> a little bit worse than that.
>
> N869
> 7.19.2 Streams
>
> [#2]
> Data read in from a text
> stream will necessarily compare equal to the data that were
> earlier written out to that stream only if: the data consist
> only of printing characters and the control characters
> horizontal tab and new-line; no new-line character is
> immediately preceded by space characters; and the last
> character is a new-line character.
>
> Whether space characters
> that are written out immediately before a new-line character
> appear when read in is implementation-defined.
>
> --
> pete




It appears that I don't get undefined behavior when I remove the space
before and after '\n' in printf()

m-net% more mod.c
#include <stdio.h>

int main (void)
{
char str[] = "Aamit";
*str='R';
printf("%s\n" ,str);
return 0;
}

m-net% gcc -g -Wall mod.c -o mod
m-net% ./mod
Ramit
m-net%
  Réponse avec citation
Vieux 30/05/2008, 19h16   #20
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

Chad <cdalten@gmail.com> writes:
[...]
> It appears that I don't get undefined behavior when I remove the space
> before and after '\n' in printf()
>
> m-net% more mod.c
> #include <stdio.h>
>
> int main (void)
> {
> char str[] = "Aamit";
> *str='R';
> printf("%s\n" ,str);
> return 0;
> }

[...]

The space before or after '\n' has nothing to do with whether the
behavior is undefined.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 31/05/2008, 05h04   #21
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

Chad wrote:
> On May 30, 7:46 am, pete <pfil...@mindspring.com> wrote:
>> Joachim Schmitz wrote:
>>> Chad wrote:


>>>> #include <stdio.h>
>>>> int main (void)
>>>> {
>>>> char str[] = "Aamit";
>>>> *str='R';
>>>> printf("%s \n " ,str);
>>>> return 0;
>>>> }


>>> On top you got a space before and anotzher one after the newline, but these
>>> are 'invisible'.

>> I think the printf version is stylistically
>> a little bit worse than that.
>>
>> N869
>> 7.19.2 Streams
>>
>> [#2]
>> Data read in from a text
>> stream will necessarily compare equal to the data that were
>> earlier written out to that stream only if: the data consist
>> only of printing characters and the control characters
>> horizontal tab and new-line; no new-line character is
>> immediately preceded by space characters; and the last
>> character is a new-line character.
>>
>> Whether space characters
>> that are written out immediately before a new-line character
>> appear when read in is implementation-defined.


> It appears that I don't get undefined behavior when I remove the space
> before and after '\n' in printf()


Yes.

> #include <stdio.h>
>
> int main (void)
> {
> char str[] = "Aamit";
> *str='R';
> printf("%s\n" ,str);
> return 0;
> }


That is an example of a "correct program" [#3]
and also of a "strictly conforming program". [#5]

N869
4. Conformance

[#1] In this International Standard, ``shall'' is to be
interpreted as a requirement on an implementation or on a
program; conversely, ``shall not'' is to be interpreted as a
prohibition.

[#2] If a ``shall'' or ``shall not'' requirement that
appears outside of a constraint is violated, the behavior is
undefined. Undefined behavior is otherwise indicated in
this International Standard by the words ``undefined
behavior'' or by the omission of any explicit definition of
behavior. There is no difference in emphasis among these
three; they all describe ``behavior that is undefined''.

[#3] A program that is correct in all other aspects,
operating on correct data, containing unspecified behavior
shall be a correct program and act in accordance with
5.1.2.3.

[#5] A strictly conforming program shall use only those
features of the language and library specified in this
International Standard.2) It shall not produce output
dependent on any unspecified, undefined, or implementation-
defined behavior, and shall not exceed any minimum
implementation limit.

--
pete
  Réponse avec citation
Vieux 31/05/2008, 05h15   #22
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Seg fault + string manipulation

Keith Thompson wrote:
> Chad <cdalten@gmail.com> writes:
> [...]
>> It appears that I don't get undefined behavior when I remove the space
>> before and after '\n' in printf()
>>
>> m-net% more mod.c
>> #include <stdio.h>
>>
>> int main (void)
>> {
>> char str[] = "Aamit";
>> *str='R';
>> printf("%s\n" ,str);
>> return 0;
>> }

> [...]
>
> The space before or after '\n' has nothing to do with whether the
> behavior is undefined.


According to some interpretations of the standard,
the removed space, which had previously been placed after the '\n',
might have had something to with it
on an implementation that requires a terminating new-line character
on the last line.

N869
7.19.2 Streams

[#2]
Whether
the last line requires a terminating new-line character is
implementation-defined.


--
pete
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h19.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,33853 seconds with 30 queries