0

Consider the following code segment:

static int const MAX = 1024; // limit on the number of directory entries
int actual = 0; // counter of directory entires
int ptrsize = sizeof(char*);
int i = 0;

char cwd[1024];
DIR* d;
struct dirent** buffer;
struct dirent** file;

getcwd ( cwd, sizeof ( cwd ) );
d = opendir ( cwd );
buffer = (struct dirent**)malloc(MAX * ptrsize); // allocate the large buffer

/* fill the buffer with dirents for each file in the cwd */
while ( ( buffer[actual] = readdir ( d ) ) != NULL ) actual++;

/* copy the active part of the buffer to the file array and free the buffer */
file = (struct dirent**)malloc(actual * ptrsize);
while ( i < actual ) {
    file[i] = buffer[i];
    i++;
}
free ( buffer );

Would it be more efficient to achieve the same result the following way, using automatic virtual memory?

static int const MAX = 1024; // limit on the number of directory entries
int actual = 0; // counter of directory entires

char cwd[1024];
DIR* d;
struct dirent* file[MAX];

getcwd ( cwd, sizeof ( cwd ) );
d = opendir ( cwd );

while ( ( file[actual] = readdir ( d ) ) != NULL ) actual++;

In case there are 10 files in the directory, 1014 array elements of file won't be used, so nearly 100%. Would it still be more efficient since virtual memory will reclaim empty array elements for other purposes?

mitin001
  • 21
  • 2
  • 1
    Your question seems to be based on an incorrect assumption. Your virtual memory system will not "reclaim" empty array elements. The virtual memory system doesn't know that you haven't put anything in those array elements. You have asked for space to be allocated - therefore it does so. – Greg Hewgill Nov 27 '14 at 23:22
  • Note: `buffer = (struct dirent**)malloc(MAX * ptrsize);` The sizes are not in the right units here. (and the cast is not needed, unwanted, etc) – wildplasser Nov 27 '14 at 23:24
  • Also, your code won't work as written because `readdir()` returns a pointer to temporary data space. From the man page, "The data returned by readdir() may be overwritten by subsequent calls to readdir() for the same directory stream.". – Greg Hewgill Nov 27 '14 at 23:24
  • Also, [Don't cast the result of malloc (and friends)](http://stackoverflow.com/q/605845). – Deduplicator Nov 27 '14 at 23:24

1 Answers1

0

I'll let you answer to you own question with the information about memory I'll give you :

The memory is composed of different parts, and here you speak about 2 off them : the one the computer give (the mapped region, like your char cwd[1024]) and the one you can attribute (the unmapped region, like your struct dirent** file;), all this memory is staked on the "head".

The max-memory of a program is allocated when it's launch. But they're 2 kind of program : the basics ones and the administrators ones. When the second can modify the head by adding or suppressing some memory, the first one can only decrease the max-memory. So, if you have a lot of data (or a little amount of memory available), it'll be hard to do 2 time the assignment of memory when you can have this one 1 time.

A good exercise to understand malloc is to try to create your own malloc. Here an help for that : http://www.inf.udec.cl/~leo/Malloc_tutorial.pdf

And, just for me, if I can avoid to use malloc, I don't use it, so I've less preoccupation about memory leaks. But, it's depend of everyone.

Aridjar
  • 301
  • 1
  • 5
  • 14