Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is there possible funky division going on here?

Reply
Thread Tools

Is there possible funky division going on here?

 
 
Chad
Guest
Posts: n/a
 
      06-19-2007
Given something like
#include <stdio.h>
#include <string.h>

int main(void)
{
char name[] = "chad";
int number = 2;
int val;

size_t len = strlen(name);

val = len/number ;

return 0;
}

[cdalten@localhost ~]$ gcc -g -Wall -ansi -pedantic div.c -o div

What if name[] is something really really long. Long enough to fill up
size_t in len. Wouldn't this value get truncated since val is int? And
if it does possibly get truncated, how come my compiler doesn't say
anyting?

Chad

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      06-19-2007
Chad <> wrote:

> Given something like
> #include <stdio.h>
> #include <string.h>
>
> int main(void)
> {
> char name[] = "chad";
> int number = 2;
> int val;
>
> size_t len = strlen(name);
>
> val = len/number ;
>
> return 0;
> }
>
> [cdalten@localhost ~]$ gcc -g -Wall -ansi -pedantic div.c -o div
>
> What if name[] is something really really long. Long enough to fill up
> size_t in len. Wouldn't this value get truncated since val is int?


Possibly, depending on the sizes of size_t and int. If size_t is at
least as large as int, the division is done unsigned. If it's exactly as
large as int, the result of dividing (what is in effect) any unsigned
int value by two is guaranteed to fit in a signed int.

> And if it does possibly get truncated, how come my compiler doesn't say
> anyting?


Who knows the motivations of the Ganuck implementors? Perhaps they do
static analysis on your code, and decide that in this case, strlen(name)
is always 4. Perhaps you need to crank up the optimisation level;
perhaps down.

Richard
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
division by 7 without using division operator krypto.wizard@gmail.com C Programming 94 02-09-2007 06:57 AM
Funky P-t-P T1, SBC says I have a timing problem, I say BS... Doug Cisco 4 07-25-2004 02:14 AM
Address Book is Funky Peter Arnold Firefox 1 07-13-2004 12:57 PM
DNS funky when using Cisco IOS w/ IPFW Brian Bergin Cisco 0 02-02-2004 01:06 PM
Funky error Situation Diogenes Dilone ASP .Net 3 01-07-2004 04:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57