DCCoolBreeze
Programmer
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,"/dev/"
;
pathNameLength = strlen((const char *)pathName);
printf("Analyzing '%s':\n",pathName);
if ((dir = opendir(pathName)) == NULL)
{
perror("Unable to open directory"
;
exit(1);
}
t1 = clock();
while ((ent = readdir(dir)) != NULL)
{
if (strstr(ent->d_name,".txt"
!= NULL)
{
strcpy(fullName,pathName);
strcat(fullName,"/"
;
strcat(fullName,ent->d_name);
printf("file(%d)=%s\n",++counter, fullName);
if ((source = open(fullName, O_RDONLY)) == -1)
{
printf("unable to open file\n"
;
exit(1);
}
if (fstat(source,&file_info) == -1)
{
printf("unable to get stats on file\n"
;
close(source);
exit(2);
}
if ((buffer = (char *)malloc(file_info.st_size)) == NULL)
{
printf("Unable to allocate %d memory\n",file_info.st_size);
close(source);
exit(3);
}
if ((bytes_read=read(source,buffer,file_info.st_size)) <= 0)
{
printf("could not read data\n"
;
close(source);
exit(1);
}
printf("bytes_read=%d\n",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("time to complete: %d %d %d => %d\n",
t1,t2,t2-t1,CLOCKS_PER_SEC);
if (closedir(dir) != 0)
perror("Unable to close directory"
;
}
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,"/dev/"
pathNameLength = strlen((const char *)pathName);
printf("Analyzing '%s':\n",pathName);
if ((dir = opendir(pathName)) == NULL)
{
perror("Unable to open directory"
exit(1);
}
t1 = clock();
while ((ent = readdir(dir)) != NULL)
{
if (strstr(ent->d_name,".txt"
{
strcpy(fullName,pathName);
strcat(fullName,"/"
strcat(fullName,ent->d_name);
printf("file(%d)=%s\n",++counter, fullName);
if ((source = open(fullName, O_RDONLY)) == -1)
{
printf("unable to open file\n"
exit(1);
}
if (fstat(source,&file_info) == -1)
{
printf("unable to get stats on file\n"
close(source);
exit(2);
}
if ((buffer = (char *)malloc(file_info.st_size)) == NULL)
{
printf("Unable to allocate %d memory\n",file_info.st_size);
close(source);
exit(3);
}
if ((bytes_read=read(source,buffer,file_info.st_size)) <= 0)
{
printf("could not read data\n"
close(source);
exit(1);
}
printf("bytes_read=%d\n",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("time to complete: %d %d %d => %d\n",
t1,t2,t2-t1,CLOCKS_PER_SEC);
if (closedir(dir) != 0)
perror("Unable to close directory"
}