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

problem with strlen

Expand|Select|Wrap|Line Numbers
  1. void show1(char *s) {
  2.   int i;
  3.   for (i = 0; i < strlen(s); i++) printf("%c", s[i]);
  4.   printf("\n");
  5. }
  6.  
  7. void show2(char *s) {
  8.   int i;
  9.   for (i = 0; i <= strlen(s) - 1; i++) printf("%c", s[i]);
  10.   printf("\n");
  11. }
When I do show1(""), it has no problem. But show2("") gives me a error. Anyone know why?
Sep 14 '10 #1
4 2979
Markus
6,050 Expert 4TB
The string length (strlen) of "" is 0 (strlen = 0)
strlen - 1 = -1
s[-1] is accessing data to which your not permitted access. This is a fundamental part of working with memory in C.
Sep 14 '10 #2
Banfa
9,065 Expert Mod 8TB
Close Markus, it never tries to access s[-1], although you are right about the condition causing the problem.

strlen(s) - 1 for s == "" == -1

Therefore the condition is i <= -1

Since i is initialised to 0 on a 32 bit system that allows i to have the range 0 - 2147483647 until i final wraps and causes undefined behaviour (for wrapping a signed integer).

However since s has length 0 as soon as i >= 1 you are accessing memory outside the bounds of the array and at some point in the 2147483646 out of bounds addresses it tries I expect it hits something that really causes a problem.


For this reason if you have a size (especially if it can be 0) for something SIZE and are range testing you should always use

range < SIZE

and not

range <= SIZE - 1

I have seen this in a production product cause the same problem.
Sep 14 '10 #3
donbock
2,426 Expert 2GB
What error do you get for show2?

Banfa: if strlen(s)-1 evaluated to -1 then execution would skip over the loop. There would be no output or run-time error.

strlen returns size_t, which is typically an unsigned type.

Is the expression (strlen(s) - 1) evaluated for type size_t or type int? If size_t, then -1 is considered to be a very large positive number that is too large to fit in an int (for a 2's-complement architecture). If int, then the result is truly -1.

The comparison of i to the expression is performed using int arithmetic. If the expression is of type size_t then the very large positive number is converted to int. The result of this conversion may be a positive number or a negative number, depending on implemention-dependent properties of the compiler.

At least that's what I think might be going on.
Sep 14 '10 #4
Markus
6,050 Expert 4TB
Of course, now I see it.

Mark (shouldn't rush to answer things).
Sep 14 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: bArT | last post by:
Hi! I have a problem with such situation. I have a class like below and have some pointers (ptr1 and ptr2). I dynamically allocate memory in constructor and I free in destructor. For one pointer...
5
by: cpptutor2000 | last post by:
I am compiling and running the following code snippet on a Linux box - I am really puzzled by the answers. Could someone please tell me what might be wrong? void test(){ int m = 0; int n = 0;...
15
by: Materialised | last post by:
I am having a problem with the following functions, I've been racking my brains trying to figure out where I am going wrong. What I need to do is return a formatted string with the current date and...
9
by: No Such Luck | last post by:
I have a function which requires me to loop from the end of a string to the beginning on a char by char basis: int foo (char string) { unsigned int i; for(i = strlen(string); i >= 0; i--) {...
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
2
by: Chris | last post by:
Hi there, I have been reading in this group for a while really enjoy this pool of infinite c wisdom. :) Anyway, I have question now. I have this little piece of code that is giving me a...
53
by: ¬a\\/b | last post by:
strlen is wrong because can not report if there is some error e.g. char *a; and "a" point to an array of size=size_t max that has no 0 in it
1
by: radicaled | last post by:
Hi, I've been working in a program for the school that use a recursive call to the function. The problem is that I have to use that function almost 86000 times, because I'm reading a dicctionary. ...
8
by: Martin the Third | last post by:
Hi, I need some help! I'm writing an infinite-precision floating point library called ipfloat (I know infinite is a misnomer - but arbitrary was taken). A quick overview: I'm storing numbers as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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
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...
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
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.