Can somebody explain malloc's behaviour?

Hello here

1
2
3
4
5
6
7
        int result = 0 ;
	int *ptr = (int *)malloc(sizeof(int) * 2);
	while (*ptr)
	{
		*ptr++;
		result++;
	}

Above , result = 6
1
2
3
4
5
6
7
        int result = 0 ;
	double*ptr = (double*)malloc(sizeof(double) * 2);
	while (*ptr)
	{
		*ptr++;
		result++;
	}

Above , result = 4

How can this be ?
Last edited on
what are you not showing us?
The code snippet you have posted i see no relationship between you malloc call and your variable result.
Man , i just wanted to check pointer's space allocated in the memory.That's why i put result in there
Last edited on
cuz each time you while the increment will be different,

ptr++
in the first case is not the same in the second.
Last edited on
The value of result isundefined as the memory pointed to by ptr is determined at runtime and its content is uninitialised.
Last edited on
Man , i just wanted to check pointer's space allocated in the memory.That's why i put result in there

why even bother with a while loop then?
Hmm , so nooby question i asked i am sorry. I think i should give coding up for a while.
Ty my friend.
I think i should give up coding too :/
Topic archived. No new replies allowed.