r/C_Programming • u/aliathar • Aug 18 '26
Negative value in a pointer question.
Please look at this code. if i define the PTRTYPE as int, it stops working, while doing a uint, it does work...
void initILAPoll(debugBridge_t **d, PTRTYPE ptr){
*d = (debugBridge_t *)ptr; // base address of the DEBUG_BRIDGE peripheral
cb_init(cb, local_memory, bufferLength);
sprintf(xvcInfo, "xvcServer_v1.0:%d\n", MAX_WINDOW_SIZE);
}
the usage in main code is done like this
initILAPoll(&myD, 0x80000000);
//myD = (debugBridge_t *)0x80000000;
where the variable myD is a structure pointer.
if i print the address of myD, it give the correct address. Moreover, the disassembly of the code is also the same in case of int and uint. Can somebody explain what behavior is at play here>
2
Upvotes
•
u/mikeblas Aug 19 '26
Please correctly format your code.
16
u/torsten_dev Aug 18 '26 edited Aug 18 '26
Your
PTRTYPEisn't actually a pointer type, nor does it have the same size as one.Signed integer overflow is UB. 0x80000000 overflows a 32-bit type. Usually signed overflow with silently wrap, meaning your int is now negative. (Usually because the compiler is free to do other things since this is UB)
When casting a signed type to a larger type (here the
debugBridge_t*is likely 64 bit) it will do sign extension, since your integer is negative this means filling the upper bits of your pointer with 1s.Your assembly should differ. Where one contains a movl the other has a movslq. Or in intel syntax one mov is a movsxd instead.
2
u/alkatori Aug 18 '26
how are you #define PTRTRYPE?
3
u/flyingron Aug 18 '26
Pointers don’t have signs and what happens when you cast them to an integer type is not portable. Relational operators with pointers are limited to two values within the same object.
1
u/NoSpite4410 Aug 23 '26
A pointer is implementation defined, but generally a 64-bit unsigned integer in a 64-bit system, and a 32 bit unsigned integer in a 32-bit system.
The "address" is mapped as an integer by the C runtime from 0 to the end of RAM, with some interesting behind the scenes schemes for virtual memory, ramdisks, things like that. As a C programmer, all you see is the linear "number" of the address. Devices are mapped onto the linear memory model, as well as interrupt vectors and system calls, signals, etc. That way things always have an address you can read and write bytes to if you are allowed to access them. Some memory is off limits such as 0x00000000, so you can never read or write to there, just use it for a NULL pointer to check pointers for "nothing there".
Of course 64 bits of address gives you theoretically 2^{64} - 1 unique bytes you could theoretical access; we don't have machines that big.
An int has only 31 bits of magnitude and 1 for sign. Even and unsigned int has only 32 bits. That really is not enough for RAM pointers over the first 4G.
An unsigned long (8 bytes unsigned integer) is the same size as a pointer, but has no actual type info.
Always use the typed pointer for addresses, not just an untyped integer, as it will make the pointer math work correctly and intelligently. Otherwise you have to do the pointer math yourself.
Many coders do not realize that the pointer type determines how the system sees the memory it points to, not the other way around. Access via an int* will cause the machine to read and write in 4-byte chunks ; a double* will read and write in 8-byte chunks; a struct* will read and write in struct-sized chunks.
RAM is just raw bytes, the pointer determines how the machine sees the ram as data.
1
u/DawnOnTheEdge Aug 18 '26 edited Aug 18 '26
On a 64-bit system, pointers are 64-bits wide and
intis only 32 bits wide. What’s most likely happening is that, when you use 32-bitunsigned int, the upper bits of your pointer are getting cleared to 0 in a round-trip conversion, but when you use a 32-bitsigned int, it gets sign-extended, so a negative value sets all the upper bits to 1. That happens to generate an illegal address that the CPU traps immediately.Neither clearing nor setting the upper bits is correct, though, so this only appears to work.If you try to use it in production, you’ll get either unpredictable crashes or memory-corruption bugs.
The type you actually want to hold a pointer is
uintptr_t(orintptr_t, but either I want to work with unsigned addresses or signedness doesn’t matter). To format a pointer argument forprintf()orsnprintf(), cast to(void*)and use a%pspecifier. And never usesprintf(). It’s unsafe because it doesn’t check the buffer size.0
u/sciencekm Aug 18 '26
My guess is that your
intis 16-bit, hence defining PTRTYPE asintwill behave differently vs defining it asuint32_t.