size-t

How to cast the size_t to double or int C++

匆匆过客 提交于 2019-12-20 10:29:32
问题 My question is that I have a size_t data, but now I want to convert it to double or int. If I do something like size_t data = 99999999; int convertdata = data; the compiler will report warning. because it maybe overflow. Do you have some method like the boost or some other method to do the convert? 回答1: A cast, as Blaz Bratanic suggested: size_t data = 99999999; int convertdata = static_cast<int>(data); is likely to silence the warning (though in principle a compiler can warn about anything

should use size_t or ssize_t [duplicate]

本秂侑毒 提交于 2019-12-20 08:01:39
问题 This question already has answers here : Signed vs. unsigned integers for lengths/counts (4 answers) Closed 6 years ago . At my code, I do not use int or unsigned int. I only use size_t or ssize_t for portable. For example: typedef size_t intc; // (instead of unsigned int) typedef ssize_t uintc; // (instead of int) Because strlen , string , vector ... all use size_t , so I usually use size_t . And I only use ssize_t when it may be negative. But I find that: The unsigned integer types are

What should happen to the negation of a size_t (i.e. `-sizeof(struct foo)`))?

痞子三分冷 提交于 2019-12-17 16:44:11
问题 I'm dealing with some code at work that includes an expression of the form -(sizeof(struct foo)) i.e. the negation of a size_t , and I'm unclear on what the C and C++ standards require of compilers when they see this. Specifically, from looking around here and elsewhere, sizeof returns an unsigned integral value of type size_t . I can't find any clear reference for specified behavior when negating an unsigned integer. Is there any, and if so, what is it? Edit: Ok, so there are some good

Should I include stddef.h or cstddef for size_t

风格不统一 提交于 2019-12-17 10:58:55
问题 When I want to use size_t in C++, should I include <stddef.h> or <cstddef> ? I have heard several people saying that <cstddef> was a bad idea, and it should be deprecated. Why is that? 回答1: I prefer #include <stddef.h> . Some of the names in the C headers are allowed to be macros, but the set differs from the C rules. In C, EXIT_FAILURE , isdigit() , getc() a.o. are macros. Do you know which ones are macros in C++? Secondly, only a couple standard C headers are required to have the <cfoo>

Why is size_t unsigned?

て烟熏妆下的殇ゞ 提交于 2019-12-17 02:35:18
问题 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

Why is rsize_t defined?

感情迁移 提交于 2019-12-13 11:52:00
问题 I found that strncpy_s() is defined under VS2013 as errno_t __cdecl strncpy_s ( _Out_writes_z_(_SizeInBytes) char * _Dst, _In_ rsize_t _SizeInBytes, _In_reads_or_z_(_MaxCount) const char * _Src, _In_ rsize_t _MaxCount ); rsize_t is: typedef size_t rsize_t; I think it's a trick done by Visual Studio . However, I found this function defined as follows on this page errno_t strncpy_s ( char *restrict dest, rsize_t destsz, const char *restrict src, rsize_t count ); Why is rsize_t defined here?

Adding a size_t variable to a pointer

我只是一个虾纸丫 提交于 2019-12-12 18:34:36
问题 I want to add a size_t type to a pointer. Some like this: void function(size_t sizeA,size_t sizeB){ void *pointer; pointer=malloc(sizeA); pointer=pointer+sizeB; } In the hipothetic case that this will not end in a segfault, the question is: Can I do this? Add a type size_t to a pointer? And the resulting address will be in the address 'size'? 回答1: Can I do this [add size_t to a pointer]? Yes, you can, provided that you cast void pointer to some other type: pointer = ((char*)pointer) + sizeB;

converting size_t into long, Is there any disadvantage?

依然范特西╮ 提交于 2019-12-11 04:59:36
问题 Is there any disadvantage of converting size_t to long? Because, I am writing an program that maintains linked_list in a file. So I traverse to another node based on size_t and I also keep track of total number of lists as size_t. Hence, obviously there is going to be some conversion or addition of long and size_t. Is there any disadvantage of this? If there is then I will make everything as long instead of size_t, even the sizes. Please advise. 回答1: It's not a problem now, but it may be in

What is “size of the largest possible object on the target platform” in terms of size_t

江枫思渺然 提交于 2019-12-10 21:55:22
问题 I am reading article about size_t in C/C++ http://web.archive.org/web/20081006073410/http://www.embedded.com/columns/programmingpointers/200900195 (link found through Stackoverflow). Quote from the article: Type size_t is a typedef that's an alias for some unsigned integer type, typically unsigned int or unsigned long, but possibly even unsigned long long. Each Standard C implementation is supposed to choose the unsigned integer that's big enough--but no bigger than needed-- to represent the

Is it safe to use negative integers with size_t?

爱⌒轻易说出口 提交于 2019-12-10 12:42:58
问题 I just saw some C++ code like this. It was using a condition to decide whether to walk forward or backward through a std::vector . The compiler doesn't complain, but I thought size_t was unsigned. Is this dangerous? vector<int> v { 1,2,3,4,5 }; bool rev = true; size_t start, end, di; if (rev) { start = v.size()-1; end = -1; di = -1; } else { start = 0; end = v.size(); di = 1; } for (auto i=start; i!=end; i+=di) { cout << v[i] << endl; } 回答1: It's well defined to use unsigned integers (and