473,466 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Runtime errors possible when I forgot include <string.h>

Hello List,

I have ar more or less academical question.

Can there arise runtime errors in a program, if the include of
<string.h> has been forgotten? If all the arguments to the functions of
<string.h> are correct, is it possible that an error occurs, and what
an error might this be?

Regards

Alexander

Nov 15 '05 #1
12 2175
Yes, you will get undefined behaviour if you try to use any of the
standard functions defined in <string.h> and not include the header.

For ex:
char *strcpy(char *dst, const char *src);

char *strncpy(char *dst, const char *src, size_t n);

char *strdup(const char *s1);

char *strpbrk(const char *s1, const char *s2);

char *strstr(const char *s1, const char *s2);

char *strtok(char *s1, const char *s2);

char *strtok_r(char *s1, const char *s2, char **lasts);

all the above return 'char *' and are defined as a part of <string.h> \
pkg, now if you dont include <string.h> and try using any of the above
fns, then the compiler thinks those functions return 'int' (by default)
and generate code in that way, this results in wrong interpretation of
return values !

- Ravi


ABeck wrote:
Hello List,

I have ar more or less academical question.

Can there arise runtime errors in a program, if the include of
<string.h> has been forgotten? If all the arguments to the functions of
<string.h> are correct, is it possible that an error occurs, and what
an error might this be?

Regards

Alexander


Nov 15 '05 #2
Le 25-10-2005, ABeck <al*******************@aei.mpg.de> a écrit*:
I have ar more or less academical question.

Can there arise runtime errors in a program, if the include of
<string.h> has been forgotten? If all the arguments to the functions of
<string.h> are correct, is it possible that an error occurs, and what
an error might this be?


Without include of <string.h>, all functions are 'implicitely
defined', that is return value is supposed to be int and
parameters are converted using 'default argument promotion'
(6.5.2.2/6), ie integer promotion and float->double.

So, the generated code could be incompatible with the
implementation (UB). If implicit conversion between
int and char* is unsafe (sizeof(int) < sizeof(char*)
for example), then, strange things will occur.

But, in most platform, implict conversion
between int and char* behaves in a way such that
the code runs.

Marc Boyer
Nov 15 '05 #3
Marc Boyer wrote:
Le 25-10-2005, ABeck <al*******************@aei.mpg.de> a écrit :
I have ar more or less academical question.

Can there arise runtime errors in a program, if the include of
<string.h> has been forgotten? If all the arguments to the functions of
<string.h> are correct, is it possible that an error occurs, and what
an error might this be?

Without include of <string.h>, all functions are 'implicitely
defined', that is return value is supposed to be int and
parameters are converted using 'default argument promotion'
(6.5.2.2/6), ie integer promotion and float->double.

So, the generated code could be incompatible with the
implementation (UB). If implicit conversion between
int and char* is unsafe (sizeof(int) < sizeof(char*)
for example), then, strange things will occur.

But, in most platform, implict conversion
between int and char* behaves in a way such that
the code runs.

Marc Boyer

In some cases (depending on what functions are bing called and what
compiler is used) the compiler will use it's own built-in function(s).
Again, this depends on what functions are being used as well as the
compiler.

-Joe
Nov 15 '05 #4
On 2005-10-25, Marc Boyer <Ma********@enseeiht.yahoo.fr.invalid> wrote:
Le 25-10-2005, ABeck <al*******************@aei.mpg.de> a écrit*:
I have ar more or less academical question.

Can there arise runtime errors in a program, if the include of <string.h>
has been forgotten? If all the arguments to the functions of <string.h> are
correct, is it possible that an error occurs, and what an error might this
be?
Without include of <string.h>, all functions are 'implicitely defined',
that is return value is supposed to be int and parameters are converted using
'default argument promotion' (6.5.2.2/6), ie integer promotion and
float->double.

So, the generated code could be incompatible with the implementation (UB).
If implicit conversion between int and char* is unsafe (sizeof(int) <
sizeof(char*) for example), then, strange things will occur.

But, in most platform, implict conversion between int and char* behaves in
a way such that the code runs.


"most"? That's a funny definition of 'most' you have there. Even on platforms
where they're the same size they may be returned in different places. Many
64-bit platforms now still have 32-bit int, and then there's still the odd LP32
platform to deal with [16-bit int, 32-bit pointer]
Marc Boyer

Nov 15 '05 #5
In article <sl********************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
(someone else wrote ">>")
But, in most platform, implict conversion between int and char*
behaves in a way such that the code runs.


"most"? That's a funny definition of 'most' you have there. Even on
platforms where they're the same size they may be returned in different
places. Many 64-bit platforms now still have 32-bit int, and then there's
still the odd LP32 platform to deal with [16-bit int, 32-bit pointer]


It is a practical, although obviously not clc-correct, definition.

Nov 15 '05 #6
On 2005-10-25, Kenny McCormack <ga*****@yin.interaccess.com> wrote:
In article <sl********************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
(someone else wrote ">>")
But, in most platform, implict conversion between int and char*
behaves in a way such that the code runs.


