Skip to main content
gamedev.net
🔒 Locked

WTF? malloc problem...

Started by Lethe Jul 4, 2004 at 6:16 AM 5 replies 950+ views
Original Post
Lethe
Lethe
Hello all, In an effort to find memory leaks in my code I put together my own wrappers arround malloc/free etc, to put buffer zones arround memory, log to file, keep track of how much memory is in use etc. Anyway, as part of that I use heapcheck & heapchecknode to localise any problems, the problem is any malloc over 1 meg in size is allways a bad node. To test this wasn't a side effect of my program somewhere I wrote the folowing test program:

#include <malloc.h>
#include <alloc.h>
#include <mem.h>
#include <stdio.h>
#include <conio.h>

void testSize(int size)
{
	void * tmp = malloc(size);
	printf("(%i) ",(int)tmp);
	int ret = heapchecknode(tmp);
	switch (ret)
	{
		case _USEDENTRY: printf("used entry (ok)\n"); break;
		case _HEAPCORRUPT: printf("heap corrupt\n"); break;
		case _BADNODE: printf("bad node\n"); break;
		case _BADVALUE: printf("bad value\n"); break;
		case _HEAPBADBEGIN: printf("heap bad begin\n"); break;
		case _HEAPBADNODE: printf("heap bad node\n"); break;
		case _HEAPBADPTR: printf("heap bad ptr\n"); break;
	}
	free(tmp);
}

int main()
{
	for (int i=1022;i<1026;i++)
	{
	 printf("%i: ",i);
	 testSize(i*1024);
	}

	getch();
	return 0;
}

And compiled it with 'bcc32 main.cpp' It outputs: 1022: (8661496) used entry (ok) 1023: (8661496) used entry (ok) 1024: (12845060) bad node 1025: (12845060) bad node Can anyone explain it to me? I'm using the free BCC32 5.5 compiler. Thanks in advance for any inteligence beyond my own, Lethe
kdogg
kdogg
One point that might or might not fix your problem. An int on a 32 bit machine only ranges up to about 65K. You might want to try using an unsigned int for size instead.
ApochPiQ
ApochPiQ
Quote:
Original post by kdogg
One point that might or might not fix your problem. An int on a 32 bit machine only ranges up to about 65K. You might want to try using an unsigned int for size instead.


Incorrect. In C++, int is defined to be the native word size of the target processor - 32 bits in the case of 32-bit platforms. A 32 bit integer has a range of -2,147,483,648 to 2,147,483,647 inclusive.

See also MSDN.

[afterthought] You may be thinking of a 16-bit int, in which case the range is IIRC -32,768 to 32,767 inclusive. An unsigned 16-bit int ranges up to 65,536. So you're a bit off in either case [wink]
nmi
nmi
Quote:
Original post by ApochPiQ
Incorrect. In C++, int is defined to be the native word size of the target processor - 32 bits in the case of 32-bit platforms. A 32 bit integer has a range of -2,147,483,648 to 2,147,483,647 inclusive.

See also MSDN.

[afterthought] You may be thinking of a 16-bit int, in which case the range is IIRC -32,768 to 32,767 inclusive. An unsigned 16-bit int ranges up to 65,536. So you're a bit off in either case [wink]


C++ does not make any assumptions on type sizes but that:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

In most cases an int has the size of a machine word, however do not rely on this.

P.S.: An unsigned 16 bit integer ranges from 0 to 65535.
Anon Mike
Anon Mike
64-bit versions of MSVC use a 32-bit int.
-Mike
kdogg
kdogg
my bad. i'm retarded and got my math all off. either way, it should be an unsigned int.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.