Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If array sizes can only be a constant value than what does char d_name[...] mean?

Tags:

arrays

c

linux

If array sizes can only be a constant value than what does

   char d_name[...] 

mean?

Actually, there is a struct dirent declared in dirent.h file. its declaration is as under:

struct dirent{
  ....
  ino_t d_ino;
  char d_name[...];
  ...
  };

It is used to read directory contents one at a time i.e. inode numbers and filenames etc...

I mean what is the max size of such an array and how much space is statically allocated in the memory once such an array is defined? Is such a definition portable?

like image 651
bhuwansahni Avatar asked Jan 18 '23 06:01

bhuwansahni


1 Answers

Assuming it's from struct linux_dirent, it's actually char d_name[] :

struct linux_dirent {
    unsigned long  d_ino;     /* Inode number */
    unsigned long  d_off;     /* Offset to next linux_dirent */
    unsigned short d_reclen;  /* Length of this linux_dirent */
    char           d_name[];  /* Filename (null-terminated) */
}

It's called a flexible array member, using malloc you can allocate more memory to the struct giving d_name a variable size.

EDIT

The text the OP is quoting:

Directory entries are represented by a struct dirent

struct dirent {
    ...
    ino_t d_ino;            /* XSI extension --- see text */
    char  d_name[...];      /* See text on the size of this array */
...
};

With the ... the authors signals the size isn't fixed per standard. Each implementation must choose a fixed size, for example Linux chooses 256. But it's not valid code.

like image 172
cnicutar Avatar answered Jan 29 '23 10:01

cnicutar