"most"? That's a funny definition of 'most' you have there. Even on
platforms where they're the same size they may be returned in different
places. Many 64-bit platforms now still have 32-bit int, and then there's
still the odd LP32 platform to deal with [16-bit int, 32-bit pointer]


It is a practical, although obviously not clc-correct, definition.


So basically by "most" you mean "your". I suppose you're on a vax, or a 386 of
some kind?
Nov 15 '05 #7
Jordan Abel <jm****@purdue.edu> writes:
On 2005-10-25, Marc Boyer <Ma********@enseeiht.yahoo.fr.invalid> wrote:
Le 25-10-2005, ABeck <al*******************@aei.mpg.de> a écrit*:
I have ar more or less academical question.

Can there arise runtime errors in a program, if the include of <string.h>
has been forgotten? If all the arguments to the functions of <string.h> are
correct, is it possible that an error occurs, and what an error might this
be?


Without include of <string.h>, all functions are 'implicitely
defined', that is return value is supposed to be int and parameters
are converted using 'default argument promotion' (6.5.2.2/6), ie
integer promotion and
float->double.

So, the generated code could be incompatible with the
implementation (UB). If implicit conversion between int and char*
is unsafe (sizeof(int) < sizeof(char*) for example), then, strange
things will occur.

But, in most platform, implict conversion between int and char*
behaves in a way such that the code runs.


"most"? That's a funny definition of 'most' you have there. Even on
platforms where they're the same size they may be returned in
different places. Many 64-bit platforms now still have 32-bit int,
and then there's still the odd LP32 platform to deal with [16-bit
int, 32-bit pointer]


