Is there a standard way to determine the max. value of size_t as a
compile-time constant? Will:
#define SIZE_T_MAX ((size_t) -1)
work in all cases, or just on 2s comp. machines?
DrX 16 6123
Xenos wrote: Is there a standard way to determine the max. value of size_t as a compile-time constant? Will:
#define SIZE_T_MAX ((size_t) -1)
work in all cases, or just on 2s comp. machines?
It will work in all cases. Casts work on values, not representations .
For what it's worth, C99 defines a SIZE_MAX macro whose value is the
maximum value of size_t.
Jeremy.
"Xenos" <do**********@s pamhate.com> wrote in message
news:c5******** *@cui1.lmms.lmc o.com... Is there a standard way to determine the max. value of size_t as a compile-time constant? Will:
#define SIZE_T_MAX ((size_t) -1)
work in all cases, or just on 2s comp. machines?
That's how I understand it. -1, converted to an unsigned type, gives
the maximum value for that type regardless of representation.
Peter
On Fri, 9 Apr 2004 11:17:50 -0400, "Xenos" <do**********@s pamhate.com>
wrote: Is there a standard way to determine the max. value of size_t as a compile-time constant? Will:
#define SIZE_T_MAX ((size_t) -1)
work in all cases, or just on 2s comp. machines?
DrX
size_t is a typdef for some unsigned integer type. While ((size_t)-1)
will produce the max value that can be stored in that type, there is
no guarantee that any function or operator returning a size_t will
ever produce a value that large. The system is free to limit the
value of a size_t to a lesser value as long as it satisfies the
definition in the standard.
So the return question is do you mean the max value that will fit in a
size_t variable or the largest size of any object per the definition?
<<Remove the del for email>>
In <c5**********@2 16.39.134.6> Barry Schwarz <sc******@deloz .net> writes: On Fri, 9 Apr 2004 11:17:50 -0400, "Xenos" <do**********@s pamhate.com> wrote:
Is there a standard way to determine the max. value of size_t as a compile-time constant? Will:
#define SIZE_T_MAX ((size_t) -1)
work in all cases, or just on 2s comp. machines?
DrX size_t is a typdef for some unsigned integer type. While ((size_t)-1) will produce the max value that can be stored in that type, there is no guarantee that any function or operator returning a size_t will ever produce a value that large. The system is free to limit the value of a size_t to a lesser value as long as it satisfies the definition in the standard.
So the return question is do you mean the max value that will fit in a size_t variable or the largest size of any object per the definition?
The largest size of any object doesn't make much sense, except on
single user, single tasking platforms, as it can change during the
program's execution, due to other activity on the same system. And the
systems where it makes sense, usually provide it, via an extension.
What really matters is not to exceed (size_t)-1 in an argument to a
function expecting a size_t parameter, because this leads to programming
errors. Consider malloc(100000) on a system with (size_t)-1 == 65535.
The call may succeed, but it won't allocate the expected amount of memory.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de Da*****@cern.ch (Dan Pop) wrote in message news:<c5******* ****@sunnews.ce rn.ch>... In <c5**********@2 16.39.134.6> Barry Schwarz <sc******@deloz .net> writes:
On Fri, 9 Apr 2004 11:17:50 -0400, "Xenos" <do**********@s pamhate.com> wrote:
Is there a standard way to determine the max. value of size_t as a compile-time constant? Will:
#define SIZE_T_MAX ((size_t) -1)
work in all cases, or just on 2s comp. machines?
DrX size_t is a typdef for some unsigned integer type. While ((size_t)-1) will produce the max value that can be stored in that type, there is no guarantee that any function or operator returning a size_t will ever produce a value that large. The system is free to limit the value of a size_t to a lesser value as long as it satisfies the definition in the standard.
So the return question is do you mean the max value that will fit in a size_t variable or the largest size of any object per the definition?
The largest size of any object doesn't make much sense, except on single user, single tasking platforms, as it can change during the program's execution, due to other activity on the same system. And the systems where it makes sense, usually provide it, via an extension.
What really matters is not to exceed (size_t)-1 in an argument to a function expecting a size_t parameter, because this leads to programming errors. Consider malloc(100000) on a system with (size_t)-1 == 65535. The call may succeed, but it won't allocate the expected amount of memory.
Dan
I tried this macro on gcc and on Microsoft Visual Studio C compiler,
the result of (size_t) -1 is still -1. The size_t is typedef as
unsigned int.
However, result of (unsigned char)-1, (unsigned short)-1 are both
correct.
Only (unsigned int) -1 still returns -1. Why is that?
Thanks.
In article <f1************ *************@p osting.google.c om>, le********@yaho o.com (Leon) wrote: I tried this macro on gcc and on Microsoft Visual Studio C compiler, the result of (size_t) -1 is still -1. The size_t is typedef as unsigned int.
However, result of (unsigned char)-1, (unsigned short)-1 are both correct.
Only (unsigned int) -1 still returns -1. Why is that?
How exactly did you figure out that "the result of (size_t) -1 is still
-1" ? I assure you that it is not; for starters, the types of both
expressions are different; one is of type size_t, the other is of type
int.
Christian Bau wrote: In article <f1************ *************@p osting.google.c om>, le********@yaho o.com (Leon) wrote:
I tried this macro on gcc and on Microsoft Visual Studio C compiler, the result of (size_t) -1 is still -1. The size_t is typedef as unsigned int.
However, result of (unsigned char)-1, (unsigned short)-1 are both correct.
Only (unsigned int) -1 still returns -1. Why is that? How exactly did you figure out that "the result of (size_t) -1 is still -1" ?
According to my crystal ball, he used the expression,
((size_t)-1 == -1)
I assure you that it is not; for starters, the types of both expressions are different; one is of type size_t, the other is of type int.
.... and he didn't realise that the -1 int value,
was getting converted to UINT_MAX.
--
pete
Leon wrote:
.... snip ... I tried this macro on gcc and on Microsoft Visual Studio C compiler, the result of (size_t) -1 is still -1. The size_t is typedef as unsigned int.
However, result of (unsigned char)-1, (unsigned short)-1 are both correct.
Only (unsigned int) -1 still returns -1. Why is that?
How did you show that result?
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
pete <pf*****@mindsp ring.com> wrote in message news:<40******* ****@mindspring .com>... Christian Bau wrote: In article <f1************ *************@p osting.google.c om>, le********@yaho o.com (Leon) wrote:
I tried this macro on gcc and on Microsoft Visual Studio C compiler, the result of (size_t) -1 is still -1. The size_t is typedef as unsigned int.
However, result of (unsigned char)-1, (unsigned short)-1 are both correct.
Only (unsigned int) -1 still returns -1. Why is that?
How exactly did you figure out that "the result of (size_t) -1 is still -1" ?
According to my crystal ball, he used the expression, ((size_t)-1 == -1)
I assure you that it is not; for starters, the types of both expressions are different; one is of type size_t, the other is of type int.
... and he didn't realise that the -1 int value, was getting converted to UINT_MAX.
Here is what I did,
#include <stdio.h>
#define SIZE_T_MAX ((size_t) -1)
#define UCHAR_MAX ((unsigned char) -1)
#define USHORT_MAX ((unsigned short) -1)
#define UINT_MAX ((unsigned int) -1)
int main()
{
printf("SIZE_T_ MAX %d\n", SIZE_T_MAX);
printf("UCHAR_M AX %d\n", UCHAR_MAX);
printf("USHORT_ MAX %d\n", USHORT_MAX);
printf("UINT_MA X %d\n", UINT_MAX);
}
and thet result is :
SIZE_T_MAX -1
UCHAR_MAX 255
USHORT_MAX 65535
UINT_MAX -1
I know that (char)-1 is represented as 0xFF as two's complement. so it
doesn't surprise me that (unsigned char)-1 is 255. However, shouldn't
(int)-1 be represented as 0xFFFFFFFF in and thus have a unsigned value
of 2^32 - 1? Basically,I don;t understand why SIZE_T_MAX and UINT_MAX
evaluates to -1.
can you explain the result of my code? Thanks a lot! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: hanzhou.zhang |
last post by:
Hi,
vector<int> v;
//...
int size = v.size();
Since the vector's size() function returns a "size_t" number, the
compilation with /Wp64 reports some warning. My question is: If I can
gurantee "size" is not a big number, will the conversion from "size_t"
to "int" on 64-bit platforms cause error ?
|
by: javadesigner |
last post by:
Hi:
I am a bit new to C programming and am trying to
write a wrapper around malloc. Since malloc takes
size_t as it's parameter, I want to be able to
see if my function recieved an argument less than
or equal to the max size_t type.
So:
|
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 cryptolibrary. Should all my
|
by: Roka100 |
last post by:
Hi,
I am using size_t and ssize_t . But I am confused about them.
<ssize_t>
typedef int __ssize_t;
typedef __ssize_t ssize_t;
<size_t >
typedef unsigned int size_t;
|
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 problems if I compare it with an
int, if those have different sizes, right?
| |
by: Mark Odell |
last post by:
I've always declared variables used as indexes into arrays to be of
type 'size_t'. I have had it brought to my attention, recently, that
size_t is used to indicate "a count of bytes" and that using it
otherwise is confusing. I also thought that size_t could be signed but
it seems I was wrong on that one.
So if you were to see code iterating through a table of Foo objects
using an index of size_t type, would it be confusing? Should I have...
|
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" for the input of my allocator function.
|
by: subramanian100in |
last post by:
Consider the following program
#include <limits.h>
#include <stddef.h>
int main(void)
{
size_t size;
size_t bytes = sizeof(size_t);
|
by: jacob navia |
last post by:
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
|
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.
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |