char *orig = _strdup(my.GetData("creatureLoads")); // Pulling data from a MySQL table.
while (*orig)
{
switch (*orig)
{
case'[':
howMany = 0;
break;
case'c':
*orig++;
creatureID = atoi(orig);
break;
case'r':
*orig++;
roomID = atoi(orig);
break;
case'x':
*orig++;
howMany = atoi(orig);
break;
}
*orig++;
}
free(orig); // Here is where the program crashes.
Program crashes when I try to free() orig. I'm assuming since it was _strdup()'d into memory, it needs to be free()'d. If I don't cycle thru it's pointers, it free's fine.
That's because you have incremented the pointer so you can't expect free to be able to understand what to do with this random pointer you have given it.
Just save a copy of the pointer to free later if you need it.
It looks correct, but you can throw away those stacked &* since that just dereferences the pointer then takes its address (effectively doing nothing). You can also just free(orig_ptr) at the end instead of assigning it to a different variable first.