LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-07-2006, 03:00 AM   #1
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 285

Rep: Reputation: 32
strlen anomaly


Hi
here is a code to check the strlen of some string without any data in it.


#include<stdio.h>

int main() {
char str[20];
prinrf("strlen(str)=%d",strlen(str));
}

result :
strlen(str)=3


Can anyone of u tell me the cause of this ? I think it's either system dependency or the way the linux represents this internally.
thanks in advance for ur interest,
barun
 
Old 08-07-2006, 03:18 AM   #2
exman
LQ Newbie
 
Registered: May 2006
Location: Germany, BS
Distribution: Debian Kernel 2.6.15, Kubuntu Dapper
Posts: 24

Rep: Reputation: 15
Hi,

I guess strlen function scans for the first appearance of 0 in the string which is the termination char.
Seemsthat the memory is filled with some old data which contains a 0 at str[3].
You can try to check for this by printing the string field by field converted to int.

exman
 
Old 08-07-2006, 06:03 AM   #3
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
I agree with exman. You should properly initialise your variable. Otherwise the result may be unexpected.
Also, this issue is not Linux specific. The same can happen on any platform.
When standard C allocates an array, the memory doesn't get null'ed (you need to use "memset" to do that).
So, it can contain any binary value really (depending on whatever the previous program put in that part of memory).
 
Old 08-07-2006, 11:32 AM   #4
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
Local variables are created by moving the stack/frame pointer only so local variables are never initialized (and could depend on the previous functions called or the previous return values other that int types)

I'm not an expert in C standards but the usual implementation of compilers is that automatic (global) variables are nulled :they are cells which are already present in your binary file, so it's possible to initialise them.

And strlen is included in string.h, include it otherwise you will end up with problems!

Quote:
#include <stdio.h>
#include <string.h>
char str2[20];

int main() {
char str[20];
printf("strlen(str)=%d",strlen(str));
printf("strlen(str2)=%d",strlen(str2));
return 0;
}
result:

strlen(str)=5strlen(str2)=0

Last edited by nx5000; 08-07-2006 at 11:33 AM.
 
Old 08-07-2006, 01:56 PM   #5
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
The string contains garbage, because it is uninitliased. Using an uninitialised variable is broken. As timmeke explained, you need to initialise it.

I'll say it again. The code is wrong. Broken. Defective. It has nothing to do with linux or gcc or any other software. It is incorrect use of the C language. So don't do it. Always initialise your variables.

Quote:
Originally Posted by nx5000
I'm not an expert in C standards but the usual implementation of compilers is that automatic (global) variables are nulled :they are cells which are already present in your binary file, so it's possible to initialise them.
Partly. Initialised global variables are zeroed at compile time and take up space in the binary. Uninitialised globals are zeroed at runtime, space is not allocated in the binary.
 
Old 08-10-2006, 08:09 AM   #6
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
Humm lets make it clear..

Quote:
Originally Posted by ioerror
The string contains garbage, because it is uninitliased. Using an uninitialised variable is broken. As timmeke explained, you need to initialise it.

I'll say it again. The code is wrong. Broken. Defective. It has nothing to do with linux or gcc or any other software. It is incorrect use of the C language. So don't do it. Always initialise your variables.
Partly. Initialised global variables are zeroed at compile time and take up space in the binary.
Its what I said, I just gave more details at what local variable contain and how they are created:

Local variables are created by moving the stack/frame pointer only so local variables are never initialized (and could depend on the previous functions called or the previous return values other that int types)


Quote:
Quote:
Originally Posted by nx5000
I'm not an expert in C standards but the usual implementation of compilers is that automatic (global) variables are nulled :they are cells which are already present in your binary file, so it's possible to initialise them.
Uninitialised globals are zeroed at runtime, space is not allocated in the binary.
Wrong. Initialising a variable has never made an increase in binary size!?
I took me 30seconds to make the test (read carefully I said for usual implementation not for C standard)
It doesn't even generate a warning. You have to initialise (compile or runtime) a variable before using it, different.

Regards
 
Old 08-10-2006, 02:16 PM   #7
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
Quote:
Originally Posted by nx5000
Its what I said, I just gave more details at what local variable contain and how they are created:
Uh, yes you did. Sorry, I just skimmed your post, didn't notice that bit.

Quote:
Originally Posted by nx5000
Local variables are created by moving the stack/frame pointer only so local variables are never initialized (and could depend on the previous functions called or the previous return values other that int types)
I know that.

Quote:
Originally Posted by nx5000
Wrong. Initialising a variable has never made an increase in binary size!?
You have to initialise (compile or runtime) a variable before using it, different.
Perhaps I was unclear. I do that sometimes.

Code:
...
int i = 1;
...
Initialised variable: this is a compile time initialisation and necessarily consumes space in the binary.

Code:
...
int i;
...
Uninitialised variable: i is zeroed at runtime before main is called. No space is consumed in the binary as it's unnecessary (no point bloating the binary with irrelevant zeroes).

In both cases, it is assumed that i is global.

That's all I meant. I was simply responding to your previous statement:

Quote:
(global) variables are nulled :they are cells which are already present in your binary file,
which is true for initialised variables, but uninitialised variables are zeroed at runtime.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CUPS anomaly barunparichha Linux - Software 0 07-03-2006 02:58 AM
strlen() function problem in C++ sajith Programming 9 01-16-2006 10:14 PM
strlen problem salahuddin_66 Programming 2 11-13-2005 05:21 AM
C++ question: strlen error!? Hady Programming 2 03-16-2005 06:08 AM
confused about strlen() mcd Programming 19 02-26-2005 09:04 PM


All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration