I'm trying to store a list of every mount point on a Linux system in a string array with C. I'm focused on this piece of code.
int i = 0;
char **mountslist = malloc(1024 * sizeof(char *));
/*
* Make sure that the number entries in the array are less than the allocated
* 1024 in the absurd case that the system has that many mount points.
*/
while (i < 1024 && (ent = getmntent(mounts))) {
/*
* The output of getmntent(mounts) goes down to
* the next mount point every time it is called.
*/
mountslist[i] = strdup(ent->mnt_dir);
i++;
}
I was wondering how I could dynamically allocate the number of entries in the mountslist array (currently statically set at 1024) to avoid that limit and wasting memory. If I had the final value of i when declaring mountslist I could use char *mountslist[i]; or char **mountslist = malloc(i * sizeof(char *));
malloc(i *.... The first one is a non standard c extension (variable length array)realloc.reallocif you're going to code in C.