Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
The code compiles but I get a Kern_Protection_Failure and fails at the fread.
Suggestions? Thanks

typedef struct matchedfilter{
int col;
int row;
float ** filters;
} matchedfilter;



struct dirent *filterfile;
DIR *ld;
matchedfilter *mfilters;
int filtnum = 0;
FILE * fp;
char * filename;

ld = (DIR *) calloc (1, sizeof(DIR));
mfilters = (matchedfilter *)calloc(LENSLETS_SIZE*LENSLETS_SIZE, sizeof(matchedfilter));
filename = (char*) calloc(25, sizeof(char));
ld = opendir(filterdir);

while ( (filterfile = readdir(ld)) != NULL)
{
if ( strstr(filterfile->d_name, "txt") != NULL)
{
strcpy(filename,filterdir);
strcat(filename,filterfile->d_name);
if (fp = fopen(filename, "r") != NULL)
{
printf("%s\n", filename);
sscanf(filterfile->d_name, "%d-%d.txt", &col, &row);
mfilters[filtnum].col = col;
mfilters[filtnum].row = row;
mfilters[filtnum].filters= (float **)malloc(NWAVES * sizeof(float *));
for (j = 0; j < NWAVES; j++)
{
if (mfilters[filtnum].filters[j] = (float *)malloc((12 + 3*j) * sizeof(float)) != NULL);
fread(mfilters[filtnum].filters[j], 1, (12 + 3*j)*sizeof(float), fp);
}
}
}
filtnum++;
}

printf("%d\n", filtnum);
 
First of all, you're leaking memory in this section. You are allocating memory for a single DIR type and then abandoning it without freeing (or using) it.
Code:
ld = (DIR *) calloc (1, sizeof(DIR));
.
.
.
ld = opendir(filterdir);

As for the rest of the code, it would be helpful if you would explain what you are trying to accomplish instead of forcing us to guess and please use code tags!
 
reply

Thanks for responding.

The code simply reads information stored in files into number of structures that hold the data.
You're right that I don't free ld but I do use it in the readdir command. In any event, I don't have to free it until the end of the program.
 
Code:
typedef struct matchedfilter{
	int col;
	int row;
	float ** filters;
	} matchedfilter;



struct dirent  *filterfile;
DIR *ld;
matchedfilter *mfilters;
int filtnum = 0;
FILE * fp;
char * filename;

ld = (DIR *) calloc (1, sizeof(DIR));
mfilters = (matchedfilter *)calloc(LENSLETS_SIZE*LENSLETS_SIZE, sizeof(matchedfilter));
filename = (char*) calloc(25, sizeof(char));
ld = opendir(filterdir);

while (  (filterfile = readdir(ld)) != NULL)
	{
	if ( strstr(filterfile->d_name, "txt") != NULL)
		{
		strcpy(filename,filterdir);
		strcat(filename,filterfile->d_name);
		if (fp = fopen(filename, "r") != NULL)
			{
			printf("%s\n", filename);
			sscanf(filterfile->d_name, "%d-%d.txt", &col, &row);
			mfilters[filtnum].col = col;
			mfilters[filtnum].row = row;
			mfilters[filtnum].filters= (float **)malloc(NWAVES * sizeof(float *));
			for (j  =  0; j < NWAVES; j++)
				{
				if (mfilters[filtnum].filters[j] = (float *)malloc((12 + 3*j) * sizeof(float)) != NULL);
					fread(mfilters[filtnum].filters[j], 1, (12 + 3*j)*sizeof(float), fp);
				}
			}
		}
	filtnum++;
	}

printf("%d\n", filtnum);

This was just posted with code tags for readability. I will look into what's actually going on now. And Guiyon was right about the opendir call. This is its signature:
Code:
DIR *opendir(const char *dirname);

So you are assigning the value of the pointer that is returned to your pointer ld, so the original value of ld is not available, so you will never be able to free it. The root of the issue is, though, why do you need to allocate space for ld at all?

Also, why alloc space for filename? Are you going to need it outside of the scope of this function? If not, why not just use a stack local variable so you don't have to remember to free it?

-Lee
 
got it

All the code is right, it is improved by the placement of some additional parentasees. Compiler warnings are sometimes more than warnings.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.