473,729 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

disturbed between two versions

I'm still a neby in C, i do have two versions of about the same algo.
The first one works fine but de second. I don't see what's the prob with
the second.

the purpose to cut out in sections a unix path.

the test source :
--- essai_token.c ------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void)
{
const char *unix_sep = "/";
char *token = malloc(sizeof (*token) * 256);
if(token == NULL) return EXIT_FAILURE;

//
// Version 1
//
printf("// Version 1\n");
char *path_v1 = strdup("/path_v1/to/something");
printf("path_v1 = %s\n", path_v1);
token = strtok(path_v1, unix_sep);
printf("token = %s\n", token);
while(token != NULL) {
token = strtok(NULL, unix_sep);
printf("token = %s\n", token);
}
free(path_v1);
path_v1 = NULL;
printf("\n");

//
// Version 2
//
printf("// Version 2\n");
char *path_v2 = malloc(sizeof (*path_v2) * 256);
if(path_v2 == NULL) return EXIT_FAILURE;
path_v2 = "/path_v2/to/something";
printf("path_v2 = %s\n", path_v2);
token = strtok(path_v2, unix_sep);// <= broken HERE
printf("token = %s\n", token);
while(token != NULL) {
token = strtok(NULL, unix_sep);
printf("token = %s\n", token);

}
free(path_v2);
path_v2 = NULL;
free(token);
token = NULL;
return EXIT_SUCCESS;
}
------------------------------------------------------------------------

run output :

~/work/C/developpez/listes_chainees _double%./essai_token
// Version 1
path_v1 = /path_v1/to/something
token = path_v1
token = to
token = something
token = (null)

// Version 2
path_v2 = /path_v2/to/something
zsh: bus error ./essai_token

then, the broken line is "token = strtok(path_v2, unix_sep);"

no warnings nor errors at the compil time using the following gcc
options :

alias ycc='cc -W -Wall -Wextra -Wuninitialized -Wstrict-prototypes
-Wmissing-prototypes -pedantic -std=c99 -O2 -pipe -o '

pratically i make use only of the first version, however i'd like to
understand what's my mistake in the second one...
i know that working with strtok needs a writable mem zone, i assume
malloc does that ???

my setup :
Mac OS X 10.4.7
~/work/C/developpez/listes_chainees _double%gcc --version
powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build
5363)

--
une bévue
Sep 10 '06
21 1550
Mark McIntyre <ma**********@s pamcop.netwrite s:
On Sun, 10 Sep 2006 16:54:42 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.orgw rote:
>>Mark McIntyre <ma**********@s pamcop.netwrite s:
[...]
>>Note: The = operator does NOT copy. It assigns values - in this case,
it assigns the value of path_p2 (which is a pointer) to be a pointer
to the string "/path_v2/to/something".

Yes, the "=" operator *does* copy.

Ok, I'll bite. For a weak definition of copy, which doen't necessarily
include the result being identical to the original.
Fair enough.
>>(In this case, what it copies is a pointer value, not what it points to.)

Bear in mind that it does not necessarily create a copy of the value,
since the value is type-converted en route if required. (6.5.16.1 p2).

(I admit I left the word "strings" out after the word "copy", but then
strings don't exist in C so it seemed reasonable not to leave it
in...)
Here you lost me. What do you mean by "strings don't exist in C"?
There is no string type, but a string is defined in C99 7.1.1p1 as "a
contiguous sequence of characters terminated by and including the
first null character." Strings certainly do exist in C.

--
Keith Thompson (The_Other_Keit h) 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.
Sep 11 '06 #21
On Mon, 11 Sep 2006 04:15:06 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.orgw rote:

>Here you lost me. What do you mean by "strings don't exist in C"?
I meant that there's no string type, unlike in certain other
languages.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 11 '06 #22

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

Similar topics

7
2120
by: Ted | last post by:
I would like to collect opinions on which versions of Python should be considered watershed versions. By this I mean versions which are stable and contain significant landmark features. As an example, in Windows operating systems, I would consider Windows 98SE to be the pre-NT watershed release and Windows 2K to be the post-NT release. Thanks.
3
1525
by: zorro | last post by:
Hello, If I have to test my code on different Netscape versions, can I download and run those versions on the same computer or will it create conflicts? And if I had to test only one version which one should it be - which one has the most common elements to all the versions?
0
1820
by: Oliver Elphick | last post by:
The attached proposal is written primarily for Debian. Its motivation is that the current package upgrade process is pretty flaky and also that the current packaging does not really provide for multiple simultaneous postmasters, which are wanted by people hosting several database clusters. I assume these proposals may also be of interest to Red Hat packagers. I am copying it to the PostgreSQL list in case there are any obvious...
14
4188
by: wolftor | last post by:
1) Is there a free runtime version of Access available that is more recent than the one for Access 2000? 2) If I create an application (MDE) in A2K, will it run on all later versions of Access? 3) If I create a CD using A2K Developer that includes the runtime version of Access 2K and an installation package, and if someone tries to install the application from the CD, what happens if someone already has Access 2000 or a later version...
4
2324
by: Dalan | last post by:
After reading and experiencing the phenomenon of installing MS Office 2000 on a system that already has MS Office 97, or for that matter just Access 97 Runtime, I saw the ugliness that ensues. If one elects the standard installation, then Office 2000 deletes a large percentage of the older files. Of course, if one happens to choose Custom installation and elects not to uninstall prior versions, then things are happier. Yes, the archives...
5
2931
by: Michael Maes | last post by:
Hi, We have an ERP-Application that needs to interact with an "external accountancy program". This is acchieved through a "Connector" (ActiveX-dll) so kindly provided by the Accountancy-Program. Our issue however is that there are multiple (current) versions of that connector, each one working only with a certain version of the installed accountancy-program. Of all current versions we use shared (I mean available in all versions, I don't...
5
12096
by: Laurence | last post by:
In VS.2005 using VB.NET There are two versions on every project, The Assembly Version and the File Version. Why are there two different versions? As far as I can tell, there is not need for two versions. The assembly is the file, isn't it?
37
3736
by: Allen Browne | last post by:
If you develop for others, you probably have multiple versions of Access installed so you can edit and create MDEs for clients in different versions. This works fine under Windows XP, even with Access 2007 installed. It does *not* work under Windows Vista Ultimate. After running Access 2007, when you open an earlier version of Access, no code works, because the references are fouled up. And Access 97 does not work at all. Access should...
1
1814
by: M.-A. Lemburg | last post by:
On 2008-07-25 08:13, python@bdurham.com wrote: Yes. But then Intel Itanium is being phased out anyway and the AMD64 build works on both Intel and AMD processors. True.
0
8913
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
8761
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
9280
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8144
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
6722
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
6016
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
4525
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
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.