By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,630 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

why debug step by step, it s ok

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
baumann@pan wrote:
Hi all,

<code snipped>
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
mf*****@sbcglobal.net

Nov 14 '05 #2

P: n/a
baumann@pan wrote:
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


--
Michael Knaup
Nov 14 '05 #3

P: n/a
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 Knaup wrote:
baumann@pan wrote:
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


--
Michael Knaup


Nov 14 '05 #4

P: n/a
you can read the post
http://groups-beta.google.com/group/...000d5e177771c8

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

Michael Knaup wrote:
baumann@pan wrote:
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


--
Michael Knaup


Nov 14 '05 #5

P: n/a
baumann@pan wrote:
you can read the post
http://groups-beta.google.com/group/...000d5e177771c8
the 2nd post i pointed out the problem, the 3rd post, i bring forward
the problem asked here.

Michael Knaup wrote:
baumann@pan wrote:
> 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


--
Michael Knaup


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.

--
Michael Knaup
Nov 14 '05 #6

P: n/a
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...";
--
Michael Knaup
Nov 14 '05 #7

P: n/a
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.

Nov 14 '05 #8

P: n/a
baumann@pan wrote on 03/06/05 :
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.


An Undefined Behaviour has an undefined behavour.

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

Nov 14 '05 #9

P: n/a
On Fri, 03 Jun 2005 17:12:39 +0200, Emmanuel Delahaye wrote:
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
Nov 14 '05 #10

P: n/a
Robert Gamble <rg*******@gmail.com> writes:
On Fri, 03 Jun 2005 17:12:39 +0200, Emmanuel Delahaye wrote:

[...]
strings are not mutable...


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11

P: n/a
baumann@pan wrote:
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
Nov 14 '05 #12

P: n/a
Keith Thompson wrote:
Robert Gamble <rg*******@gmail.com> writes:
On Fri, 03 Jun 2005 17:12:39 +0200, Emmanuel Delahaye wrote: [...]
strings are not mutable...


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.


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

Nov 14 '05 #13

P: n/a
On 3 Jun 2005 00:37:19 -0700, "baumann@pan" <ba*********@gmail.com>
wrote:
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>>
Nov 14 '05 #14

P: n/a
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.

Nov 14 '05 #15

P: n/a
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
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".


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.