Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a portable method to find the maximum value of size_t?

Tags:

c

size-t

I'd like to know the maximum value of size_t on the system my program is running. My first instinct was to use negative 1, like so:

size_t max_size = (size_t)-1; 

But I'm guessing there's a better way, or a constant defined somewhere.

like image 340
Justicle Avatar asked Aug 12 '10 21:08

Justicle


2 Answers

A manifest constant (a macro) exists in C99 and it is called SIZE_MAX. There's no such constant in C89/90 though.

However, what you have in your original post is a perfectly portable method of finding the maximum value of size_t. It is guaranteed to work with any unsigned type.

like image 110
AnT Avatar answered Sep 20 '22 03:09

AnT


#define MAZ_SZ (~(size_t)0) 

or SIZE_MAX

like image 21
nategoose Avatar answered Sep 21 '22 03:09

nategoose