size-t

Is size_t large enough to represent size of any type? [duplicate]

邮差的信 提交于 2021-02-08 14:59:01
问题 This question already has answers here : C standard regarding sizeof overflowing size_t (1 answer) What is the maximum size of an array in C? (7 answers) Can calloc() allocate more than SIZE_MAX in total? (7 answers) Closed 1 year ago . Is size_t guaranteed to be large enough to represent size of any type? According to this reference: size_t can store the maximum size of a theoretically possible object of any type (including array). This is generally a reliable reference but I could not find

Should I always use size_t when indexing arrays?

安稳与你 提交于 2021-02-07 11:24:35
问题 Do I need to use size_t always when indexing an array even if the array is not big enough to exceed the size of an int? It's not a question about when I should use size_t . I just want to know if, for example, a program having 2GB of available memory (all of these fields can be indexed by an int32) but with this memory being (virtual memory) assigned to the "fields" 14GB - 16GB of the computer's RAM. Would it always fail when indexing the memory if I used an int32 instead of a size_t (or an

Why size_t is used for indexing / representing size of an array?

﹥>﹥吖頭↗ 提交于 2021-02-07 04:24:12
问题 According to C++ - should you size_t with a regular array? § 18.2 6 The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object. I don't understand why this guarantees a type size_t to be big enough for an array index or big enough to represent the number of elements in an array. For example: int array[1000]; for (size_t i = 0; i < 1000; ++i) { } It seems unrelated to me why a "large enough number to contain the size in

For Loop Exit Condition (size_t vs. int) [duplicate]

ε祈祈猫儿з 提交于 2021-01-28 00:41:43
问题 This question already has answers here : What's the best way to do a reverse 'for' loop with an unsigned index? (20 answers) Closed 5 years ago . When I put the following in my program: for (size_t i = VectorOfStructs.size()-1; i > 0; i--) It works correctly but does "i" will never equal 0. So, I cannot access the first element (VectorOfStructs[0]). If I change it to: for (size_t i = VectorOfStructs.size()-1; i > -1; i--) The program doesn't even enter the for loop! But, if I change it to the

Infinite loop when using size_t in a count down for loop

旧巷老猫 提交于 2020-12-25 10:54:36
问题 So I'm using size_t instead of int in any indexing for loop to prevent negative indices. But when counting down, this leads to an overflow: for (size_t i = 10; i >= 0; --i) { // Do something, f.ex. array[i] = i } What would be a nice way to prevent this? Using int instead? Using i == 0 as the termination condition? (This would however not work if I need the 0) I'd appreciate any feedback! 回答1: for (size_t i = 10; i <= 10; --i) // do something When overflow do happens, it will round to the

Infinite loop when using size_t in a count down for loop

萝らか妹 提交于 2020-12-25 10:53:38
问题 So I'm using size_t instead of int in any indexing for loop to prevent negative indices. But when counting down, this leads to an overflow: for (size_t i = 10; i >= 0; --i) { // Do something, f.ex. array[i] = i } What would be a nice way to prevent this? Using int instead? Using i == 0 as the termination condition? (This would however not work if I need the 0) I'd appreciate any feedback! 回答1: for (size_t i = 10; i <= 10; --i) // do something When overflow do happens, it will round to the

Comparison size_t variable with -1 (maximum size value) in c++ code

无人久伴 提交于 2020-06-01 04:14:50
问题 I'm refactoring a library and trying to get rid of many gcc warnings. The big part of these warning are about signed / unsigned comparison and are related to the usage of size_t . The library works on 64 bit Linux systems. A programmer used -1 as the special value similar to std::string::npos . There are many places in the library where code looks like this: class AnnotationBase { public: size_t m_offset = -1; size_t m_length = -1; } ... AnnotationBase foo(const std::string& text, const

Comparison size_t variable with -1 (maximum size value) in c++ code

倾然丶 夕夏残阳落幕 提交于 2020-06-01 04:14:24
问题 I'm refactoring a library and trying to get rid of many gcc warnings. The big part of these warning are about signed / unsigned comparison and are related to the usage of size_t . The library works on 64 bit Linux systems. A programmer used -1 as the special value similar to std::string::npos . There are many places in the library where code looks like this: class AnnotationBase { public: size_t m_offset = -1; size_t m_length = -1; } ... AnnotationBase foo(const std::string& text, const

Is it safe to take the difference of two size_t objects?

别说谁变了你拦得住时间么 提交于 2020-01-30 19:58:52
问题 I'm investigating a standard for my team around using size_t vs int (or long , etc). The biggest drawback I've seen pointed out is that taking the difference of two size_t objects can cause problems (I'm unsure of specific problems -- maybe something wasn't 2s complemented and the signed/unsigned angers the compiler). I wrote a quick program in C++ using the V120 VS2013 compiler that allowed me to do the following: #include <iostream> main() { size_t a = 10; size_t b = 100; int result = a - b