473,386 Members | 1,803 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,386 software developers and data experts.

Strange problem on GCC - PLEASE HELP!!

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;
int i = 0;
int j = 0;

double start = 0.0;
double end = 0.0;
double gap = 0.0;
struct timeval tp;

aes_context ctx;

unsigned char buf[16];

unsigned char key[32];

for(i = 0; i < 16; i++){ buf[i] = 'a'; }

for(j = 0; j < 32; j++){ key[j] = '9'; }

printf("%d, %d\n", strlen(buf), strlen(key));

/* Some more code omitted */
}

The print statements provide the answers 16, 48
Could someone please provide some hint as to what might be the problem?

Nov 14 '05 #1
5 1538
cp**********@yahoo.com wrote:
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;
int i = 0;
int j = 0;

double start = 0.0;
double end = 0.0;
double gap = 0.0;
struct timeval tp;

aes_context ctx;

unsigned char buf[16];

unsigned char key[32];

for(i = 0; i < 16; i++){ buf[i] = 'a'; }

for(j = 0; j < 32; j++){ key[j] = '9'; }

printf("%d, %d\n", strlen(buf), strlen(key));
1. strlen() returns values of the type size_t; size_t is an unsigned
integer type and not necessarily the same as unsigned int.
With C89, use
printf("%lu\n", (unsigned long)strlen(somestring));
with C99, you have a separate length modifier for size_t:
printf("%zu\n", strlen(somestring));
2. Valid C strings are _always_ terminated by a character with the
value 0, often written as '\0' and referred to as string terminator.
Your arrays of char do not contain C strings. In order to do so, one
array element in the range 0 .. sizeof buf -1 (or sizeof key -1,
respectively) needs to be zero.
Otherwise, strlen() keeps probably reading through the memory until
it encounters a byte of value 0. Why probably? Because the behaviour
is undefined. It is also possible that the program dies with a
segmentation fault or that your computer crashes.

/* Some more code omitted */
}

The print statements provide the answers 16, 48
Could someone please provide some hint as to what might be the problem?


In your case, it is possible that the first byte after the storage
reserved for buf is '\0', so you get sizeof buf. Moreover, it could
be that key is stored right before buf, so you have sizeof key plus
sizeof buf until '\0' is encountered. This is highly speculative.

To get you started:

#define SOMELEN (16)
.....
char mybuf[SOMELEN+1];
for (i=0; i<SOMELEN; i++)
mybuf[i] = '.';
mybuf[SOMELEN] = '\0';
printf("String length is %scorrect\n", strlen(mybuf)==SOMELEN
? "":"not ");

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2
unsigned char buf[16];

unsigned char key[32];


Try

unsigned char buf[16] = {};
unsigned char key[32] = {};

to initialize them to all \0s

so the strings will actually end (a string for use with the
standard libraries (calls such as strlen, printf, etc) ends at some \0)

as long as you keep some \0

and go from there

-Mysid

Nov 14 '05 #3
On 11 Feb 2005 14:58:53 -0800, cp**********@yahoo.com wrote in
comp.lang.c:
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;
int i = 0;
int j = 0;

double start = 0.0;
double end = 0.0;
double gap = 0.0;
struct timeval tp;

aes_context ctx;

unsigned char buf[16];

unsigned char key[32];

for(i = 0; i < 16; i++){ buf[i] = 'a'; }

for(j = 0; j < 32; j++){ key[j] = '9'; }

printf("%d, %d\n", strlen(buf), strlen(key));
Obviously you don't have a prototype for strlen() in scope, usually
done by including <string.h>, or you are operating your compiler in a
broken (non conforming) mode. Because the argument to strlen() must
be a pointer to char, and you are passing pointer to unsigned char.
No automatic conversion is possible, so each call to strlen() would be
a constraint violation.

Actually calling strlen() with a pointer to unsigned char produces
undefined behavior.

Even if you change buf and key to arrays of char, instead of unsigned
char, they are not strings, just arrays. Calling strlen() with a
pointer to char that is not a pointer to a string also produces
undefined behavior.
/* Some more code omitted */
}

The print statements provide the answers 16, 48
Could someone please provide some hint as to what might be the problem?


So...

1. Turn up the warning level on your compiler.

2. Invoke it in standard conforming mode.

3. Include <string.h> before calling strlen().

4. Do not pass pointers to unsigned char to strlen().

5. Do not pass pointers to character arrays that are not strings to
strlen().

Then your problem will go away.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
I would suggest to make some modifications in your code as bellow

unsigned char buf[17]; //array bound 17 to include '\0' string
termination character.
unsigned char key[33]; //Same justification as above.
for(i = 0; i < 16; i++){ buf[i] = 'a'; }
for(j = 0; j < 32; j++){ key[j] = '9'; }

buf[i] = '\0'; /*This is required as string terminates
with '\0' as you know*/
key[j] = '\0';

BTW just modifiying the code to unsigned char buf[17] = {}; will not
work because = {}; does not implicitely put '\0' at all positions of
array.

Nov 14 '05 #5
Mysidia wrote:
Try

unsigned char buf[16] = {};
unsigned char buf[16] = { 0 };
unsigned char key[32] = {};
unsigned char key[32] = { 0 };
to initialize them to all \0s

There is no such thing as an empty initializer.
Christian
Nov 14 '05 #6

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

Similar topics

2
by: leegold2 | last post by:
How do I use rawurlencode()? A snippet would extremely appreciated. I read I should use it twice for plus signs(?) - I need help! Thanks. Very strange stuff happens when I use GET to pass a...
5
by: brulsmurf | last post by:
Ok iam despered, in Main of this code the object 'two' is looking as should be when i print it, but the exact same code in procedure "duTest" makes the object filled up with additional strange...
3
by: Bill C. | last post by:
Hi, I've got a simple console app that just reads an XML file into a DataSet then prints out a description of each table in the DataSet, including column names and row values for each column. ...
3
by: Mitchell Thomas | last post by:
I hope someone out there can solve my mysterious problem. I have tried everything imaginable, even paid $35 to Microsoft to help me, but they were not able to figure out this problem: Here is the...
0
by: unknown | last post by:
Hi, I am developing an online book store with shopping cart. My shopping cart is represented as a Xml server control and I am using an XSLT to render it at the client side. I am using an...
1
by: Martin Feuersteiner | last post by:
Dear Group I'm having a very weird problem. Any hints are greatly appreciated. I'm returning two values from a MS SQL Server 2000 stored procedure to my Webapplication and store them in...
0
by: David Pratt | last post by:
Hi. I am creating a couple of small methods to help me manage time from UTC as standard but I am getting strange results. If I start with a datetime of 2005-12-12 14:30:00 in timezone...
11
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
14
by: blumen | last post by:
Hi all, I'm a newbie in VB.Net Programming.. Hope that some of you can help me to solve this.. I'm working out to read,parse and save textfile into SQL Server. The textfile contains thousands...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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...

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.