473,587 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there possible funky division going on here?

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@localho st ~]$ 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

Jun 19 '07 #1
1 1930
Chad <cd*****@gmail. comwrote:
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@localho st ~]$ 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
Jun 19 '07 #2

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

Similar topics

2
1909
by: Sebastian Haase | last post by:
Hi, I'm interested in having more people in our lab using numarray/NumPy instead of MatLab. For that I have put together a couple useful modules and written many myself. But then I got reminded of a new / upcoming feature of Python: "2/3 == 1.5" AKA PEP 238. I'm very interested in putting lots of "from __future__ import division" lines...
9
17237
by: Darius Fatakia | last post by:
hi, i'm new to MS Visual Studio and C++ but not to C programming in general. i'm trying to divide two integers and get their actual quotient (eg 5/3 = 1.666667 etc). i thought i had type cast correctly, but please let me know, because it appears to be rounding off. my code follows. Thanks! void DrawLine(GLint x1, GLint y1, GLint x2, GLint...
9
12736
by: Marcin | last post by:
How I can make division of two numbers placed in arrays, example: short int a = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2}; short int b = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2}; short int result = a / b; 'division without additional lib's and header files, only in standart C.
3
1925
by: John C Kirk | last post by:
I've come across an odd situation, where doing a floating point division produces different results for the same numbers. Basically, there are 4 ways to run this application: A) Debug build, inside the IDE B) Debug build, outside the IDE (e.g. launched from Explorer) C) Release build, inside the IDE D) Release build, outside the IDE ...
17
2404
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab replacement" and would generally like 5/2 ==2.5 So, I was contemplating to default all my modules/scripts to start with "from __future__ import...
13
2524
by: Alison Givens | last post by:
....... that nobody knows the answer. I can't imagine that I am the only one that uses parameters in CR. So, my question again: I have the following problem. (VB.NET 2003 with CR) I have a report with a multiple-value discrete value and a rangevalue. The report shows fine in the viewer, but when I hit the export to pdf
1
1189
by: tony | last post by:
Hello!! Below I have some methods that first setup a select string then read the dataset into a ArrayList. I have used the Data Adaper ConfigurationWizard for these adapter that I use here. I just wonder if it's possible to use inheritance and polymorfism or some other construction here to minimize that code.
13
7108
by: jamesonang | last post by:
Supposed unsigned int(32 bits) is the largest number that computer can represent with a single variable. Now, i have a big integer ( less than 64 bit, but great than 32 bit) . i represent it by this way: unsigned int dividend : divident store low 32 bits of big integer, dividend store high 32 bits of big integer.
0
1061
by: Protoman | last post by:
I wrote a DHM key exchange program that has something funky going on: the number that you have to send to your friend is completely wrong, but the session key it generates is correct. Wha? Code: DHMAlg.hpp ------------------------ #pragma once const long long& DHMAlg(long long& base, long long& mod, long long&
0
7918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.