473,405 Members | 2,187 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,405 software developers and data experts.

sizeof strings

hello everyone ,
this one might seem wired but i dont know why iam confused with this
simple stuff

the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be '\0' .right?

but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the '\0' character of name[9] gone?????

and why is it not complaining???

thank you
mohan gupta
Jul 26 '08 #1
18 1838
mohi wrote:
hello everyone ,
this one might seem wired but i dont know why iam confused with this
simple stuff

the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be '\0' .right?
Not necessarily. Even when storing a string in 'name' you need not
consume all it's elements to do so. A one character string stored
in 'name' is perfectly fine, though it may be a waste of storage.
but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the '\0' character of name[9] gone?????
The strlen function returns the number of characters it's argument
points to, up to, but *not* including the null character, which is not
a part of the string, though it's needed to create a string. Therefore
your 'name' array is one character too short to hold a string of ten
characters. It can only hold a string of maximum nine characters.

Strcpy will blindly copy it's second argument to it's first. It's your
responsibility to make sure that the buffer pointed to by the first
argument has sufficient space to contain the string pointed to by the
second. Otherwise strcpy will overrun the destination buffer and
undefined behaviour will result.
and why is it not complaining???
By pure luck. Try not to depend too much on it in programming.

<snip>

Jul 26 '08 #2
santosh wrote:
mohi wrote:
>char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be '\0' .right?

Not necessarily. Even when storing a string in 'name' you need not
consume all it's elements to do so. A one character string stored
in 'name' is perfectly fine, though it may be a waste of storage.
>but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the '\0' character of name[9] gone?????

The strlen function returns the number of characters it's argument
points to, up to, but *not* including the null character, which is not
a part of the string, though it's needed to create a string.
The null character terminates the string, but is defined by the standard as
part of the string:
7.1.1:
"A string is a contiguous sequence of characters terminated by and
including the first null character."

--
Thad
Jul 26 '08 #3
In article <e1**********************************@n33g2000pri. googlegroups.com>,
mohi <mo**********@gmail.comwrote:
>this one might seem wired but i dont know why iam confused with this
simple stuff
the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be '\0' .right?

but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the '\0' character of name[9] gone?????

and why is it not complaining???
Show the [exact] code to complement the words, otherwise we can
only but guess.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 26 '08 #4
Thad Smith <Th*******@acm.orgwrites:
santosh wrote:
>mohi wrote:
>>char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be '\0' .right?

Not necessarily. Even when storing a string in 'name' you need not
consume all it's elements to do so. A one character string stored
in 'name' is perfectly fine, though it may be a waste of storage.
>>but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the '\0' character of name[9] gone?????

The strlen function returns the number of characters it's argument
points to, up to, but *not* including the null character, which is not
a part of the string, though it's needed to create a string.

The null character terminates the string, but is defined by the
standard as part of the string:
7.1.1:
"A string is a contiguous sequence of characters terminated by and
including the first null character."
Which means that strlen is, by definition, wrong.....
Jul 26 '08 #5
In comp.lang.c, mohi wrote:
hello everyone ,
this one might seem wired but i dont know why iam confused with this
simple stuff

the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be '\0' .right?
Yes... and no.

All you have done with
char name[10];
is defined the variable "name" to reference an array of 10 character
elements. You may fill this array in any way you please, so long as you do
not try to store an element value that is outside of the value range
permitted by a char entity.

A /string/ is a specific type of character array, which contains zero or
more non-zero char elements, followed by a single char element of zero
(\0). A string with 10 non-zero characters (i.e. "0123456789") is contained
in a character array of 11 elements ( i. e.
{'0','1','2','3','4','5','6','7','8','9',0} ), with the final element being
the \0 character.
but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the '\0' character of name[9] gone?????
You overflowed your name[] array.

A string of 10 characters (that is, strlen() returns 10) is stored in a
character array of 11 elements. The first 10 elements contain the non-zero
characters, and the 11'th element contains the terminating \0

