Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Free(buffer) gives Memory fault(coredump) 2

Status
Not open for further replies.

DCCoolBreeze

Programmer
Jul 25, 2001
208
US
Interesting thing is happening to my application. I have a loop that simply runs through a directory reading all .txt files. When the application locates a .txt file, it allocates (malloc) enough memory to read the file contents into a buffer (buffer). Then I simply free the memory; close the file and move to the next. The application works fine up to the 11th file. The 11th file is read and everything works well until it attempts to free the buffer memory. Then I get a message that states Memory fault(coredump). If I remove the free(buffer) statement, the application reads all 37 files without problems...of course I am now gobbling up memory. Has anyone ever experienced this error and have an idea what is wrong. If I compile the same application in Windows and run it, it runs fine. I am including my code for reference...

In addition, I cannot seem to get the clock function to work properly...

#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <malloc.h>

#include <fcntl.h> /* needed for open mode arguments */
#include <sys/stat.h> /* need for file status flags */
#include <sys/types.h> /* needed for file types */

main()
{

char pathName[256];
char fullName[256];
char fileName[256];
char header[256];
char *output;
char *ptr;

int pathNameLength;
int bufferLength;
int stringLength;

long idx;
long counter=0;

int source;
char *buffer;
long bytes_read;

struct stat file_info;

long t1;
long t2;
double rate;

DIR *dir;
struct dirent *ent;

strcpy(pathName,&quot;/dev/&quot;);
pathNameLength = strlen((const char *)pathName);

printf(&quot;Analyzing '%s':\n&quot;,pathName);


if ((dir = opendir(pathName)) == NULL)
{
perror(&quot;Unable to open directory&quot;);
exit(1);
}

t1 = clock();
while ((ent = readdir(dir)) != NULL)
{
if (strstr(ent->d_name,&quot;.txt&quot;) != NULL)
{
strcpy(fullName,pathName);
strcat(fullName,&quot;/&quot;);
strcat(fullName,ent->d_name);
printf(&quot;file(%d)=%s\n&quot;,++counter, fullName);


if ((source = open(fullName, O_RDONLY)) == -1)
{
printf(&quot;unable to open file\n&quot;);
exit(1);
}

if (fstat(source,&file_info) == -1)
{
printf(&quot;unable to get stats on file\n&quot;);
close(source);
exit(2);
}

if ((buffer = (char *)malloc(file_info.st_size)) == NULL)
{
printf(&quot;Unable to allocate %d memory\n&quot;,file_info.st_size);
close(source);
exit(3);
}


if ((bytes_read=read(source,buffer,file_info.st_size)) <= 0)
{
printf(&quot;could not read data\n&quot;);
close(source);
exit(1);
}
printf(&quot;bytes_read=%d\n&quot;,bytes_read);
buffer[bytes_read] = 0x00;

for (idx=0; idx<255; idx++)
{
header[idx]=buffer[idx];
if (buffer[idx] == 0x0A) break;
}
header[++idx] = 0x00;

ptr = &buffer[idx];
close(source);
free(buffer);

}

}

t2 = clock();
printf(&quot;time to complete: %d %d %d => %d\n&quot;,
t1,t2,t2-t1,CLOCKS_PER_SEC);
if (closedir(dir) != 0)
perror(&quot;Unable to close directory&quot;);
}

 
The application actually does open the 11th file and it does allocate the proper amount of space but free() does not like the 11th file buffer for some reason...maybe we are in the 11th hour of the 11th day of the 11th month of the 11th year of the 11th century of the 11th millenium... :eek:)
 
DCCoolBreze,

The line in your program

buffer[bytes_read] = 0x00

writes one byte outside of the allocated buffer. Malloc() probably stores some information at the end of the buffer which it uses in the free() call (possibly to catch such errors).

Why the 11th file? Perhaps becasue malloc gives you at least as much memory as you ask for. For the 11th file it gives you exactly enough memory. My guess is that the 11th file is divisible by some power of two that the others are not.

Hope this helps,

Brudnakm
 
Yes brudnakm is right. One more point.

for(idx = 0; idx < 255; idx++)
...

In your code the idx loop will be executed upto 254 if the given if statement within that is not satisfied, but here you have to check the size of the buffer ( the size of the file). If the size of the file is less than 255 byes and the if statement within the for(idx ... )is not satisfied then it will cause problems.

the header[++idx] = ... will also cause problems if the for (idx = 0; ... loop executes upto 254. Because at the end of for loop idx value will be 255 and header[++idx] will be header[256] but what is the size header ?.

Maniraja S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top