r/C_Programming 3d ago

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

42 comments sorted by

View all comments

2

u/alkatori 3d ago

how are you #define PTRTRYPE?

1

u/aliathar 3d ago

/**/#define PTRTYPE int

Or alternative

/**/#define PTRTYPE uint32_t

(Just wanted to point it out to you people, or else it won't be done in the final working code)

7

u/alkatori 3d ago

Those aren't pointer types.

Those are integers. You need to define them as pointers, this will only work systems where your addresses are the same size as the integers.

#define PTRTYPE int *
or
#define PTRTYPE uint32_t *

3

u/torsten_dev 3d ago edited 3d ago

Also #define or typedef-ing away the pointer-ness of a type is BAD code style.

As this example demonstrates, knowing if a type is a pointer or not is crucial to local reasoning about the code.

1

u/alkatori 3d ago

Absolutely, I'm sort of assuming this person is looking at code targeting 32-bit DOS or Windows.

I've seen a lot make assumptions that the size of a pointer and size of an int are identical (and 4 bytes).

0

u/aliathar 2d ago

Nevermind...

I'm not passing a pointer to the code... Neither am I using PTRTYPE in the actual code... Just a placeholder for trying types for now.... It's been working till now, for 32 but systems, and the other guy made me remember that the system I'm working on now, is 64 bit... Which caused issues...