size-t

Platform independent size_t Format specifiers in c?

江枫思渺然 提交于 2019-11-26 18:30:59
I want to print out a variable of type size_t in C but it appears that size_t is aliased to different variable types on different architectures. For example, on one machine (64-bit) the following code does not throw any warnings: size_t size = 1; printf("the size is %ld", size); but on my other machine (32-bit) the above code produces the following warning message: warning: format '%ld' expects type 'long int *', but argument 3 has type 'size_t *' I suspect this is due to the difference in pointer size, so that on my 64-bit machine size_t is aliased to a long int ( "%ld" ), whereas on my 32

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

吃可爱长大的小学妹 提交于 2019-11-26 17:37:02
问题 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 回答1: 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)

When should I use std::size_t?

旧街凉风 提交于 2019-11-26 15:36:42
I'm just wondering should I use std::size_t for loops and stuff instead of int ? For instance: #include <cstdint> int main() { for (std::size_t i = 0; i < 10; ++i) { // std::size_t OK here? Or should I use, say, unsigned int instead? } } In general, what is the best practice regarding when to use std::size_t ? A good rule of thumb is for anything that you need to compare in the loop condition against something that is naturally a std::size_t itself. std::size_t is the type of any sizeof expression and as is guaranteed to be able to express the maximum size of any object (including any array)

Cross platform format string for variables of type size_t? [duplicate]

女生的网名这么多〃 提交于 2019-11-26 13:50:37
问题 This question already has answers here : What's the correct way to use printf to print a size_t? (3 answers) Closed 13 days ago . 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? 回答1: The PRIuPTR macro (from <inttypes.h>) defines a decimal format for uintptr

Why is size_t unsigned?

半世苍凉 提交于 2019-11-26 13:03:59
Bjarne Stroustrup wrote in The C++ Programming Language: The unsigned integer types are ideal for uses that treat storage as a bit array. Using an unsigned instead of an int to gain one more bit to represent positive integers is almost never a good idea. Attempts to ensure that some values are positive by declaring variables unsigned will typically be defeated by the implicit conversion rules. size_t seems to be unsigned "to gain one more bit to represent positive integers". So was this a mistake (or trade-off), and if so, should we minimize use of it in our own code? Another relevant article

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

三世轮回 提交于 2019-11-26 12:28:44
问题 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:

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

依然范特西╮ 提交于 2019-11-26 11:37:33
问题 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:

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

不打扰是莪最后的温柔 提交于 2019-11-26 10:30:20
问题 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

Platform independent size_t Format specifiers in c?

前提是你 提交于 2019-11-26 06:27:08
问题 I want to print out a variable of type size_t in C but it appears that size_t is aliased to different variable types on different architectures. For example, on one machine (64-bit) the following code does not throw any warnings: size_t size = 1; printf(\"the size is %ld\", size); but on my other machine (32-bit) the above code produces the following warning message: warning: format \'%ld\' expects type \'long int *\', but argument 3 has type \'size_t *\' I suspect this is due to the

Should I use std::size_t or int in my for loops?

佐手、 提交于 2019-11-26 04:29:38
问题 I\'m just wondering should I use std::size_t for loops and stuff instead of int ? For instance: #include <cstdint> int main() { for (std::size_t i = 0; i < 10; ++i) { // std::size_t OK here? Or should I use, say, unsigned int instead? } } In general, what is the best practice regarding when to use std::size_t ? 回答1: A good rule of thumb is for anything that you need to compare in the loop condition against something that is naturally a std::size_t itself. std::size_t is the type of any sizeof