All pastes #1060185 Raw Edit

c malloc() issue

public c v1 · immutable
#1060185 ·published 2008-07-02 04:11 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>int main() {	char * cp = 0;	unsigned int * uip = 0;	unsigned int cp_size = sizeof(cp) * 8;	unsigned int uip_size = sizeof(uip) * 8;	printf("\n");	if (  cp = (char *)malloc( cp_size )  ) {		printf(" malloc #1 succeeded, start is %u, size %u\n", &cp, cp_size);	} else {		printf(" malloc #1 failed!\n");	}	if (  uip = (unsigned int *)malloc( uip_size )  ) {		printf(" malloc #2 succeeded, start is %u, size %u\n", &uip, uip_size);	} else {		printf(" malloc #2 failed!\n");	}	if ( cp ) { free(cp); }	if ( uip ) { free(uip); }	printf("\n");	return 0;}/*   output on my system is:     malloc #1 succeeded, start is 3217024436, size 32     malloc #2 succeeded, start is 3217024432, size 32   Q: Why does #2 OVERLAP #1?      Do I need to somehow "lock" malloc-allocated memory to        prevent this from happening?*/