u/alex_sakuta

Strange data reset in C program

Strange data reset in C program

Disclaimer: When you see the below functions, don't worry about their implementations, I know I have implemented them incorrectly. Skip to the explanation below.

#include <stdio.h>
#include <stddef.h>

struct header {
	size_t cap;
	size_t len;
};
#define header(pointer) ((struct header *) pointer - 1)

size_t cap(const void *const obj) {
	if (obj != NULL) {
		return header(obj) -> cap;
	}

	return 0;
}

size_t len(const void *const obj) {
	if (obj != NULL) {
		return header(obj) -> len;
	}

	return 0;
}

int main() {
	const struct {
		struct header hdr;
		int *buff;
	} obj = {
		.hdr = {
			.cap = 4,
			.len = 4
		},
		.buff = (int[]) { 1, 2, 3, 4 }
	};
	const int *const arr = ((
		struct {
			struct header hdr;
			int *buff;
		}
	) {
		.hdr = {
			.cap = 4,
			.len = 4
		},
		.buff = (int[]) { 1, 2, 3, 4 }
	}).buff;

	printf("\
			\rlen(&obj.buff) : %zu, cap(&obj.buff) : %zu\n\
			\rlen(&arr) : %zu, cap(&arr) : %zu\n\
	\r", len(&obj.buff), cap(&obj.buff), len(&arr), cap(&arr));

	return 0;
}

This is just a program demonstrating something I have been trying and an interesting thing I noted in my attempts to create it.

godbolt link

I am creating 2 identical objects. However, somehow they are acting differently.

My output of this program is returning the length and capacity values from &obj.buff but not from &arr. This is the exact output:

bin/main.exe	
len(&obj.buff) : 4, cap(&obj.buff) : 4	
len(&arr) : 8, cap(&arr) : 17179869187

So, why is that one of them is correctly returning the length and capacity values and the other is not?

My guess is that maybe C is resetting the data that it allocated since I am using only a part of that allocated data. However, I can't seem to verify this.

The output is compiled using -O3 but I tried removing that flag too and it didn't change the output.

Any help is appreciated.

PS: Yes the implementation of the function may seem incorrect, ignore that for now, I changed it, this was a previous implementation that I noticed this strange behaviour in.

u/alex_sakuta — 2 days ago