473,795 Members | 3,002 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Crazy stuff

If I have this(please bear with me - this is a case of a former java
guy going back to C ;-)

int main () {
char *tokconvert(cha r*);
char str[] = "dot.delimited. str";
char *result;

result = tokconvert(str) ;
return 0;
}

char *tokconvert(cha r *strToConvert) {

char *token;
char *tokDelim =",.";

token=strtok(st rToConvert, tokDelim);

while(token != NULL) {
printf("Token -> %s \n", token);
token=strtok(NU LL, tokDelim);
}
}

the thing compiles and runs fine, however if I declare char *str =
"dot.delimited. str" (instead of char str[] = "dot.delimited. str") the
whole thing falls on its ass real bad (bus error) - strtok is defined
as char *strtok(char *, const char *) - what's going on?

more craziness: if I declare/initialise

char *str;
str = strdup("dot.del imited.str)

inside tokconvert (instead of main) and pass that to strtok - it runs
fine!!

any thoughts are most welcome..

chumbo.

P.S.
btw this is on FreeBSD 5.1.2 (using gcc 3.3.3)
Nov 14 '05
32 1990
"Charlie Gordon" <ne**@chqrlie.o rg> wrote:
please stop teaching newbies to use functions with flawed semantics like
this one or its friends : gets(), strncpy(), strncat()...
Obviously I agree about gets(), and strncpy() is rarely if ever the
right function, but if you won't allow strncat(), which function _do_
you use when you want a length-limited string copy?


Why not write one that does what is needed.


Such as strncat()?
Or use a test and write appropriate code for each case.

strncat() is not the worst of these, but it is consistently misused in code I
come across :


So is printf().

Richard
Nov 14 '05 #31
> > He probably refers to Criminally Stupid Behaviour (TM), such as
ignoring the difference between the size of the destination
and the number of characters that still fit in, that is using
strncat(dst, src, dst_size);
instead of
strncat(dst, src, dst_size-strlen(dst)-1);
Exactly, and many variants with more subtle issues (such as what if strlen(dst)
= dst_size)

However, if you have to work with people who cannot or do not
want to read and comprehend the function documentation, this is
really just one of the many things which will go wrong.
Quite true. But I make it part of house rules to avoid "booby trap programming
techniques" and part of house teachings to learn about them and detect them in
external code.
I guess that he would rather like to pass the overall size of the
destination which is IMO no bad idea at all.
The only question to answer is then whether to make the last
character '\0' if no string terminator was encountered or not.

Misleading: I mean in the destination (that is
strlen(dst)>= the passed size)


Insightful remark !

The function we are talking about does not modify the destination buffer in this
case, nor in the case of dest_size <= 0.
It does not dereference dest beyond dest_size, thus does not compute
strlen(dest).
It is arguable whether it should accept NULL pointers either as source or
destination.
It returns a useful result : not the adress of the destination buffer, but an
iidication of what happened, such as the number of characters copied before the
'\0' or something negative in case of truncation, or malformed destination, or
invalid dest_size.

This is really a family of 5 functions :

int pstrlen(const char *buf, int buf_size);
int pstrcpy(char *buf, int buf_size, const char *str);
int pstrncpy(char *buf, int buf_size, const char *str, int n);
int pstrcat(char *buf, int buf_size, const char *str);
int pstrncat(char *buf, int buf_size, const char *str, int n);

I use int instead of size_t for the sizes on purpose, only ssize_t might be more
appropriate.

Chqrlie.

Nov 14 '05 #32
On Tue, 16 Nov 2004 15:34:51 +0100, Charlie Gordon wrote:
"Lawrence Kirby" <lk****@netacti ve.co.uk> wrote in message
news:pa******** *************** *****@netactive .co.uk...
On Tue, 16 Nov 2004 12:36:55 +0100, Charlie Gordon wrote:

...
> The case against strtok() is also worth fighting : please stop teaching newbies > to use functions with flawed semantics like this one or its friends : gets(), > strncpy(), strncat()...


What's wrong with strncat()?


It is often misunderstood, and misused :

strncpy(dest, source, sizeof(dest)); // dangerous !
strncat(dest, source, sizeof(dest)); // worse even!


I can understand strncpy() being misunderstood, its behaviour
is oddball. strncat() is pretty simple though. You might just
as well say strcpy() is dangerous (and yes some people will
say that) but it isn't difficult to use safely.

Lawrence
Nov 14 '05 #33

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

Similar topics

0
1670
by: Tom Schmitt | last post by:
Hi everybody. For a long time now, I've been searching for a good, fast, reliable CMS. Most of the stuff out there (like phpnuke) is too fancy and overloaded for what I need: - more than one Menu-Layer... submenus and all - MySQL-support - configurable templates - custom input templates (not a must) That's all I need. Do you have any suggestions on what to use/try out?
7
3383
by: Oliver Andrich | last post by:
Hi everybody, I have to write a little skript, that reads some nasty xml formated files. "Nasty xml formated" means, we have a xml like syntax, no dtd, use html entities without declaration and so on. A task as I like it. My task looks like that... 1. read the data from the file. 2. get rid of the html entities 3. parse the stuff to extract the content of two tags.
11
3352
by: doltharz | last post by:
Please Help me i'm doing something i though was to be REALLY EASY but it drives me crazy The complete code is at the end of the email (i mean newsgroup article), i always use Option Explicit and Response.Expires=-1,
4
1800
by: Teknowbabble | last post by:
Hi All... Ive done several simple web pages, no biggy! Is there any way to layout a Table with lets say 3 columns and have the contents of those columns not have to fit the rigid row column standard of setting up tables. Basically all I want to do is fill up column one, then column 2, and then column 3. Each column has its spacing.
38
4597
by: Kai Jaeger | last post by:
I am playing with setting font sizes in CSS using em as unit of measurement. All seems to be fine. Even Netscape Navigator shows the characters very similar to IE, what is not the kind if px is used! But! when selecting the "Larger" or "Smaller" command from the menubar in IE, font sizes increases from normal (1em) to, say, 6em or so _in the first step_!!! In the next step it seems to be 20em or say. Choosing "Smaller" makes the text...
9
2341
by: rbronson1976 | last post by:
Hello all, I have a very strange situation -- I have a page that validates (using http://validator.w3.org/) as "XHTML 1.0 Strict" just fine. This page uses this DOCTYPE: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> When I change the DOCTYPE to (what should be the equivalent):
1
4600
by: Bruce W.1 | last post by:
I want to put a live connection in a web page to periodically updated data. When the data changes, the server notifys the client and gives the new data. The client does not regularly refresh the page. The client responds to server-side events only. The screen display changes without any user interaction. An application like this might be a chat, stock ticker, or data inventory. Sofar as I can tell nothing in the .NET framework can...
5
1715
by: Pupeno | last post by:
Hello, I am experiencing a weird behavior that is driving me crazy. I have module called Sensors containing, among other things: class Manager: def getStatus(self): print "getStatus(self=%s)" % self return {"a": "b", "c": "d"} and then I have another module called SensorSingleton that emulates the
3
1771
by: squash | last post by:
I have spent two hours trying to make sense of this script, called crazy.php. The output should be nothing because $cookie_password is nowhere defined in this script, correct? But it actually outputs the value that other scripts i have running set it to. Why should crazy.php care what other scripts are running that use that variable name?? <?php crazy();
2
3631
by: kheitmann | last post by:
OK, so I have a blog. I downloaded the "theme" from somewhere and have edited a few areas to suit my needs. There are different font themes within the page theme. Long story short, my "Text Posts" are supposed to be in the font: Georgia, but they are showing up in "Times New Roman"...blah! I can't find anything wrong in the code, but who am I trying to fool? I know nothing about this stuff. The code is below. The parts that I *think*...
0
9672
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
10439
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
10165
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,...
1
7541
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
6783
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.