why debug step by step, it s ok

B

baumann@pan

Hi all,


I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>


int main() {
char *buf="5/90/45";
char *token;
char *lasts;


printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
printf("token = \"%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}

the program has abused the strok, i know it,

******but why when i `gdb prog_name` and execute step by step , it 's
ok?


thanks

baumann@pan
 
M

Mark F. Haigh

baumann@pan said:

the program has abused the strok, i know it,

******but why when i `gdb prog_name` and execute step by step , it 's
ok?

Undefined behavior is exactly that: undefined. The code might work
fine, crash, generate nasal demons, or cause mudslides in California.

The point is, you don't know.


Mark F. Haigh
(e-mail address removed)
 
M

Michael Knaup

baumann@pan said:
Hi all,


I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>


int main() {
char *buf="5/90/45";
buf is a pointer to a string constant
char buf[] = "5/90/45"
is correct.
char *token;
char *lasts;


printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
strtok changes the content of buf, so the content must be changeable.
 
B

baumann@pan

you donn't answer my question,

i have answered the question in another post thread.

and in practice, i found when in debug and exec step by step, the prog
runs well.


Michael said:
baumann@pan said:
Hi all,


I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>


int main() {
char *buf="5/90/45";
buf is a pointer to a string constant
char buf[] = "5/90/45"
is correct.
char *token;
char *lasts;


printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
strtok changes the content of buf, so the content must be changeable.
printf("token = \"%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}

the program has abused the strok, i know it,

******but why when i `gdb prog_name` and execute step by step , it 's
ok?


thanks

baumann@pan
 
B

baumann@pan

you can read the post
http://groups-beta.google.com/group...abde6a37a51/e7000d5e177771c8#e7000d5e177771c8

the 2nd post i pointed out the problem, the 3rd post, i bring forward
the problem asked here.

Michael said:
baumann@pan said:
Hi all,


I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>


int main() {
char *buf="5/90/45";
buf is a pointer to a string constant
char buf[] = "5/90/45"
is correct.
char *token;
char *lasts;


printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
strtok changes the content of buf, so the content must be changeable.
printf("token = \"%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}

the program has abused the strok, i know it,

******but why when i `gdb prog_name` and execute step by step , it 's
ok?


thanks

baumann@pan
 
M

Michael Knaup

baumann@pan said:
you can read the post
http://groups-beta.google.com/group...abde6a37a51/e7000d5e177771c8#e7000d5e177771c8

the 2nd post i pointed out the problem, the 3rd post, i bring forward
the problem asked here.

Michael said:
baumann@pan said:
Hi all,


I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>


int main() {
char *buf="5/90/45";
buf is a pointer to a string constant
char buf[] = "5/90/45"
is correct.
char *token;
char *lasts;


printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
strtok changes the content of buf, so the content must be changeable.
printf("token = \"%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}

the program has abused the strok, i know it,

******but why when i `gdb prog_name` and execute step by step , it 's
ok?


thanks

baumann@pan

I showed the solution for the problem and M. F. Haigh wrote the
answer to your question.

And here, espacially for you, again:

Modifiying a string literal leads to undefined behavior.
This means it might work in one execution environment (gdb)
or crash, or format your disk, or ... in another.
 
M

Michael Knaup

Michael Knaup wrote:

so where is the difference between

(1) char *buffer = "Constant Literal";

and
(2) char buffer[17] = "Constant Literal";

In (1) you define a pointer to char in (2) you define an
array of char's.

In (1) the pointer points to the constant string literal,
in (2) the array is initialised with the char's of the
constant literal. This means (2) is equivalent to
(2) char buffer[17] = { 'C', 'o', ..., 'l', '\000' };

And cause the compiler knows the length of the initialising
string literal in (2) you are not required to give the array
size explicitly so you can just write
char buffer[] = "Con...";
 
E

Emmanuel Delahaye

baumann@pan wrote on 03/06/05 :
#include <stdio.h>
#include <string.h>

int main() {
char *buf="5/90/45";

char buf[]="5/90/45";
char *token;
char *lasts;

printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
printf("token = \"%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}

strings are not mutable...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
E

Emmanuel Delahaye

R

Robert Gamble

baumann@pan wrote on 03/06/05 :
#include <stdio.h>
#include <string.h>

int main() {
char *buf="5/90/45";

char buf[]="5/90/45";
char *token;
char *lasts;

printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
printf("token = \"%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}

strings are not mutable...

Psst: string _literals_ are not mutable.

Robert Gamble
 
K

Keith Thompson

Robert Gamble said:
Psst: string _literals_ are not mutable.

Psst: Attempting to modify a string literal invokes undefined
behavior. One way that undefined behavior can be manifested is that
the string literal can be modified.

Saying that string literals are not mutable could imply that it's an
error that the implementation will catch. It might, or it might not.
It's up to the programmer to avoid making the mistake in the first
place.
 
A

August Karlstrom

baumann@pan said:
Hi all,


I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>


int main() {
char *buf="5/90/45";
char *token;
char *lasts;


printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
printf("token = \"%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}

the program has abused the strok, i know it,

******but why when i `gdb prog_name` and execute step by step , it 's
ok?

baumann here clearly states that he knows that the program is incorrect
and the reason for it. Many of you reply as if he didn't know that.
Please read the post before replying.


-- August
 
R

Robert Gamble

Keith said:
Psst: Attempting to modify a string literal invokes undefined
behavior. One way that undefined behavior can be manifested is that
the string literal can be modified.

I thought about clarifying that but decided not to assuming that the
"without invoking undefined behavior" part was implied and trying not
to be too pedantic as I know what Emmanuel meant ;)
Saying that string literals are not mutable could imply that it's an
error that the implementation will catch. It might, or it might not.

I agree, bad decision on my part.

Robert Gamble
 
B

Barry Schwarz

Hi all,


I have one simple program:
#define __EXTENSIONS__
#include <stdio.h>
#include <string.h>


int main() {
char *buf="5/90/45";
char *token;
char *lasts;


printf("tokenizing \"%s\" with strtok():\n", buf);
if ((token = strtok(buf, "/")) != NULL) {
printf("token = \"%s\"\n", token);
while ((token = strtok(NULL, "/")) != NULL) {
printf("token = \"%s\"\n", token);
}
}
}

the program has abused the strok, i know it,

******but why when i `gdb prog_name` and execute step by step , it 's
ok?

One of the worst forms of undefined behavior is "pretending to work as
intended". This can be compounded by "only failing when being
displayed to an important customer or senior management".

Undefined means anything can happen. You just experienced one such
anything.


<<Remove the del for email>>
 
E

Emmanuel Delahaye

Robert Gamble wrote on 04/06/05 :
I thought about clarifying that but decided not to assuming that the
"without invoking undefined behavior" part was implied and trying not
to be too pedantic as I know what Emmanuel meant ;)

Ok, I rephrase it:

"The C langage doesn't guarantee that a string literal is mutable".

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
K

Keith Thompson

Emmanuel Delahaye said:
Robert Gamble wrote on 04/06/05 :

Ok, I rephrase it:

"The C langage doesn't guarantee that a string literal is mutable".

Another way to put it is that you can't go wrong assuming that string
literals are immutable.

That's not quite true if you're trying to figure out why code that
modifies a string literal *seems* to be working, but it's a good rule
for writing new code.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top