Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the pointer to my allocated memory for my type not being realloc'd properly?

I am working on a file system of sorts in my free time and I have ran into an issue with reallocating memory for a pointer to a typedef struct.
file_t:

typedef struct {
    char *fileName;
    FILE *filePointer;
    char *fileContents;
    char *filePermissions;
    size_t fileSize;
    int numFilesUsed;
    char *fileOwner;
    char *fileGroup;
    char *fileCreationDate;
} file_t;

dir_t:

typedef struct {
    char **fileNames;
    int numFiles;
    int *fileSizes;
    int dirSize;
    file_t *files;
    char *path;
} dir_t;

Here is the part that is causing the issue with allocation:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dir_t *currentDir = (dir_t*)malloc(sizeof(dir_t));
for(int i = 0; fileLines[i] != NULL; i++){
    currentDir->files = (file_t *)realloc(currentDir->files, sizeof(file_t) * (i+2)); // I am using i+2 so that I have space for a null
}

When I compile and run, the output is:

EXEC(33314,0x1042f4580) malloc: *** error for object 0x13cf0414a: pointer being realloc’d was not allocated
EXEC(33314,0x1042f4580) malloc: *** set a breakpoint in malloc_error_break to debug Abort trap: 6

I originally thought that this could be an issue with fileLines[i] not having a proper NULL terminator, causing some sort of infinite loop, but it is working fine. Examining the variables on the stack it shows:
Before for loop:

(char **) fileLines = 0x0000600002900000

(dir_t *) currentDir = 0x0000600000c04000

During loop:
i = 0:

(char **) fileLines = 0x0000600002900000

(dir_t *) currentDir = 0x0000600000c04000

Then the abort signal arrives.
The assembly for malloc_error_break:

    libsystem_malloc.dylib`malloc_error_break:
->  0x184876278 <+0>:  pacibsp 
    0x18487627c <+4>:  stp    x29, x30, [sp, #-0x10]!
    0x184876280 <+8>:  mov    x29, sp
    0x184876284 <+12>: nop    
    0x184876288 <+16>: ldp    x29, x30, [sp], #0x10
    0x18487628c <+20>: retab  

Current contents of registers:

General Purpose Registers:
        x0 = 0x0000000000000000
        x1 = 0x000000010020c000
        x2 = 0x0000000000004000
        x3 = 0x0000000184a597aa  libsystem_platform.dylib`___lldb_unnamed_symbol3$$libsystem_platform.dylib + 10
        x4 = 0x0000000000000000
        x5 = 0x0000000000000000
        x6 = 0x0000000000000001
        x7 = 0x0000000000000000
        x8 = 0x0000000010000003
        x9 = 0x000000010020c07c
       x10 = 0xcccccccccccccccd
       x11 = 0x000000000000000a
       x12 = 0x0000000000000000
       x13 = 0x0000000000000033
       x14 = 0x0000000000600000
       x15 = 0x0000000000000002
       x16 = 0xfffffffffffffff4
       x17 = 0x00000001dec1c0f8  (void *)0x0000000184a0bd40: vm_deallocate
       x18 = 0x0000000000000000
       x19 = 0x0000000000000050
       x20 = 0x0000000000000000
       x21 = 0x0000000100208028
       x22 = 0x000000016fdff5b0
       x23 = 0x0000000100208000
       x24 = 0x0000000000000000
       x25 = 0x0000000000000000
       x26 = 0x000000016fdff9df
       x27 = 0x000000010007c580  dyld`_main_thread
       x28 = 0x0000000000000000
        fp = 0x000000016fdff580
        lr = 0x0000000184867d50  libsystem_malloc.dylib`malloc_vreport + 428
        sp = 0x000000016fdff510
        pc = 0x0000000184876278  libsystem_malloc.dylib`malloc_error_break
      cpsr = 0x80000000

What am I not seeing here? Thanks.

>Solution :

While you allocated currentDir with malloc, you did not allocate any memory for currentDir->files to point to. As such, you cannot realloc it.

Allocating memory for a struct that contains pointer members does allocate memory for the pointers, but not memory for them to point to.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading