473,651 Members | 3,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding max value of size_t

Consider the following program

#include <limits.h>
#include <stddef.h>

int main(void)
{
size_t size;
size_t bytes = sizeof(size_t);

if (bytes == sizeof(unsigned int))
size = UINT_MAX;
else if (bytes == sizeof(unsigned long))
size = ULONG_MAX;
else if (bytes == sizeof(unsigned long long))
size = ULLONG_MAX;

return 0;
}

Can this code give the maximum value of size_t in all standard C
compliant compilers.

Mar 9 '07 #1
22 27045
su************* *@yahoo.com, India said:
Consider the following program

#include <limits.h>
#include <stddef.h>

int main(void)
{
size_t size;
size_t bytes = sizeof(size_t);

if (bytes == sizeof(unsigned int))
size = UINT_MAX;
else if (bytes == sizeof(unsigned long))
size = ULONG_MAX;
else if (bytes == sizeof(unsigned long long))
size = ULLONG_MAX;

return 0;
}

Can this code give the maximum value of size_t in all standard C
compliant compilers.
No, because there is no guarantee that size_t has the same size as any
other integer type. But you can do this:

#include <stddef.h>

int main(void)
{
size_t size = (size_t)-1;
return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 9 '07 #2
(size_t) - 1

Please let me know whether this will give the maximum value of size_t

Mar 9 '07 #3
su************* *@yahoo.com, India said:
(size_t) - 1

Please let me know whether this will give the maximum value of size_t
Didn't I just do that? Yes, it will.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 9 '07 #4
Yes. It does give the expected value. Thanks.

The following is a beginner's question.

What is (size_t) ? ie what is parenthesized size_t ? how does it
produce a value ?

Please explain
Mar 9 '07 #5
su************* *@yahoo.com, India wrote:
Yes. It does give the expected value. Thanks.

The following is a beginner's question.

What is (size_t) ? ie what is parenthesized size_t ?
Your C book should tell you. (If you haven't got one, get
one; you'll make progress faster than by being drip-fed via
netnews. See the FAQ for suggestions.)

It's a type-name in brackets.

In the expression `(size_t) -1`, it's a /cast/, requesting
a conversion of the expression `-1` to the type `size_t`.

[Note that `-1` is an expression, not a literal, consisting
of the unary minus operator applied to the literal `1`.
It happens to be a /constant expression/, meaning that the
compiler can -- and in some circumstances [1], must -- evaluate
it at compile-time.]
how does it produce a value ?
It converts the value -1 to the guaranteed-unsigned type
`size_t` by adding/subtracting one more than the maximum
value of that type: ie taking it modulo maxvalue+1.

[1] eg as a case label.

--
Chris "electric hedgehog" Dollin
Nit-picking is best done among friends.

Mar 9 '07 #6
su************* *@yahoo.com, India wrote:
Yes. It does give the expected value. Thanks.

The following is a beginner's question.

What is (size_t) ? ie what is parenthesized size_t ? how does it
produce a value ?

Please explain

It's a typecast. In this case it's converting the value -1 to the type
size_t -- which is unsigned.
Mar 9 '07 #7
shadowman said:
su************* *@yahoo.com, India wrote:
>Yes. It does give the expected value. Thanks.

The following is a beginner's question.

What is (size_t) ? ie what is parenthesized size_t ? how does it
produce a value ?

Please explain

It's a typecast.
Well, close. It's a cast. C doesn't have anything called "typecast".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 9 '07 #8
Richard Heathfield wrote:
shadowman said:
>su************* *@yahoo.com, India wrote:
>>Yes. It does give the expected value. Thanks.

The following is a beginner's question.

What is (size_t) ? ie what is parenthesized size_t ? how does it
produce a value ?

Please explain
It's a typecast.

Well, close. It's a cast. C doesn't have anything called "typecast".
I must have been thinking about actors who always play similar roles.
Mar 9 '07 #9
su************* *@yahoo.com, India wrote:
(size_t) - 1

Please let me know whether this will give the maximum value of size_t
Please quote context.

Yes it will, not only for size_t, but for any unsigned type, (with the
appropriate cast).

You can also use the SIZE_MAX macro defined in stdint.h, to get the
maximum value of the system's size_t type. It's however a C99 feature.

Mar 9 '07 #10

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

Similar topics

41
8307
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by reference?? Thanks, BB
5
5824
by: Robert Manea | last post by:
Hello everyone, I wrote, simply as an exercise, a small piece of code to find 'strings' (defined as an amount of at least 3 ASCII characters followed by a non ASCII character) in binary files. The purpose of the program is to serve as a facile 'strings' (Unix command) replacement and to be 100% ANSI C. Unfortunatelly it operates notedly slower than the original 'strings' from the fileutils package.
21
2046
by: newlang | last post by:
Hello everyone, I am eager to know about string functions, (user defined) . tell me the technique of find a string in another string.
2
2843
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. The SELECT query is built as follows: $itemtype = stripslashes(trim($_POST));
19
8573
by: ramu | last post by:
Hi, I have, suppose 1000 numbers, in a file. I have to find out 5 largest numbers among them without sorting. Can you please give me an efficient idea to do this? My idea is to put those numbers into a binary tree and to find the largest numbers. How else can we do it? Regards
22
1734
by: jobo | last post by:
Hello, I'm very new to C so please forgive my ineptitude. If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;'d1904" I want to capture each occurence of an integer 0-9 into an array. So in the array I would have {3,8,1,2,8,1,9,0,4}
7
3460
by: dana_livni2000 | last post by:
how do i print the acuale byte sequence that represent a vairable (let say a char - i want to see the 8 bits). thanks dana
7
4906
by: DarthBob88 | last post by:
I have to go through a file and replace any occurrences of a given string with the desired string, like replacing "bug" with "feature". This is made more complicated by the fact that I have to do this with a lot of replacements and by the fact that some of the target strings are two words or more long, so I can't just break up the file at whitespace, commas, and periods. How's the best way to do this? I've thought about using strstr() to...
173
8074
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc. OTOH, if you're allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.
0
8701
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8584
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.