When you strcpy()ed the string, you copied the first 10 characters of the
string to elements name[0] to name[9]. Since that's all the space that you
allocated to the name[] array, the next character (the terminating \0)
didn't wind up in the name[] array, but was placed somewhere else. This is
bad - you've introduced a buffer overflow, and caused your program to
exhibit undefined behaviour (which could include successful completion, if
you are lucky).
and why is it not complaining???
Because, strcpy() is a function (albeit, a function implemented by the
standard library), and the size of an array defined outside of a function
is not implicitly determinable from within a function (that's how C works).
You, as the programmer, are expected to cope with that, and ensure that you
do not pass strcpy() invalid values.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Jul 26 '08 #6
co****@panix.com (Greg Comeau) writes:
In article <e1**********************************@n33g2000pri. googlegroups.com>,
mohi <mo**********@gmail.comwrote:
>>this one might seem wired but i dont know why iam confused with this
simple stuff
the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be '\0' .right?

but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the '\0' character of name[9] gone?????

and why is it not complaining???

Show the [exact] code to complement the words, otherwise we can
only but guess.
He has explained it well enough that you should not have to guess.

He has simply overwritten the buffer and has been lucky enough for it
not to crash.

Inconsistency between strlen and standard definition of a string of
course - the same thing which every C programmer in the world gets used
to in lesson 2.

Of course, if he used a debugger such as gdb he could examine the memory
and see the terminating nul character at Addr+10. But that advice would
be far too "off topic" for c.l.c
Jul 26 '08 #7
On Sat, 26 Jul 2008 17:11:17 +0200, Richard wrote:
Thad Smith <Th*******@acm.orgwrites:
>>[...]
The null character terminates the string, but is defined by the
standard as part of the string:
7.1.1:
"A string is a contiguous sequence of characters terminated by and
including the first null character."

Which means that strlen is, by definition, wrong.....
It means that the length of a string does not mean the length of an array
of char. Similarly, a pointer to a string does not mean a pointer to an
array of char. This is not wrong, but this is more confusing than
necessary.
Jul 26 '08 #8
mohi wrote:
the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be '\0' .right?
name[9] will be '\0' if you put a string of length exactly 9 in it. It
may be something else if you put a shorter string in.
but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the '\0' character of name[9] gone?????

and why is it not complaining???
The '\0' went into name[10], which invokes undefined behavior since it's
not part of the object (which ended at name[9]).

One possible example of undefined behavior is that there is no error and
nothing bad happens. In this particular case, that doesn't surprise me;
there's good odds the next 2-6 bytes after 'name' are unused (due to
alignment constraints) and the machine never notices you did something
wrong. However, a minor change in your program may result in memory
corruption of other variables, or porting it to more strict systems may
result in an error, or something entirely random may happen for no
specific reason at all. That's the joy of UB.

S
Jul 26 '08 #9
Thad Smith wrote:
santosh wrote:
>mohi wrote:
>>char name[10];

this defines a array of char with size 10 where 0-8 positions(count
9) can be characters and name[9] should be '\0' .right?

Not necessarily. Even when storing a string in 'name' you need not
consume all it's elements to do so. A one character string stored
in 'name' is perfectly fine, though it may be a waste of storage.
>>but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the '\0' character of name[9] gone?????

The strlen function returns the number of characters it's argument
points to, up to, but *not* including the null character, which is
not a part of the string, though it's needed to create a string.

The null character terminates the string, but is defined by the
standard as part of the string:
7.1.1:
"A string is a contiguous sequence of characters terminated by and
including the first null character."
Thanks for the heads-up.

Jul 26 '08 #10
On Sat, 26 Jul 2008 06:20:09 -0700 (PDT), mohi
<mo**********@gmail.comwrote:
>hello everyone ,
this one might seem wired but i dont know why iam confused with this
simple stuff

the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be '\0' .right?

but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the '\0' character of name[9] gone?????
I:\c16>type z2.c
#include <stdio.h>
#include <string.h>

int main() {
char string1[10];
char string2[5] = "wxyz";

printf("string2 = '%s'\n", string2);
strcpy(string1, "1234567890");
printf("string1 = '%s'\n", string1);
printf("string2 = '%s'\n", string2);
strcpy(string1, "1234567890a");
printf("string1 = '%s'\n", string1);
printf("string2 = '%s'\n", string2);

return 0;
}

I:\c16>tcc z2.c
Turbo C Version 2.01 Copyright (c) 1987, 1988 Borland International
z2.c:
Turbo Link Version 2.0 Copyright (c) 1987, 1988 Borland
International

Available memory 389890

I:\c16>z2
string2 = 'wxyz'
string1 = '1234567890'
string2 = ''
string1 = '1234567890a'
string2 = 'a'

I:\c16>
>
and why is it not complaining???
It does not do any checking.
>


thank you
mohan gupta
Jul 27 '08 #11
santosh wrote:
mohi wrote:
>this one might seem wired but i dont know why iam confused with
this simple stuff. the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions
(count 9) can be characters and name[9] should be '\0' .right?

