size-t

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

流过昼夜 提交于 2019-11-27 19:57:11
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. 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. #define MAZ_SZ (~(size_t)0) or SIZE_MAX As an alternative to bit-operations suggested

Difference between size_t and std::size_t

雨燕双飞 提交于 2019-11-27 19:51:17
问题 What are the differences between size_t and std::size_t in terms of where they are declared, when they should be used and any other differentiating features? 回答1: C's size_t and C++'s std::size_t are both same. In C, it's defined in <stddef.h> and in C++, its defined in <cstddef> whose contents are the same as C header (see the quotation below). Its defined as unsigned integer type of the result of the sizeof operator. C Standard says in §17.7/2, size_t which is the unsigned integer type of

How to printf a size_t without warning in mingw-w64 gcc 7.1?

穿精又带淫゛_ 提交于 2019-11-27 16:00:58
I am using the mingw-w64 (x64) fork of minGW as prepared on nuwen.net. This is from the 7.1 version of gcc : gcc --version gcc (GCC) 7.1.0 I am compiling this program: #include <stdio.h> int main(void) { size_t a = 100; printf("a=%lu\n",a); printf("a=%llu\n",a); printf("a=%zu\n",a); printf("a=%I64u\n",a); } with warnings and c11 standard: gcc -Wall -Wextra -Wpedantic -std=c11 test_size_t.c and I get these warnings: test_size_t.c: In function 'main': test_size_t.c:6:14: warning: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'size_t {aka long long unsigned

When should I use vector<int>::size_type instead of size_t?

我与影子孤独终老i 提交于 2019-11-27 15:07:02
问题 In this question I see following: for (vector<int>::size_type ix = 0; ix ! = ivec.size(); ++ix) { ivec[ix] = 0; } I understand that why int is not used here, but why not just use size_t ? Under what circumstances I should use vector<int>::size_type instead of size_t ? 回答1: The primary time to use size_type is in a template. Although std::vector<T>::size_type is usually size_t , some_other_container<T>::size_type might be some other type instead 1 . One of the few things a user is allowed to

Cross platform format string for variables of type size_t?

♀尐吖头ヾ 提交于 2019-11-27 07:47:52
On a cross platform c/c++ project (Win32, Linux, OSX), I need to use the *printf functions to print some variables of type size_t. In some environments size_t's are 8 bytes and on others they are 4. On glibc I have %zd, and on Win32 I can use %Id . Is there an elegant way to handle this? finnw The PRIuPTR macro (from <inttypes.h>) defines a decimal format for uintptr_t , which should always be large enough that you can cast a size_t to it without truncating, e.g. fprintf(stream, "Your size_t var has value %" PRIuPTR ".", (uintptr_t) your_var); There are really two questions here. The first

What's sizeof(size_t) on 32-bit vs the various 64-bit data models?

爷,独闯天下 提交于 2019-11-27 07:12:45
On a 64-bit system, sizeof(unsigned long) depends on the data model implemented by the system, for example, it is 4 bytes on LLP64 (Windows), 8 bytes on LP64 (Linux, etc.). What's sizeof(size_t) supposed to be? Does it vary with data model like sizeof(long) does? If so, how? References: 64-bit data models on Wikipedia size_t is defined by the C standard to be the unsigned integer return type of the sizeof operator (C99 6.3.5.4.4), and the argument of malloc and friends (C99 7.20.3.3 etc). The actual range is set such that the maximum (SIZE_MAX) is at least 65535 (C99 7.18.3.2). However, this

Why is the maximum size of an array “too large”?

半腔热情 提交于 2019-11-27 04:56:56
I'm under the same impression as this answer , that size_t is always guaranteed by the standard to be large enough to hold the largest possible type of a given system. However, this code fails to compile on gcc/Mingw: #include <stdint.h> #include <stddef.h> typedef uint8_t array_t [SIZE_MAX]; error: size of array 'array_t' is too large Am I misunderstanding something in the standard here? Is size_t allowed to be too large for a given implementation? Or is this another bug in Mingw? EDIT: further research shows that typedef uint8_t array_t [SIZE_MAX/2]; // does compile typedef uint8_t array_t

Does “std::size_t” make sense in C++?

拈花ヽ惹草 提交于 2019-11-27 03:29:33
In some code I've inherited, I see frequent use of size_t with the std namespace qualifier. For example: std::size_t n = sizeof( long ); It compiles and runs fine, of course. But it seems like bad practice to me (perhaps carried over from C?). Isn't it true that size_t is built into C++ and therefore in the global namespace? Is a header file include needed to use size_t in C++? Another way to ask this question is, would the following program (with no includes) be expected to compile on all C++ compilers? size_t foo() { return sizeof( long ); } There seems to be confusion among the

Clean code to printf size_t in C++ (or: Nearest equivalent of C99's %z in C++)

我的未来我决定 提交于 2019-11-27 00:07:54
问题 I have some C++ code that prints a size_t : size_t a; printf("%lu", a); I'd like this to compile without warnings on both 32- and 64-bit architectures. If this were C99, I could use printf("%z", a); . But AFAICT %z doesn't exist in any standard C++ dialect. So instead, I have to do printf("%lu", (unsigned long) a); which is really ugly. If there's no facility for printing size_t s built into the language, I wonder if it's possible to write a printf wrapper or somesuch such that will insert

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

社会主义新天地 提交于 2019-11-26 20:05:09
问题 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. 回答1: 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