0

I have a struct defined like this

typedef struct {
   char* Value;
   unsigned int Length;
} MY_STRUCT;

I'm creating an array of these structs using calloc:

MY_STRUCT* arr = (MY_STRUCT*)calloc(50, sizeof(MY_STRUCT));

Then, in a loop, I'm accessing each struct and trying to allocate and assign a value to the Value field using calloc and memcpy:

int i;
for(i = 0; i < 50; i++)
{
    MY_STRUCT myStruct = arr[i];
    int valueLength = get_value_length(i);//for sake of example, we can assume that this function returns any value [1-99]
    myStruct.Length = valueLength;
    myStruct.Value = (char*) calloc(valueLength, sizeof(char));
    memcpy(myStruct.Value, get_value(i), valueLength); //assume get_value(i) returns char* pointing to start of desired value
}

This code block crashes on the calloc line with Visual Studio indicating heap corruption. It doesn't fail the first time through the loop. Instead, it fails on the second pass when I'm trying to allocate a length 20 char array (first pass is length 5). I've tried using malloc as well, and I've tried using recommendations in:

Heap Corruption with malloc, struct and char *

Do I cast the result of malloc?

Nothing seems to mitigate the problem. I am originally a managed code programmer so my knowledge of memory allocation and management is not always the best. I'm sure I'm doing something boneheaded, but I'm not sure what. Any help would be greatly appreciated. Thank you!

JayHay23
  • 38
  • 1
  • 4
  • I can't seem to duplicate your crash. Though I have no reference to `get_value_length` and `get_value` functions, I replaced them with `42` and `myStruct.Value` respectively. I complied it with `-std=c11` and no crash? – Miket25 Jun 02 '17 at 14:31
  • 4
    Don't cast the result of `calloc()`. Doing so causes the same problem as casting the result of `malloc()`. Your loop does nothing to change the elements of `arr` - changing `myStruct` has no effect on `arr[i]`. Beyond those two factors, nothing will explain your problem unless `calloc()` is returning `NULL`, or the offender is in some code you have excluded. Try providing an [mcve]. – Peter Jun 02 '17 at 14:33
  • 2
    Most probably, the source of corruption is somewhere else. Like @Miket25 says, this code should not produce corruption on its own. You should extract the code you show, plus `get_value_length()` and `get_value()`, into a standalone program and test it. I'd be surprised if it reproduces your problem. So, the problem is almost certainly not in the code you show but rather in some code you don't show. As @Peter points out, you should be using something like `MY_STRUCT *myStructPtr = &arr[i];` and appropriate changes in the loop. – Jonathan Leffler Jun 02 '17 at 14:33
  • Thanks to all of you for looking into this. I will try to isolate the problem further. This is in a large codebase and I tried to simplify the issue without exposing our actual source. Really appreciate the tips. – JayHay23 Jun 02 '17 at 14:36
  • if this is part of larger code, I suggest linking with some form of debugging malloc library . – user3344003 Jun 02 '17 at 18:49

0 Answers0