Not necessarily. Even when storing a string in 'name' you need not
consume all it's elements to do so. A one character string stored
in 'name' is perfectly fine, though it may be a waste of storage.
The object name can hold up to 10 chars. If any one of them is a
'\0' then name holds a string, and that string ends with the char
(if any) before that '\0'. If there is no '\0' in the char array,
that array just holds chars, not a string, and should not be passed
to any functions that want string input.

Strings are a data format, and are held in objects of the type
'array of char'.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Jul 27 '08 #12
On Sat, 26 Jul 2008 20:42:11 -0400, CBFalconer <cb********@yahoo.com>
wrote:
>santosh wrote:
>mohi wrote:
>>this one might seem wired but i dont know why iam confused with
this simple stuff. the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions
(count 9) can be characters and name[9] should be '\0' .right?

Not necessarily. Even when storing a string in 'name' you need not
consume all it's elements to do so. A one character string stored
in 'name' is perfectly fine, though it may be a waste of storage.

The object name can hold up to 10 chars. If any one of them is a
'\0' then name holds a string, and that string ends with the char
(if any) before that '\0'. If there is no '\0' in the char array,
Since the '/0' is part of the string, it ends with the '\0'.
>that array just holds chars, not a string, and should not be passed
to any functions that want string input.

Strings are a data format, and are held in objects of the type
'array of char'.

