2

I understand that in general , pointers to any data type will have the same size. On a 16 bit system, normally 2 bytes and and on a 32 bit system , 4 bytes.

Depending on what this pointer points to, if it is incremented , it will increment by a different number of bytes depending on if it's a char pointer, long pointer etc.

My query is how does the compiler know by how many bytes to increment this pointer. Isn't it just a variable stored in memory like any other? Are the pointers stored in some symbol table with information about how much they should be incremented by? Thanks

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Engineer999
  • 3,683
  • 6
  • 33
  • 71

3 Answers3

3

That is why there are data types. Each pointer variable will have an associated data type and that data type has a defined size (See about complete/incomplete type in footnote). Pointer arithmetic will take place based on the data type.

To add to that, for pointer arithmetic to happen, the pointer(s) should be (quoted from c11 standard)

pointer to a complete object type

So, the size of the "object" the pointer points to is known and defined.

Footnote: FWIW, that is why, pointer arithmetic on void pointers (incomplete type) is not allowed / defined in the standard. (Though GCC supports void pointer arithmetic via an extension.)

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Re

I understand that in general , pointers to any data type will have the same size

No. Different pointer sizes are unusual for ¹simple pointers to objects, but can occur on word-addressed machines. Then char* is the largest pointer, and void* is the same size.

C++14 §3.9.2/4

An object of type cv void* shall have the same representation and alignment requirements as cv char*.

All pointers to class type object are however the same size. For example, you wouldn't be able to use an array of pointers to base type, if this wasn't the case.


Re

how does the compiler know by how many bytes to increment this pointer

It knows the size of the type of object pointed to.

If it does not know the size of the object pointed to, i.e. that type is incomplete, then you can't increment the pointer.

For example, if p is a void*, then you can't do ++p.

Notes:
¹ In addition to ordinary pointers to objects, there are function pointers, and pointers to members. The latter kind are more like offsets, that must be combined with some specification of a relevant object, to yield a reference.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

The data type of the pointer variable defines how many bytes to be incremented.

for example: 1) on incrementing a character pointer, pointer is increment by 1 Byte. 2) Likewise, for a integer pointer, pointer is increment by 4 Bytes (for 32-bit system) and 8 Bytes (for 64-bit system)

Rjain
  • 127
  • 10
  • As data (char or int) will be stored in a different segment in memory to the code, how does the code know in which way to increment the variable or pointer? Are the variables stored in a symbol table? – Engineer999 Apr 22 '16 at 13:09
  • Variables get stored either in stack (local variables) or data segment (Global Variables), data type has nothing to do with the segment they should be saved in 2) Symbol table contains only links to function and not varibles – Rjain Apr 22 '16 at 14:01
  • When we talk about pointers, we refer to addresses in memory. when you declare a pointer to a variable, your pointer now points to that memory location. Whenever you increment or decrement a pointer, it will perform operations with respect to this memory location. – Rjain Apr 22 '16 at 14:09