Runtime error when trying to allocate more than 3 kiB of memory

I've fixed the problem, but I'll leave this thread here so that anyone having the same issue can fix theirs. The problem, as it happens, was caused by something I was doing with a buffer earlier on in the program that resulted in heap abuse.

In the interpreter/virtual machine I'm writing, I want to allocate ~64 kiB of main memory and 16 kiB of video memory for the machine to use. So I defined the following:
sint.h
9
10
11
12
13
14
#ifndef MEMORYSIZE
# define MEMORYSIZE	65536 /* 64 kiB */
#endif
#ifndef FRAMEBUFSIZE
# define FRAMEBUFSIZE	16384 /* 16 kiB */
#endif 

and then wrote the following:
sint.c
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * \brief Set up the virtual machine and run the interpreter
 * \param bytecode A buffer containing opcodes and their arguments
 * \param bc_nelem Number of elements in bytecode
 * \return 0 on success or -1 on error
 */
int sint(const char* bytecode, register uint32_t bc_nelem)
{
	int ret = 0;
	struct register_state rstate;
	char* memory = malloc(MEMORYSIZE);
	char* framebuf = malloc(FRAMEBUFSIZE);
	memset(&rstate, 0, sizeof(rstate));

	ret = sint_interpret(&rstate, memory, MEMORYSIZE, framebuf,
			    FRAMEBUFSIZE, bytecode, bc_nelem);

	free(memory);
	free(framebuf);

	return 0;
}


When I use this code, however, I get the following error:
Sint: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) -
__builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned 
long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && 
((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted


If I set MEMORYSIZE and FRAMEBUFSIZE thus:
sint.h
9
10
11
12
13
14
#ifndef MEMORYSIZE
# define MEMORYSIZE	3072 /* 3 kiB */
#endif
#ifndef FRAMEBUFSIZE
# define FRAMEBUFSIZE	768 /* 1 kiB */
#endif 


then the program runs.

I checked the program's memory consumption during runtime (with the second set of values for MEMORYSIZE and FRAMEBUFSIZE) and it was 612 kiB. I don't think an extra 80 kiB is too much, is it? I have ~5.8 GiB in total.

What else could be causing this problem? I really need to fix it.
Last edited on
hm, what's the solution? And what's so interesting about that post?
Topic archived. No new replies allowed.