473,573 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

size_t problems

I am trying to compile as much code in 64 bit mode as
possible to test the 64 bit version of lcc-win.

The problem appears now that size_t is now 64 bits.

Fine. It has to be since there are objects that are more than 4GB
long.

The problem is, when you have in thousands of places

int s;

// ...
s = strlen(str) ;

Since strlen returns a size_t, we have a 64 bit result being
assigned to a 32 bit int.

This can be correct, and in 99.999999999999 9999999999999%
of the cases the string will be smaller than 2GB...

Now the problem:

Since I warn each time a narrowing conversion is done (since
that could loose data) I end up with hundreds of warnings each time
a construct like int a = strlen(...) appears. This clutters
everything, and important warnings go lost.
I do not know how to get out of this problem. Maybe any of you has
a good idea? How do you solve this when porting to 64 bits?

jacob
Aug 29 '07
409 10853
On Aug 30, 2:40 am, CBFalconer <cbfalco...@yah oo.comwrote:
Richard Heathfield wrote:
Ian Collins said:
jacob navia wrote:
<snip>
>int s = strlen(str) is NOT broken.
Why would you want to assign an unsigned value to an int? Why do
you think it makes sense to have a negative size?
Well, obviously it doesn't make any sense at all, and assigning
strlen's result to an int is clearly wrong; strlen yields size_t,
not int.
On the other hand, does it really make sense to play with trolls?

Now that is not fair. Yes, Jacob has peculiar (and many are
unsound) ideas, but that does not make him a troll. He seems to
have co-operated on advertising his compiler, for example, without
specifically acknowledgeing so doing.
There have been other posters who (over time) became more and more
sensible.

In fact, that should happen to all of us, and it should be an ongoing
process (never coming to completion because none of us are perfect).

Now, I sometimes disagree with Jacob, but I think he makes an honest
attempt to communicate most of the time. Though he may often be
'contrary' I would not label him as a troll.

For my way of thinking, a troll has no interest in information. He
just wants to do something controversial and then sit back and laugh
at the reactions. Either that, or they are mentally ill and are
incapable of intelligent exchange of information. In my opinion,
Jacob Navia is not of either sort.

Aug 30 '07 #81
On Aug 30, 9:14 am, Ed Jensen <ejen...@visi.c omwrote:
Richard Heathfield <r...@see.sig.i nvalidwrote:
It is also possible to steer clear of problems. The "chain reaction"
simply doesn't happen if everything has the right type to start off
with. And if it doesn't, the chain reaction is a good thing, not a bad
thing, because it reveals type misconceptions in the code.

Just out of curiosity, what was the rationale behind having strlen()
return size_t instead of "int" or "unsigned int" in the first place?
The result of a sizeof () operator is a size_t.

Can you imagine any object which has a negative size?

A size_t can describe the size of any object allowed by the C
language. An int cannot.

What if objects larger than unsigned are allowed by a compiler
implementation?
It is not unlikely that int and unsigned int are 32 bits on some
implementation on 64 bit hardware.
Would you want to be able to allocate an array of 20 GB if you needed
it? In such an instance, an unsigned integer would not work but a
size_t could work.

Can you imagine a better thing to return than a size_t?

Summary:
An int is a defective return from anything that describes the size of
an object.
An unsigned is a defective return from anything that describes the
size of an object.
A size_t is the perfect return from anything that describes the size
of an object.
I try to always use size_t both for object dimentions and array
addressing (though I admit I am not totally rigorous about it).

Aug 30 '07 #82
In article <46************ ***@spamcop.net >,
Kenneth Brody <ke******@spamc op.netwrote:
>What happens if sizeof(char *) doesn't fit in an int?
Then you should get a different computer.
>(Okay, I don't
expect any real world system to exist for which that is true. But,
does the standard say that sizeof must fit in an int? And if so, why
return a size_t?)
No, and it commonly doesn't. But not all programs have to handle
objects that big.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 30 '07 #83
user923005 <dc*****@connx. comwrote:
An int is a defective return from anything that describes the size of
an object.
And yet, other programming languages get by -- somehow -- by returning
an integer when asked for the length of a string.
An unsigned is a defective return from anything that describes the
size of an object.
And yet, other programming languages get by -- somehow -- even though
they don't even have unsigned integer types.
A size_t is the perfect return from anything that describes the size
of an object.
I recognize and understand why the range of C types are defined the
way they're defined, but that doesn't minimize the pain when trying to
write 100% portable code.
Aug 30 '07 #84
Richard Heathfield wrote:
Bart said:
>If I have a container that can hold 0 to 12 eggs and another that
can hold (use some imagination) -6 to 6 eggs, it's easy enough to
compare say 4 in one with +3 in the other (the first will make a
bigger omelette).

Let's just say I'm suffering from imaginatrophy, shall we? The
problem here is not the negative value itself, but the comparison
between a value of a type that can store negative values and a value
of a type that cannot. These are fundamentally different concepts.
However we can compare the "count of items in the bins",
eliminating the dependance on what the items actually are. This
corresponds to casting in some form or another, and carries its own
dangers.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 30 '07 #85
Richard Tobin wrote:
Ian Collins <ia******@hotma il.comwrote:
>>int s = strlen(str) is NOT broken.
>Why would you want to assign an unsigned value to an int? Why
do you think it makes sense to have a negative size?

