realloc() : invalid next size Aborted (core dumped)

I’m Tryng to write a code that have to expand the memory of a malloc array of 1 for some cicles of a loop (i need to store the zeros of a function). but after compiling this error occurs: realloc(): invalid next size Aborted (core dumped) have someone an idea of the reasons of this… Read More realloc() : invalid next size Aborted (core dumped)

Undefined Behavior on inner realloc()

I’m attempting to write a PE parser, and my program contains two loops as shown below: size_t i = 0; while (condition_1) { struct _IMAGE_IMPORT_DESCRIPTOR64 *importDescriptorArray = malloc(sizeof(struct _IMAGE_IMPORT_DESCRIPTOR64)); struct _IMAGE_THUNK_DATA64 *originalFirstThunkArray = malloc(sizeof(struct _IMAGE_THUNK_DATA64)); size_t originalFirstThunkArrayIndex = 0; originalFirstThunkArray[originalFirstThunkArrayIndex].u1.ordinal = some_value; //set to a computed value while (condition_2) { originalFirstThunkArrayIndex++; originalFirstThunkArray = realloc(originalFirstThunkArray, originalFirstThunkArrayIndex… Read More Undefined Behavior on inner realloc()

Confusion about using malloc/calloc pointer to hold the return value of realloc

Someone said that using the original malloc()/calloc() pointer as the variable that holds the return value of realloc() is wrong. Here is an example: #include <stdlib.h> int main() { int *malloc_ptr = malloc(5 * sizeof(int)); malloc_ptr = realloc(malloc_ptr, 10 * sizeof(int)); return 0; } Their reasoning was that if realloc() failed it would return NULL… Read More Confusion about using malloc/calloc pointer to hold the return value of realloc

Realloc throws stack overflow sporadically in program that calculates large integers

void inf_int::Sub(const char num, const unsigned int index) { if (this->length < index) { char* tmp = (char*)realloc(this->digits, index + 1); if (tmp == NULL) { cout << "Memory reallocation failed, the program will terminate." << endl; free(this->digits);// provided that the destructor does not call free itself exit(0); } this->digits = tmp; this->length = index;… Read More Realloc throws stack overflow sporadically in program that calculates large integers