Remove del for email
Jul 28 '08 #13
Lew Pitcher <lp******@teksavvy.comwrites:
[...]
A /string/ is a specific type of character array, which contains zero or
more non-zero char elements, followed by a single char element of zero
(\0). A string with 10 non-zero characters (i.e. "0123456789") is contained
in a character array of 11 elements ( i. e.
{'0','1','2','3','4','5','6','7','8','9',0} ), with the final element being
the \0 character.
Quibble: a string is a specific *kind* of character array. There is
no string *type* in C. (I know you didn't mean "type" in that sense.)

[...]
A string of 10 characters (that is, strlen() returns 10) is stored in a
character array of 11 elements. The first 10 elements contain the non-zero
characters, and the 11'th element contains the terminating \0
Since a string includes its terminating '\0', it would be clearer to
say "A string of length 10" rather than "A string of 10 characters".
The '\0' is one of the 11 characters that make up the string.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 28 '08 #14
In comp.lang.c, Keith Thompson wrote:
Lew Pitcher <lp******@teksavvy.comwrites:
[...]
>A /string/ is a specific type of character array, which contains zero or
more non-zero char elements, followed by a single char element of zero
(\0). A string with 10 non-zero characters (i.e. "0123456789") is
contained in a character array of 11 elements ( i. e.
{'0','1','2','3','4','5','6','7','8','9',0} ), with the final element
{being
the \0 character.

Quibble: a string is a specific *kind* of character array. There is
no string *type* in C. (I know you didn't mean "type" in that sense.)
Agreed. You knew what I meant, but I should have phrased myself better. Your
phrasing is clearer than mine.
[...]
>A string of 10 characters (that is, strlen() returns 10) is stored in a
character array of 11 elements. The first 10 elements contain the
non-zero characters, and the 11'th element contains the terminating \0

Since a string includes its terminating '\0', it would be clearer to
say "A string of length 10" rather than "A string of 10 characters".
The '\0' is one of the 11 characters that make up the string.
Again, agreed.

Thanks for the clarifications :-)

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Jul 28 '08 #15
Keith Thompson <ks***@mib.orgwrites:
Lew Pitcher <lp******@teksavvy.comwrites:
[...]
>A /string/ is a specific type of character array, which contains zero or
more non-zero char elements, followed by a single char element of zero
(\0). A string with 10 non-zero characters (i.e. "0123456789") is contained
in a character array of 11 elements ( i. e.
{'0','1','2','3','4','5','6','7','8','9',0} ), with the final element being
the \0 character.

Quibble: a string is a specific *kind* of character array. There is
no string *type* in C. (I know you didn't mean "type" in that sense.)
It is a character array. Not a "kind" of anything.
>
[...]
>A string of 10 characters (that is, strlen() returns 10) is stored in a
character array of 11 elements. The first 10 elements contain the non-zero
characters, and the 11'th element contains the terminating \0

Since a string includes its terminating '\0', it would be clearer to
say "A string of length 10" rather than "A string of 10 characters".
The '\0' is one of the 11 characters that make up the string.
--
Jul 28 '08 #16
Richard<rg****@gmail.comwrites:
Keith Thompson <ks***@mib.orgwrites:
>Lew Pitcher <lp******@teksavvy.comwrites:
[...]
>>A /string/ is a specific type of character array, which contains zero or
more non-zero char elements, followed by a single char element of zero
(\0). A string with 10 non-zero characters (i.e. "0123456789") is contained
in a character array of 11 elements ( i. e.
{'0','1','2','3','4','5','6','7','8','9',0} ), with the final element being
the \0 character.

Quibble: a string is a specific *kind* of character array. There is
no string *type* in C. (I know you didn't mean "type" in that sense.)

It is a character array. Not a "kind" of anything.
[...]

I don't know what you mean; perhaps you can clarify.

All strings are character arrays. Not all character arrays are
strings. Specifically, a character array without a '\0' is not a
string. For that matter, a character array that contains a '\0' other
than in the final position is not a string, though it does contain
one.

I thought that saying that "a string is a specific *kind* of character
array" was a reasonable way to express that.

(I'm assuming here that a "contiguous sequence of characters",
referred to in the definition of "string" in C99 7.1.1p1, is an array.
I'm not certain that the two are entirely equivalent. But that
doesn't seem to be what you were disputing.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 28 '08 #17
On Jul 26, 8:20 am, mohi <mohangupt...@gmail.comwrote:
hello everyone ,
this one might seem wired but i dont know why iam confused with this
simple stuff

the problem is :

char name[10];

this defines a array of char with size 10 where 0-8 positions(count 9)
can be characters and name[9] should be '\0' .right?
Not quite. You can store a string of *up to* 9 non-zero characters
followed by a '\0'. For example, you can store the string "one" in
name (name[10] = {'o', 'n', 'e', '\0', ?, ?, ?, ?, ?, ?}, where '?'
can be any value, including 0). The size of the buffer is 10, the
*maximum* length of a string you can store in that buffer is 9, and
the *actual* length of the string in the buffer is 3.
but when i strcpy() a string of 10 characters (strlen()=10) into
name ,it works fine
and shows name[9]=a valid character from the string .
so where did the '\0' character of name[9] gone?????

and why is it not complaining???

thank you
mohan gupta
C does not do bounds checking; if you try to write more data than a
buffer is sized to hold, that extra data will be written to the memory
immediately following the buffer. In this case, the '\0' is stored in
the byte immediately following name[9]. For example, if your buffer
is at location 0x8000 and sized to hold 10 characters, and you write
100 characters to it, the extra 90 characters will be stored starting
at address 0x800A. Depending on whether you were keeping anything
important at 0x800A, this could be a problem.
Jul 29 '08 #18
John Bode <jf********@gmail.comwrites:
C does not do bounds checking;
C implementations are not _required_ to do bounds checking, but
likewise are not _prohibited_ from doing so.

mlp
Jul 29 '08 #19

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

Similar topics

12
by: Casper | last post by:
How would one go about summing up the memmory custom tree structures occupies in memmory? Example: struct node { struct node *parent; unsigned int nChildCount; string folder; //string class...
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
15
by: signuts | last post by:
I'm aware of what sizeof(...) does, what I would like to know is if sizeof(...) is compiled in or a function that's executed at run-time. Like for example { int a; printf("a is %d...
10
by: Trevor | last post by:
If I have a string that should be NULL terminated, is it good practice to use "sizeof(str)" or "sizeof(str) - 1" when using a function like strncpy? If I have a 'string' that should NOT be NULL...
21
by: sugaray | last post by:
hi, it just came up my mind that since we can get the length of any given string literal S with 'sizeof S-1', so, what's the merit of library function strlen()'s existence ? thanx in advance for...
16
by: Martin Roos | last post by:
hy ppl, i'm trying to create some multiplatformed software here and i'm very curious about the sizes of common variables on different machines. if you are running something different than a 32-bit...
15
by: stand__sure | last post by:
Having recently had a need to use ZeroMemory from the kernel32.dll, I "discovered" that there is apparently no .NET equivalent to the c/c++ sizeof operator -- one does exist in the Marshall...
2
by: bob | last post by:
sorry to double, triple post but testFile.txt has 28 character (what editor says anyhow): "nothing here, see I'm empty" how can I get the size of this 'buffer': FILE *filestream;...
40
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long...
27
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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
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,...
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...

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.