473,382 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

why debug step by step, it s ok

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
15 1316
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
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
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
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
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
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
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
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Grant Schenck | last post by:
Hello, I'm trying to figure out how to debug a very simple COM object I developed in VB. I'm very new to VB and ASP so I have a lot to learn. That said, I found what I thought was perfect...
0
by: Edi | last post by:
I want to debug my application and for that I use the step into debug command button. When I click on the step into command the next step ist executed. The executed step raises some events. Now I...
6
by: Alpha | last post by:
Hi, I'm fixing a bug in an application and need to step thru the appl to find where it's occuring. The main project includs 33 other projects each having a .resx file in it. When I step through it...
4
by: Rob R. Ainscough | last post by:
I've created several DLL's that I use in other projects. I "Add Reference" to these other projects pointing to my DLL. Probelm is, I can't seem to be able to step into my DLL code (from these...
3
by: ahawk | last post by:
Hi, All, I'm a beginner of Vb.net. My solution(A) has reference another object (solution B), so when I debug( trace) my program step by step, the system opens the class code of solution B. My...
5
by: Steve Le Monnier | last post by:
I've just noticed that the menu items available in Visual Studio 2005 for the Debug and Build menus differ significantly when compared to the menu items in Visual Studio 2003. On my C#...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
2
by: =?Utf-8?B?Sm9obiBT?= | last post by:
Hi, I'm trying to follow Lab1 of the WF Hands On Labs. Having created the project and set a breakpoint as per instructions, I press F5 to compile and run the project. The instructions say...
5
by: =?Utf-8?B?U0M=?= | last post by:
Hi everyone, i'm workin with vb.net 2003 and i using the debug step into, but when i call a form within another the debugger doesn't go to the form that i call, how do i solve this, without putting...
2
by: steven | last post by:
hi i've got a web app that calls a web service, both of which i've written and are available on localhost. i've added a web reference in my web app to http://localhost/mywebservice. i've set a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.