Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 303
Rep:
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
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.
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).
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!
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.