I am in the process of porting a C++ application to x64 from x86. Now, there is a custom array class with functions like GetRowCount(); etc. If these functions return a size_t (and the whole array class would be dealing with size_t for indexing) it would mean I could get (2^64)-1 items in such an array, but if I would utilize a signed ptrdiff_t instead I would lose much of the max array size. My question is basically which types to use: signed or unsigned types for arrays? I have been reading other topics on this but they have been purely focused on indexing.
The problem I see is due to let's say somewhere code looks something like this:
[signed/unsigned type] result = customArray.GetRowCount() - a;where in some cases the variable "a" may be signed and in other cases unsigned, and assuming it could be larger than the row count returned. What is then done with the "result" variable I don't know, but it could for instance be used in some further arithmetic operations.
So, to stay out of trouble with mixing signed and unsigned arithmetic and implicit casts in code which utilizes the custom array class, should I in that array class instead be working with ptrdiff_t and thereby also let functions like GetRowCount() return a ptrdiff_t? Or should I use intptr_t, is it more logical?
Here http://www.viva64.com/en/a/0050/ the example shows ptrdiff_t should be used...
However, like stated in the beginning I can then no longer index arrays with up to SIZE_MAX elements since half of the value span of a signed type will be used for negative values.
I hope I am making any sense and would really appreciate some clarification and guidance. Thank you all in advance!
답변
This is pretty close to a matter of personal taste.
Some people say any use of an unsigned integral type for anything other than bit manipulation is faulty, and use signed types for all quantities. Others (including the designers of the C++ standard libraries) use unsigned types for counting things. Neither side of the debate has ever proven that the other side are total idiots who can't write decent code (although I think occasional individuals on both sides have claimed they have).
I feel that unless you're going to eliminate unsigned types entirely you might as well copy the standard. If the array is backed by memory then it's unlikely that an implementation will create an array half the size of the address space even on a 32 bit system. On a 64 bit system it's utterly infeasible. If you arrange for your array class to prevent that it's not much of a loss. So the max value of the type isn't really a concern.
However, that's for newly-designed code. If the class is central then you shouldn't change its interface more than necessary. Either continue with int or switch to ptrdiff_t if you need more capacity. Definitely don't use intptr_t. That's for storing addresses, not counts or offsets.
이런 의견도 있네
취향이래 ,,,, 흠.,,
출처
http://stackoverflow.com/questions/20388790/c-signed-or-unsigned-array-indexing
길어서 안봄 ㅎ
개취래요 근데 난 이 의견 반댈세
auto로 걸어버리면 다 unsigned int로 읽히긴 하드만
그건 걍 C++에서 그렇게 뱉어내서 그런 거 같음. 왜 표준 만든 놈들이 그래 했는지는 다음 글에 나오긴 하는데 대충 요약하자면 그게 로우레벨에서는 쓸만하고 STL을 쓰는 사용자가 로우할지 하이할지 모르니까 일단 다 커버하게 만들었다.... 라고 되있음
(시험에 안 나오는 거라서 영어 대충 읽음)
글쿠나
그런데 unsigned를 니가 쓰고 싶지 않더라도... size_t로 되어 있는게 워낙 많아서 알게 모르게 쓰긴할건데
상관은 딱히 없을 것같긴하군
다음 글도 읽어봐요 난 그냥 signed 쓸라요
츄럴//되있->돼있 (되어 = 돼임) [리듬 맞춤법 봇♬]