Somebody kindly helped me a couple days ago with a large part of this in another SO question. At that time, the size of struct base
was known in advance. I'd like to alter member path
to be a character array the size of which isn't known until right before allocating memory for a new instance of base
in the base_new()
function.
In the previous version, all files were required to be stored in the same directory and only the file name was added; and it was limited to length 256. Now I'd like to permit user added subdirectories under the ../../databases
directory and not limit the length.
Is it possible to set the size of path
before or after the db[i] = malloc( sizeof ( struct base ) )
in base_new()
?
Or, perhaps, I should simply ask, how can this be accomplished?
Thank you.
/* Global declaration */
struct base {
...
char path[];
};
struct base **db;
/* in main() */
db = malloc( n * sizeof *db );
for (size_t i = 0; i < n; ++i)
db[i] = NULL;
/* Function to assign pointer db[i] to newly allocated struct base */
int base_new( void )
{
/* declarations */
// Assign pointer to beginning of memory allocation for the new instance of struct base.
if ( ( db[i] = malloc( sizeof ( struct base ) ) ) == NULL )
{
printf( "Error attempting to malloc space for new base.\n" );
return 1;
}
// When size of path was fixed, just truncated name to 256. */
l = sizeof( db[i]->path );
rc = snprintf( db[i]->path, l, "%s%.*s", "../../databases/", 256, name );
if ( rc > l - 1 || rc < 0 )
{
// Not enough space; truncate and add the '\0' at l.
db[i]->path[l] = '\0';
}
// When size of path variable and writing path. */
l = sizeof( db[i]->path ) - 16;
rc = snprintf( db[i]->path, l, "%s%s", "../../databases/", path );
if ( rc > l - 1 || rc < 0 )
{
db[i]->path[l] = '\0';
}
}
I got a message at the top of my question asking if an existing question answers this one. It is closely related and helpful, but the answer I received here is better I think and discusses a few other related points. I don't know how it is supposed to work but I picked No because this answer is better, or at least I can understand it better. This answer shows how to malloc
the variable member of the struct and discusses freeing the memory of the struct by member before freeing the pointer to the struct. The other question is a bit more general but still helpful. Thanks.