473,671 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("tokeniz ing \"%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 1332
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*****@sbcglob al.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("tokeniz ing \"%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("tokeniz ing \"%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("tokeniz ing \"%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("tokeniz ing \"%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("tokeniz ing \"%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("tokeniz ing \"%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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4726
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 article which covers this: http://support.microsoft.com/default.aspx?scid=kb;en-us;299633#2
0
288
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 want to step into the EventHandlers of the raised events without setting a breakpoint. How can this be done?
6
1624
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 would give me message that it can't show me codes because only assembly codes are available. Is there any good article on how to debug something like this? Also, I've only being working with C#.net for 3 months and am still not very familiar...
4
10590
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 other projects) -- I've tried adding the source code project (that was used to build the DLL) into my current solution, but I still can't seem to step into the code of my DLL. Any suggestion? I must be missing something simple?
3
1854
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 question is how can I add breakpoint somewhere of one clsss of solution B (open it in Solution A environment) and when I trace the Solution A, the system will stop at break-point. I don't want to trace the code one step by step to open a class in...
5
1979
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# installation I don't have build solution, rebuild solution, Step into, Step out, or the Exceptions options on my menu Does anybody know why these important tools are missing from the menu structure?
22
4017
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 receive 5 bytes buffer , I call the BeginReceive and then wait on AsyncWaitHandle.WaitOne() but it is signald imidiatly , and the next call to EndReceive return zero bytes length , also the buffer is empty. here is the code: public static byte...
2
1656
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 that when the breakpoint is reached I should select Step Into from the Debug menu. However, Step Into does not appear as an option. I only see Step Over? On the Debug toolbar, I can see the icon for Step Into but it is greyed out.
5
2369
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 a breakpoint. SC.
2
2223
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 breakpoint in my web app that calls one of the web service methods and try to step into it, but am unable to step into the web service. i've also tried running two instances of VS2003 at the same time. the second instance of VS opens my web...
0
8485
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8403
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5704
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.