This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Bug in Calloc??


hi,
pl excuse me if this is not the right place to talk about it...

i'm suspecting that the implementation of 'calloc' i'm using is buggy. i
got the binaries for gcc (2.95.3) and glibc with Mandrake 7.1--

[soubhik@cseproj22 test]$ gcc -v 
Reading specs from /usr/lib/gcc-lib/i586-mandrake-linux/2.95.3/specs gcc
version 2.95.3
19991030 (prerelease) 
[soubhik@cseproj22 test]$  

now in my program i'm allocating an array to a pointer using
'calloc'. then passing the address of an element of the array to a
function. in that function i have another pointer. i'm callocing to this
second pointer too. and to my surprise i discovered that the address
assigned to this pointer is same as the address passed to the
function.....

here's the piece of code:

//caller

local_map=(uint8 *) calloc((4*j+1), sizeof(uint8));  //'j' is unsigned
							//long long
ret_val= analyze_reg_expr ( &(local_map[2*j]), 2*j, and_tbl_index,
			mode_tbl_index, dest_pretable);

//now callee

int32 analyze_reg_expr(uint8 *reg_map, uint64 len, uint32 and_tbl_index,
		uint32 mode_tbl_index, PrefixTuple_t *dest_pretable)
{

	uint8 *local_map[2];

	local_map[0]=(uint8 *) calloc((2*len+1), sizeof(uint8));
	local_map[1]=(uint8 *) calloc((2*len+1), sizeof(uint8));

	printf("reg_map=%p local_map[0]=%p local_map[1]=%p\n", reg_map,
		local_map[0], local_map[1]);

	//some more stuff

	free(local_map[1]); //setting MALLOC_CHECK_=1 causes to print a
				//message 'Invalid Pointer' here
	free(local_map[0]);
	
	return 0;
}


now the output:

reg_map=0x81619c0 local_map[0]=0x81619b0 local_map[1]=0x81619c0

note that address stored in 'reg_map' is same as the address assigned to
'local_map[1]' !!
not only this, the array allocated by calloc is not properly initialized--
it contains some bytes with nonzero values!! i observed it while debugging
with gdb.....

the problem vanished as i replaced each 'calloc' by a pair of 'malloc' (to
allocate memory) and 'memcpy' (to initialize) -:)...

Regards,
soubhik.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]