Reputation: 1559
Im writing a small console based application for the windows command line.
Im allocating memory for two arrays, chi
and chi_comp
like this:
/* allocating memory for chi */
if ((chi = (int *)malloc(cm * sizeof(int))) == NULL)
{
printf("Error allocating %i bytes of memory.\n", lm * sizeof(int));
return CMD_NONE;
}
/* allocating memory for chi_comp */
if ((chi_comp = (int *)malloc(cm * sizeof(int))) == NULL)
{
printf("Error allocating %i bytes of memory.\n", cm * sizeof(int));
return CMD_NONE;
}
A bit later in the code I initialize them
for (i = 0; i < cm; i++)
{
chi[i] = -1;
chi_comp[i] = -1;
}
and use them like
chi[grade] = i;
chi_comp[i] = grade;
and
if (chi_comp[i] != -1)
{
printf(" %i ", v[chi_comp[i]][0]);
}
Working with them works fine but when I try to free the memory when I don't need it no more the program crashes.
free(chi);
free(chi_comp);
Debugging with gdb results the following:
warning: HEAP[m.exe]: warning: Heap block at 003518B8 modified at 003518CC past requested size of c
Program received signal SIGTRAP, Trace/breakpoint trap. 0x776f0b2d in ntdll!RtlpNtEnumerateSubKey () from C:\Windows\system32\ntdll.dll
chi in this case values 003518C0
and chi_comp values 003518E8
Upvotes: 1
Views: 2697
Reputation: 134376
Following the comments below the question,
it appears that, in your code, by saying
chi[grade] = i;
you're going out of bounds. Accessing out of bound memory invokes undefined behavior.You should put a check like
if (grade < cm)
chi[grade] = i;
Upvotes: 2
Reputation: 73424
From the error message:
warning: HEAP[m.exe]: warning: Heap block at 003518B8 modified at 003518CC past requested size of c
I suspect you are going out of bounds somewhere in your code, so check your counters please. What tells me that is the past requested size of. Make sure you read twice the error messages, they are for your own good. :)
Next time you post, make sure you provide a minimal example of your bitte.
Upvotes: 2