1

so I have spent some hours trying to figure out why my realloc doesnt enlarge my array of structs, but I seem to make no progress. Realloc either fails or doesnt enlarge the array. Is there any obvious mistake that Im making?

#include <getopt.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


typedef struct fileInfo {
    char accessRights[12];
    short hardLinks;
    short userName;
    short groupName;
    long size;
    char *time;
    char *fileName;
    short nrOfNode;
} fileInfo;


void enlargeFileInfos(fileInfo *fileInfoArray, int currentSize)
{
    fileInfo *temp = (fileInfo*)realloc(fileInfoArray, (currentSize + 1) * sizeof(fileInfo));
    if (!temp) {
        printf("realloc --FAILED--\n");
        return;
    }
    fileInfoArray = temp;
    printf("fileInfo grew to %d item(s)\n", currentSize + 1);
}

int main( )
{
    size_t nrOfDirs = 1;
    fileInfo *fileInfoArr = malloc(sizeof(fileInfo));

    for (int i = 0; i < 5; i++) {
        enlargeFileInfos(fileInfoArr, nrOfDirs);
        nrOfDirs++;
    }
    return 0;
}
vanderer
  • 13
  • 2
  • 4
    Hint: `void f(int i) {i=6;} int main() {int x=12; f(x); printf("%d\n", x);}` <-- what does this print? – user253751 May 14 '18 at 00:23
  • What makes you think `realloc()` does not enlarge the array (or at least return a pointer to a larger array)? – John Bollinger May 14 '18 at 00:25
  • 2
    You are passing the pointer by value to enlargeFileInfos, main() never receives the new realloc'ed pointer. Either pass &fileInfoArray as fileInfo** (chaging the function declaration), or make the function return the new pointer. – FBergo May 14 '18 at 00:25
  • well, if I run the script, this is the output I get: fileInfo grew to 2 item(s) fileInfo grew to 3 item(s) realloc --FAILED-- realloc --FAILED-- realloc --FAILED-- – vanderer May 14 '18 at 00:29
  • I have tried, what @FBergo suggested, but with no luck. You can find the adjusted code here: [link](https://pastebin.com/ejDXdTx4) – vanderer May 14 '18 at 00:39
  • You used `fileInfoArray` instead of `*fileInfoArray` in the realloc call, the correct line is `fileInfo *temp = (fileInfo*)realloc(*fileInfoArray, (currentSize + 1) * sizeof(fileInfo));` – FBergo May 14 '18 at 00:57
  • @vanderer with the change above it works correctly. – FBergo May 14 '18 at 00:58

1 Answers1

1

To realloc memory on which fileInfoArray points to inside enlargeFileInfos, you have to pass its address to the function:

void enlargeFileInfos(fileInfo **fileInfoArray, int currentSize)
{
    fileInfo *temp = realloc(*fileInfoArray, (currentSize + 1) * sizeof(fileInfo));
    if (temp == NULL) {
        printf("realloc --FAILED--\n");
        return;
    }
    *fileInfoArray = temp;
    printf("fileInfo grew to %d item(s)\n", currentSize + 1);
}

Then you call the function in this way:

enlargeFileInfos(&fileInfoArr, nrOfDirs);

As pointed by Jonathan Leffler in comments, an alternative way is to return the realloced memory from the function enlargeFileInfos:

fileInfo *enlargeFileInfos(fileInfo *fileInfoArray, int currentSize)
{
    fileInfo *temp = realloc(fileInfoArray, (currentSize + 1) * sizeof(fileInfo));
    if (temp == NULL) {
        printf("realloc --FAILED--\n");
        return NULL;
    }
    printf("fileInfo grew to %d item(s)\n", currentSize + 1);
    return temp;
}

And than, you use it this way:

fileInfoArr = enlargeFileInfos(fileInfoArr, nrOfDirs);
if (fileInfoArr == NULL) {
    /* Handle allocation failure */    
}

And after you finish working with fileInfoArr, don't forget to free it:

free(fileInfoArr);

I have removed the cast from realloc, so take a look on Do I cast the result of malloc?, and change the signature of main to int main(void) .

medalib
  • 937
  • 1
  • 6
  • 7
  • 1
    The alternative is: `fileInfo *enlargeFileInfos(fileInfo *fileInfoArray, int currentSize) { …; return fileInfoArray; }` called like: `fileInfo *newInfo = enlargeFileInfos(fileInfoArr, nrOfDirs); if (newInfo != NULL) fileInfoArr = newInfo;` — taking an ordinary pointer and returning it (returning NULL if the allocation fails). – Jonathan Leffler May 14 '18 at 00:59
  • @JonathanLeffler, thanks for the note, I will mention it in my post. – medalib May 14 '18 at 01:04
  • 1
    YW. Check it carefully — it hasn't been near a compiler and could easily have typos in it. – Jonathan Leffler May 14 '18 at 01:05
  • @JonathanLeffler Ok got it :). – medalib May 14 '18 at 01:06