2

It seems to have no impact on the functionality of sbrk, but in sbrk()'s documentation it says that it requires an intptr_t parameter.

It works (or at least seems to work) when passing an int type as a parameter.

This is in regular C.

Bob
  • 159
  • 1
  • 5

2 Answers2

6

intptr_t changes size depending on the architecture. int may or may not change size depending on the architecture- that is, if you are on 64bit, then intptr_t MUST be 64bits, whereas int may be 32bits.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Isn't intptr_t a pointer though, not an actual value? So in sbrk() there would be a dereference of intptr_t, whereas if you pass an int, there wouldn't be a dereference required. – Bob Mar 20 '11 at 23:24
  • @Bob `intptr_t` is an integer type, the smallest one that can contain all the bits of a pointer. – Pascal Cuoq Mar 20 '11 at 23:26
  • Oh that makes sense! For some reason I thought it was a pointer.. Thanks for the clarification. What is the significance of "all the bits of a pointer"? Does that mean from machine to machine, the bits required for a pointer changes and so does the size of intptr_t? – Bob Mar 20 '11 at 23:28
  • @Bob Well, it changes "from platform to platform". The amd64 processors that most people have can work in 32-bit compatibility mode (32-bit pointers) or in 64-bit mode. Most OSes even allow 32-bit processes and 64-bit processes to coexist. But when compiling a C program you are either targeting one mode or the other. – Pascal Cuoq Mar 20 '11 at 23:32
1

As long as the right header is included, you're not passing an int to sbrk(). The value you are passing is converted to intptr_t according to C promotion rules. These rules are terribly subtle so you should at least know they exist and avoid invoking them in corner cases.

As a consequence, do not expect something spectacular such as a crash, when you pass sbrk something that looks to you like an int, even if intptr_t is different from int on your platform, as long as the header that provides sbrk()'s prototype is included.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • So when you pass sbrk() an 'int', it stores this value and passes an intptr_t with the location of the int? – Bob Mar 20 '11 at 23:26
  • @Bob No, promotion rules do not do this. Also `intptr_t` is an integer type, just not always the same as `int`. – Pascal Cuoq Mar 20 '11 at 23:29