size-t

What to do with size_t vs. std::size_t?

我怕爱的太早我们不能终老 提交于 2019-12-05 01:43:41
Having just read: Does "std::size_t" make sense in C++? I realize using ::size_t is not standards-compliant (although supported by my compiler) when you #include <cstddef> . I want to comply with the standard, but I do not want to prepend std:: to all of my size_t 's. So, what is the more customary/popular way to handle this: write using std::size_t; ? include <stddef.h> ? just rely on compiler support? something else? Bob Miller You should specify it with the using directive. using std::size_t; Add it either to the global scope of each compilation unit, or to the local scopes if it would

C size_t and ssize_t negative value

左心房为你撑大大i 提交于 2019-12-05 00:54:08
size_t is declared as unsigned int so it can't represent negative value. So there is ssize_t which is the signed type of size_t right? Here's my problem: #include <stdio.h> #include <sys/types.h> int main(){ size_t a = -25; ssize_t b = -30; printf("%zu\n%zu\n", a, b); return 0; } why i got: 18446744073709551591 18446744073709551586 as result? I know that with size_t this could be possible because it is an unsigned type but why i got a wrong result also with ssize_t ?? In the first case you're assigning to an unsigned type - a . In the second case you're using the wrong format specifier. The

Cast ssize_t or size_t

时光毁灭记忆、已成空白 提交于 2019-12-04 18:03:46
问题 In source files which I am using in my project, there is a comparison between ssize_t and size_t variables: ssize_t sst; size_t st; if(sst == st){...} I would like to get rid of the warning: warning: comparison between signed and unsigned integer expressions But I am not sure, which variable should I cast to the other? if((size_t)sst == st){...} or if(sst == (ssize_t)st){...} What is safer, better, cleaner? Thanks 回答1: There is no one right answer to this question. There are several possible

What is the limit on malloc parameter of type size_t in C? Docs say it has an upper limit of UINT_MAX but I can't go beyond INT_MAX

不打扰是莪最后的温柔 提交于 2019-12-04 13:58:38
I want to allocate a 2.9GB char array with database = (char*) malloc((2900 * 1000000 * sizeof(char))); This gives an integer overflow warning and the malloc returns NULL . The malloc parameter is of type size_t which according to documentation is of type unsigned int . So the max should be UINT_MAX which is at least 2.9GB. However, if I try to allocate more than MAX_INT the malloc fails. Does this mean size_t on my system is of type int? How do I check this? I looked through /usr/include/stdlib.h and ./lib/gcc/x86_64-redhat-linux/4.1.1/include/stddef.h but can't find the definition of size_t .

Is size_t the word size?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 09:54:17
问题 Is size_t the word size of the machine that compiled the code? Parsing with g++, my compiler views size_t as an long unsigned int . Does the compiler internally choose the size of size_t , or is size_t actually typdefed inside some pre-processor macro in stddef.h to the word size before the compiler gets invoked? Or am I way off track? 回答1: In the C++ standard, [support.types] (18.2) /6: "The type size_t is an implementation-defined unsigned integer type that is large enough to contain the

What is the correct definition of size_t? [duplicate]

和自甴很熟 提交于 2019-12-04 07:41:39
This question already has answers here : What is size_t in C? (12 answers) Closed 4 years ago . First of all, what do I mean, by 'correct definition`? For example, K&R in "C Programming Language" 2nd ed. , in section 2.2 Data Types and Sizes , make very clear statements about integers: There are short , int and long for integer types. They are needed to repesent values of different boundaries. int is a "naturally" sized number for a specific hardware, so also probably the most fastest. Sizes for integer types short , int and long are purely implementation-dependent. But they have restrictions.

How to get SIZE_MAX in C89

柔情痞子 提交于 2019-12-04 03:58:42
问题 I'm trying to get SIZE_MAX in C89. I thought of the following way to find SIZE_MAX : const size_t SIZE_MAX = -1; Since the standard (§6.2.1.2 ANSI C) says: When a signed integer is converted to an unsigned integer with equal or greater size, if the value of the signed integer is nonnegative, its value is unchanged. Otherwise: if the unsigned integer has greater size, the signed integer is first promoted to the signed integer corresponding to the unsigned integer; the value is converted to

typedef for a signed type that can contain a size_t?

纵然是瞬间 提交于 2019-12-04 02:59:42
问题 Is there a standard (or MSVC proprietary) typedef for a signed type that can contain the full range of size_t values? I.e. on a 64-bit system, it would be a 128-bit signed integer. 回答1: It's not possible in general to define such a type. It's perfectly legal for an implementation to make size_t the largest supported unsigned type, which would mean that no signed type can hold all its values. ptrdiff_t is not necessarily wide enough. It's the result of subtracting two pointers, but there's

printf for size_t

。_饼干妹妹 提交于 2019-12-03 23:32:11
Is there any way to give printf a size_t without either casting it first or generating a compiler warning? (I always compile with -Wall .) printf("%zu", sizeof(whatever)); 来源: https://stackoverflow.com/questions/4150056/printf-for-size-t

Objective-C Runtime: What to put for size & alignment for class_addIvar?

余生长醉 提交于 2019-12-03 16:44:35
The Objective-C Runtime provides the class_addIvar C function : BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types) What do I put for size and alignment ? I'm adding an instance variable of type UITextPosition * , but no UITextPosition object is in scope. For size , can I just do sizeof(self) , where self is a subclass of UITextField ? I.e., can I assume that a UITextPosition object is the same size as a UITextField object? How do I get alignment ? The documentation on this stuff is not very informative, which does reflect that generally you