Dynamic array inside struct and malloc fail

Nitkov

Consider the following abstracted code that reads some bytes from a file:

typedef struct A{
int size;
char * dataArray;
}A

A load(char* filename, int inSize)
{
    A newA;
    newA.size = inSize;
    FILE *filePtr;
    filePtr = fopen(filename,"rb");

    char buff[1];
    int i = 0;

    newA.dataArray = ( char*)malloc(sizeof(char) * newA.size);
    for (i = 0; i < newA.size; i++)
    {
        fread(buff, sizeof(char), 1, filePtr);
        newA.dataArray[i] = buff[0];
    }

    char* copyOfDataArray = (char*)malloc(sizeof(char) * newA.size);

    for (i = 0; i < newA.size; i++)
    {
        fread(buff, sizeof(char), 1, filePtr);
        copyOfDataArray[i] = newA.dataArray[i];
    }

    newA.dataArray = copyOfDataArray;
    return newA
}

void Initialize()
{
    A first = load("file1", 100);
    A second = load("file2", 20);
}

Both calls to function load return the expected result (data array has the same bytes as the file). Variables first and second are never used again.

However after a couple of hundreds lines of code the program always crashes with:

*malloc.c:2451: sYSMALLOC: Assertion '(old_top == (..... failed.*

The crash always occurs on the same line of code, but that line has nothing to do with variables first, second or even with struct A whatsoever.

My question is: is my way of instancing and loading 'first' and 'second' wrong? Can it cause some kind of memory leak / memory overflow that crashes the program long after the load function has finished?

Bonus: The crash does not occur if I only load "file1", as soon as i load both "file1" and "file2" the crash reappears.

Sorry for the long question.

vookimedlo

You have memory leaks there. You have to free the previously allocated memory in newA.dataArray, before you assign there a new memory.

As stated by Joachim, read operation is very time consuming and you shall read data in blocks to minimize overhead.

Additionally, you have to close file descriptors, otherwise they will be depleted soon.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Malloc an array inside a struct

From Dev

Replacing the value of dynamic array inside a Solidity Struct

From Dev

C - dynamic array of typedef struct with in-function malloc

From Dev

Malloc 'ing array of struct in struct

From Dev

c malloc array of struct

From Dev

How do I use malloc for an array of structs inside another struct that has been created using malloc

From Dev

malloc'ing for field inside struct

From Dev

C, How to malloc the correct amount of space for an array of a struct inside another struct?

From Dev

Dynamic array without malloc?

From Dev

How to initiliaze a dynamic 2D array inside a struct in c?

From Dev

can i use "int" as my dynamic array inside a struct?

From Dev

C dynamic allocation of an array under struct inside a function

From Dev

How to implement a dynamic 2D array inside a struct in Rust?

From Dev

malloc of array in struct passed as argument

From Dev

c++ malloc for array in struct

From Dev

Malloc and memcpy struct plus array

From Dev

Creating dynamic struct arrays using malloc

From Dev

Malloc'ing a double pointer structure inside a struct?

From Dev

malloc'd pointer inside struct that is passed by value

From Dev

Dynamic array using malloc and realloc?

From Dev

Iterator on dynamic array in a struct

From Dev

Swift - Dynamic Array of Struct

From Dev

Dynamic array length in struct

From Dev

Array assignment inside a Struct

From Dev

realloc of array inside a struct

From Dev

Reallocating array inside struct

From Dev

Array inside struct

From Dev

Passing malloc struct array through a function

From Dev

how to do malloc to array from struct