ptrdiff-t

Can we subtract NULL pointers?

穿精又带淫゛_ 提交于 2021-01-27 05:29:34
问题 Since pointer arithmetic is defined within the same array I'm in doubt if we can subtract NULL from another NULL . I'm concerned about the implementation of: //first and second can both either be from the same array //or be both NULL prtdiff_t sub(void *first, void *second){ //Do I really need this condition? if(!first && !second) return (ptrdiff_t) 0; return second - first; } 回答1: Subtracting two NULL pointers is not allowed. Section 6.5.6p9 of the C standard states: When two pointers are

Can ptrdiff_t represent all subtractions of pointers to elements of the same array object?

孤街醉人 提交于 2019-12-21 07:02:15
问题 For subtraction of pointers i and j to elements of the same array object the note in [expr.add#5] reads: [  Note: If the value i−j is not in the range of representable values of type std​::​ptrdiff_­t , the behavior is undefined. —  end note ] But given [support.types.layout#2], which states that ( emphasis mine): The type ptrdiff_­t is an implementation-defined signed integer type that can hold the difference of two subscripts in an array object, as described in [expr.add]. Is it even

Can ptrdiff_t represent all subtractions of pointers to elements of the same array object?

社会主义新天地 提交于 2019-12-03 22:05:40
For subtraction of pointers i and j to elements of the same array object the note in [expr.add#5] reads: [  Note: If the value i−j is not in the range of representable values of type std​::​ptrdiff_­t , the behavior is undefined. —  end note ] But given [support.types.layout#2] , which states that ( emphasis mine): The type ptrdiff_­t is an implementation-defined signed integer type that can hold the difference of two subscripts in an array object, as described in [expr.add] . Is it even possible for the result of i-j not to be in the range of representable values of ptrdiff_t ? PS: I

For iterating though an array should we be using size_t or ptrdiff_t?

做~自己de王妃 提交于 2019-11-30 23:10:52
In this blog entry by Andrey Karpov entitled, "About size_t and ptrdiff_t " he shows an example, for (ptrdiff_t i = 0; i < n; i++) a[i] = 0; However, I'm not sure if that's right, it seems that should be for (size_t i = 0; i < n; i++) a[i] = 0; Is this correct? I know we should also likely be using something like memset , but let's avoid that entirely. I'm only asking about the type Pascal Cuoq In a blog post , I argue that you should always refrain from allocating memory blocks larger than PTRDIFF_MAX (*), because doing so will make compilers such as Clang and GCC generate nonsensical code

For iterating though an array should we be using size_t or ptrdiff_t?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 18:53:02
问题 In this blog entry by Andrey Karpov entitled, "About size_t and ptrdiff_t" he shows an example, for (ptrdiff_t i = 0; i < n; i++) a[i] = 0; However, I'm not sure if that's right, it seems that should be for (size_t i = 0; i < n; i++) a[i] = 0; Is this correct? I know we should also likely be using something like memset , but let's avoid that entirely. I'm only asking about the type 回答1: In a blog post, I argue that you should always refrain from allocating memory blocks larger than PTRDIFF