There are lots of reasons to assign an unsigned value to an int.
Most notably, so that you can do integer arithmetic on it. Do
you really want all array subscript variables to be size_t? If
so, you'll have to consider casting them if, say, you want to
subtract one from another.
Not if you don't get a negative result. You can easily avoid this
by applying the complex test "if (szt1 szt2) ... else ...".

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 30 '07 #86
On Aug 30, 2:15 pm, Ed Jensen <ejen...@visi.c omwrote:
user923005 <dcor...@connx. comwrote:
An int is a defective return from anything that describes the size of
an object.

And yet, other programming languages get by -- somehow -- by returning
an integer when asked for the length of a string.
Can those same languages create objects with a size to large to be
held in an integer?
If 'yes', then those languages are defective. If 'no', then integer
is the correct return.
An unsigned is a defective return from anything that describes the
size of an object.

And yet, other programming languages get by -- somehow -- even though
they don't even have unsigned integer types.
I can create a language with a single type. Somehow, I think it will
be less effective than C for programming tasks.
A size_t is the perfect return from anything that describes the size
of an object.

I recognize and understand why the range of C types are defined the
way they're defined, but that doesn't minimize the pain when trying to
write 100% portable code.
The way to minimize the pain of writing 100% portable code is to write
it correctly, according to the language standard. For instance, that
would include using size_t for object sizes. Now, pre-ANSI C did not
have size_t. So that code will require effort to repair.
Aug 30 '07 #87
Ed Jensen <ej*****@visi.c omwrites:
user923005 <dc*****@connx. comwrote:
>An int is a defective return from anything that describes the size of
an object.

And yet, other programming languages get by -- somehow -- by returning
an integer when asked for the length of a string.
>An unsigned is a defective return from anything that describes the
size of an object.

And yet, other programming languages get by -- somehow -- even though
they don't even have unsigned integer types.
What programming languages are you thinking of here?
--
"Am I missing something?"
--Dan Pop
Aug 30 '07 #88
On Aug 30, 5:42 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Bart said:
If I have a container that can hold 0 to 12 eggs
and another that can hold (use some imagination) -6 to 6 eggs, it's
easy enough to compare say 4 in one with +3 in the other (the first
will make a bigger omelette).

Let's just say I'm suffering from imaginatrophy, shall we? The problem
here is not the negative value itself, but the comparison between a
value of a type that can store negative values and a value of a type
that cannot. These are fundamentally different concepts.
Possibly, but comparing two such values is still meaningful. Try
adding +6 to the numbers in my little example and both values must now
be positive and can be compared a little more easily. There's still a
question of overflow but that's a different problem.

Signed/unsigned numbers have different ranges. Why is it a big deal to
compare these two types of values? Is it because one type can store a
value that does not exist in the other? That's also a problem with
short and long ints. Anyway the solution can be simple, such as
converting the numbers into a type that accommodates both ranges.

Bart C

Aug 30 '07 #89
user923005 wrote:
>
.... snip ...
>
A size_t can describe the size of any object allowed by the C
language. An int cannot.
In the interests of accuracy, a size_t is guaranteed to describe
those sizes. An int is not guaranteed to have similar capability,
however it may in some systems.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 30 '07 #90

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

Similar topics

18
23550
by: Steffen Fiksdal | last post by:
Can somebody please give me some rules of thumb about when I should be using size_t instead of for example int ? Is size_t *always* typedef'd as the largest unsigned integral type on all systems ? I know that the strlen() functions and such returns a size_t, and it should be received that way. I am at the time creating some sort of...
17
2789
by: G Patel | last post by:
E. Robert Tisdale wrote: > > int main(int argc, char* argv) { > quad_t m = {0, 1, 2, 3}; > int r; > fprintf(stdout, "m = ("); > for (size_t j = 0; j < 4; ++j) Why did you declare j as type size_t ?
17
134417
by: candy_init | last post by:
I sometimes comes across statements which invloves the use of size_t.But I dont know exactly that what is the meaning of size_t.What I know about it is that it is used to hide the platform details.I tried to find its meaning in the header files but did'nt got a good answer.So can somebody please tell me that what is the meaning of size_t and...
5
3157
by: edware | last post by:
Hello, I have some questions about the size_t type. First, what do we know about size_t? From what I have read I believe that it is an unsigned integer, but not necessarily an int. Am I correct? Does this mean that if I need to compare two variables, one of the size_t type, should the other also be a size_t variable? There could be...
12
10691
by: Alex Vinokur | last post by:
Why was the size_t type defined in compilers in addition to unsigned int/long? When/why should one use size_t? Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
23
4865
by: bwaichu | last post by:
To avoid padding in structures, where is the best place to put size_t variables? According the faq question 2.12 (http://c-faq.com/struct/padding.html), it says: "If you're worried about wasted space, you can minimize the effects of padding by ordering the members of a structure based on their base types, from largest to smallest."
318
12837
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people started to try to find possible errors, a harmless passtime they seem to enjoy. One of their remarks was that I used "int" instead of "size_t"...
73
7379
by: Yevgen Muntyan | last post by:
Hey, I was reading C99 Rationale, and it has the following two QUIET CHANGE paragraphs: 6.5.3.4: "With the introduction of the long long and extended integer types, the sizeof operator may yield a value that exceeds the range of an unsigned long." 6.5.6: "With the introduction of the long long and extended integer
89
5676
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the right thing to use for every var that holds the number of or size in bytes of things. * size_t should only be used when dealing with library functions.
0
7784
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
8031
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7795
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8073
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
3734
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3735
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2223
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1309
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1044
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.