There's nothing funny about it. "Most", in the sense of "the
majority, but not all", is probably correct. 64-bit platforms,
platforms with 16-bit ints and 32-bit pointers, and platforms that
return integers and pointers in different registers are (I'm guessing)
still in the minority.

What this means, of course, is that on most systems the undefined
behavior is more difficult to diagnose.

--
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 15 '05 #8
In article <sl********************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-10-25, Kenny McCormack <ga*****@yin.interaccess.com> wrote:
In article <sl********************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
(someone else wrote ">>")
But, in most platform, implict conversion between int and char*
behaves in a way such that the code runs.

"most"? That's a funny definition of 'most' you have there. Even on
platforms where they're the same size they may be returned in different
places. Many 64-bit platforms now still have 32-bit int, and then there's
still the odd LP32 platform to deal with [16-bit int, 32-bit pointer]


It is a practical, although obviously not clc-correct, definition.


So basically by "most" you mean "your". I suppose you're on a vax, or
a 386 of some kind?


I didn't know we were talking about me, but since you brought up the
subject, let me say that this thread does bring back memories.

When I first learned C, a million or so years ago, I was working on two
platforms - Sun, which was/is 32/32, and another Unix platform, by
a company whose name you wouldn't recognize, that happened to be 16/32.
Until I learned about #include files, I was frequently surprised when my
code, that worked fine on the Sun, crashed on the other platform. Make of
this what you will.

Nov 15 '05 #9
On 2005-10-25, Kenny McCormack <ga*****@yin.interaccess.com> wrote:
In article <sl********************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-10-25, Kenny McCormack <ga*****@yin.interaccess.com> wrote:
In article <sl********************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
(someone else wrote ">>")
> But, in most platform, implict conversion between int and
> char* behaves in a way such that the code runs.

"most"? That's a funny definition of 'most' you have there. Even
on platforms where they're the same size they may be returned in
different places. Many 64-bit platforms now still have 32-bit
int, and then there's still the odd LP32 platform to deal with
[16-bit int, 32-bit pointer]

It is a practical, although obviously not clc-correct,
definition.
So basically by "most" you mean "your". I suppose you're on a vax,
or a 386 of some kind?


I didn't know we were talking about me, but since you brought up
the subject, let me say that this thread does bring back memories.


Sorry - I tend to lose track of who I'm talking to - even when the
attributions are there I only pay attention about half the time.
When I first learned C, a million or so years ago, I was working
on two platforms - Sun, which was/is 32/32, and another Unix
platform, by a company whose name you wouldn't recognize, that
happened to be 16/32. Until I learned about #include files, I was
frequently surprised when my code, that worked fine on the Sun,
crashed on the other platform. Make of this what you will.


Didn't do much floating point code, huh? I suspect you'd have
discovered the bugs much quicker in that case [while it sometimes
appears safe to lack for a declaration of the string functions, it
is never safe to do that for the math functions]

I have had the dubious luxury of learning C in an era where, as the
original poster said, most platforms are ILP32.
Nov 15 '05 #10
Marc Boyer wrote:
Le 25-10-2005, ABeck <al*******************@aei.mpg.de> a écrit :
I have ar more or less academical question.

Can there arise runtime errors in a program, if the include of
<string.h> has been forgotten? If all the arguments to the functions
of <string.h> are correct, is it possible that an error occurs, and
what an error might this be?
Without include of <string.h>, all functions are 'implicitely
defined',


Unless correctly declared elsewhere.
that is return value is supposed to be int and
parameters are converted using 'default argument promotion'
(6.5.2.2/6), ie integer promotion and float->double.

So, the generated code could be incompatible with the
implementation (UB). If implicit conversion between
int and char* is unsafe (sizeof(int) < sizeof(char*)
for example), then, strange things will occur.

But, in most platform, implict conversion
between int and char* behaves in a way such that
the code runs.


There is no conversion in the C sense, implicit or otherwise.

Even if they have the same size, ints and pointers may be returned
in different registers. The return value may well be totally
unrelated to the actual value returned by a given function.

--
Peter

Nov 15 '05 #11
"Peter Nilsson" <ai***@acay.com.au> writes:
Marc Boyer wrote:
Le 25-10-2005, ABeck <al*******************@aei.mpg.de> a écrit :
> I have ar more or less academical question.
>
> Can there arise runtime errors in a program, if the include of
> <string.h> has been forgotten? If all the arguments to the functions
> of <string.h> are correct, is it possible that an error occurs, and
> what an error might this be?


Without include of <string.h>, all functions are 'implicitely
defined',


Unless correctly declared elsewhere.
that is return value is supposed to be int and
parameters are converted using 'default argument promotion'
(6.5.2.2/6), ie integer promotion and float->double.

So, the generated code could be incompatible with the
implementation (UB). If implicit conversion between
int and char* is unsafe (sizeof(int) < sizeof(char*)
for example), then, strange things will occur.

But, in most platform, implict conversion
between int and char* behaves in a way such that
the code runs.


There is no conversion in the C sense, implicit or otherwise.

Even if they have the same size, ints and pointers may be returned
in different registers. The return value may well be totally
unrelated to the actual value returned by a given function.


Right.

To expand on that a bit, what happens (even if ints and pointers are
the same size and are returned in the same registers) is not a
conversion; it's a reinterpretation of the bits. The function returns
a char* value; the caller treats it as if it were an int.

It's fairly (very?) common for an actual pointer-to-int conversion to
be just a reinterpretation of the bits, but it needn't be.

There likely will be a conversion if the caller explicitly casts the
result to char* (to shut up the required diagnostic). This doesn't
help, though.

A similar situation occurs when the called function returns a double,
and the caller (because of a failure to include the required header0
interprets the result as an int. In this case, conversion from double
to int almost certainly does require some computation rather than just
a reinterpretation of the bits, and the result will be garbage.

--
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 15 '05 #12
Le 25-10-2005, Jordan Abel <jm****@purdue.edu> a écrit*:
On 2005-10-25, Marc Boyer <Ma********@enseeiht.yahoo.fr.invalid> wrote:
Le 25-10-2005, ABeck <al*******************@aei.mpg.de> a écrit*:

Can there arise runtime errors in a program, if the include of <string.h>
has been forgotten? If all the arguments to the functions of <string.h> are
correct, is it possible that an error occurs, and what an error might this
be?
Without include of <string.h>, all functions are 'implicitely defined',
that is return value is supposed to be int and parameters are converted using
'default argument promotion' (6.5.2.2/6), ie integer promotion and
float->double.

So, the generated code could be incompatible with the implementation (UB).
If implicit conversion between int and char* is unsafe (sizeof(int) <
sizeof(char*) for example), then, strange things will occur.

But, in most platform, implict conversion between int and char* behaves in
a way such that the code runs.


"most"? That's a funny definition of 'most' you have there.


Good news: I am always hapy to set a little fun in people life.
Even on platforms
where they're the same size they may be returned in different places. Many
64-bit platforms now still have 32-bit int, and then there's still the odd LP32
platform to deal with [16-bit int, 32-bit pointer]


Of course, 'many' is less ambigus than 'most'.

Përhaps I could gives a more precise definition with something like
'in most plateforms with a C programer developping and testing code on',
but, this was an informal explanation, and I am feed up with
experts playing this kind of game.
If you want to make the list of all/some platforms and reasons
where and why some truble could appear, do it.

Marc Boyer
Nov 15 '05 #13

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

Similar topics

5
by: Danny Anderson | last post by:
Hola! I am working on a program where I am including a library that came with my numerical methods textbook. The "util.h" simply includes a large number of files. I had to change the util.h...
5
by: Gandalf | last post by:
Hello. I thought that strlen was only available after adding #include <string> to my c++ program, but apperently not, or the compiler is clever enough to find it by it self, or what? The...
8
by: Subra Mallampalli | last post by:
Hi, I am trying to use <runtime> section within the web.config file. However, the contents of the <runtime> section seem to be ignored. What am i missing here? Is <runtime> section not used by...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
3
by: asclearuc | last post by:
Hello Is it possible to use map<string&, string&>? Why I need it. I have a large amount of data obtained from XML file. I should do processing of this data. The processing takes many...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
4
by: Travis | last post by:
So here's something I've always wondered. Typically if I want to use a string I simply using namespace std::string; string myString; But when do I actually need to include <string>. What...
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...
0
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...

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.