All pastes #1060173 Raw Edit

c malloc()

public c v1 · immutable
#1060173 ·published 2008-07-02 03:54 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>int main() {	char * cp = 0;	unsigned int * uip = 0;	unsigned int cp_size = sizeof(char *) * 8;	unsigned int uip_size = sizeof(unsigned int *) * 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 32Q: Why does malloc #2 OVERLAP #1?Do I need to somehow "lock" my malloc-allocated memory?Any help would be greatly appreciated.*/