Questions tagged [ptrdiff-t]

In the C programming language, ptrdiff_t is the signed integer type of the result of subtracting two pointers.

In the C programming language, ptrdiff_t is the signed integer type of the result of subtracting two pointers.

See the C programming language,ISO/IEC 9899:201x specification for more details.

17 questions
33
votes
2 answers

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

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…
jotik
  • 17,044
  • 13
  • 58
  • 123
8
votes
5 answers

Pointer difference across members of a struct?

The C99 standard states that: When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object Consider the following code: struct test { int x[5]; char something; …
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
7
votes
3 answers

Does size_t have the same size and alignment as ptrdiff_t?

On my platform (and on most of them I think) std::size_t and std::ptrdiff_t have the same size and the same alignment. Is there any platform where that is not true? In short: is it required by the standard?
user7769147
  • 1,559
  • 9
  • 15
7
votes
4 answers

Can we subtract NULL pointers?

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…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
5
votes
2 answers

Usage of ptrdiff_t

I'm implementing iterator through continuous chunk of memory and came to the issue about its conforming usage. My current implementation (assuming I'm iterating through array of chars). typedef struct iterator{ void *next_ptr; void *limit;…
Some Name
  • 8,555
  • 5
  • 27
  • 77
5
votes
1 answer

C++ size_t and ptrdiff_t for negative array indexing

I'm having difficulty choosing between size_t and ptrdiff_t for the type of an index, which should need to be able to store a negative value. To be precise, in my code I need to implement an array. I receive it's length (in the constructor) as a…
Mikrosaft
  • 195
  • 1
  • 8
4
votes
3 answers

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

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] =…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
1
vote
2 answers

What is the motivation for ptrdiff_t?

Why does C define a separate type for the difference between two pointers? For example, why couldn't this be defined as long long, or even doing intmax_t? Is there ever a time when intmax_t does not equal ptrdiff_t ? Basically, what is the meaning…
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
1
vote
3 answers

Should I cast size_t to ptrdiff_t?

I have a malloc'ed array of pointers that forms a hash table. To step through the hash table I'd use pointer arithmetic, eg: node_t ** tc = table; size_t tcs = sizeof(node_t *); for(long i = 0; i < tableSize; tc+=tcs, ++i) { // Do some stuff…
Toby
  • 9,696
  • 16
  • 68
  • 132
1
vote
2 answers

Using std::ptrdiff_t on user defined types

I have a simple struct called node which holds a value + 2 pointers to next/previous nodes. template struct node { node *prev = NULL; node *next = NULL; T data; }; Here we have the function which adds a new…
tuk
  • 367
  • 1
  • 4
  • 10
0
votes
1 answer

Why is ptrdiff_t signed?

If ptrdiff_t were unsigned, it would be able to refer to twice as many elements. On my machine PTRDIFF_MAX is expanded to 9223372036854775807i64, whereas ULLONG_MAX is 18446744073709551615Ui64. I know that these values are huge themselves, but…
ZoomIn
  • 1,237
  • 1
  • 17
  • 35
0
votes
2 answers

&q - &p, q and p pointing to non-initialised char array. And o/p is 1. How?

main(){ char a[20],*p,*q; p=&a[0]; q=&a[10]; printf ("%d\n",&q - &p) } This C program gives o/p as: 1 As I understand, the values stored at those addresses are garbage. How can their subtraction be 1? Can anyone please explain how?
spiders.here
  • 127
  • 2
  • 13
0
votes
0 answers

Can I cast void * to ptrdiff_t in C89?

Will the C89 standard allow me to cast void * to ptrdiff_t so I can print the memory location as hexadecimal? For example: static const char *dig = "0123456789abcdef"; char buf[16], *ptr = buf; /* Need 16 bytes when sizeof(void *) == 8 */ void…
cbot
  • 117
  • 6
0
votes
1 answer

How to handle ptrdiff_t in SWIG generated Java wrapper?

I have this struct in C: typedef struct THTensor { ... ptrdiff_t storageOffset; ... } THTensor; However, the SWIG-generated Java code is: public SWIGTYPE_p_ptrdiff_t getStorageOffset() { return new…
Tongfei Chen
  • 613
  • 4
  • 14
0
votes
2 answers

Copying one part of a string to another in C

I have problems trying to copy one part of a string into a another. Given these two char pointers: line points at string cointaining: "helmutDownforce:1234:44:yes" username points at: NULL Here's my function that takes these pointers as…
cass
  • 11
1
2