A
a.zeevi
free() multiple allocation error in C
====================================
Hi!
I have written a program in C on PC with Windows 2000 in a Visual C
environment.
I have an error in freeing multiple allocation as follows:
1. I allocated an array of pointer.
2. I Read line by line from a text file.
3. I allocated memory for the read line.
4. I wrote the pointer from malloc() to the array of pointers.
5. The problem is in the free() function. When I tried to free in a
loop the
allocated lines, the Visual C throw me with an exception.
6. I wanted to know why?. I tried also to free in the opposite order I
have
allocated them, but this also doesn't work.
7. Does anyone has an idea?
8. Please send answer to (e-mail address removed) .
9. Thanks in advance.
Ariel Ze'evi.
Parts of the code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define byte unsigned char
#define word unsigned short
typedef struct {
word FlashAddress, index;
} IndexAddress;
FILE *fp;
char **FileBuff;
IndexAddress *AddStruct;
word LinesNumber=0;
int main(int argc, char* argv[])
{
char FileName[]={"c:\\ariel\\flash196\\modbus.hex"};
char line[MAX_LINE];
long BufSize=0;
word i,LineLen=0;
// Open for read (will fail if file "FileName" does not exist)
if( (fp = fopen( FileName, "r" )) == NULL )
printf( "The file %s was not opened\n",FileName );
else
printf( "The file '%s was opened\n",FileName );
// Finds Number of lines in the Hex File
while (!feof(fp))
{
fgets(line,MAX_LINE,fp);
LinesNumber++;
}
printf("\nLinesNumber=%d\n",LinesNumber);
// Allocate an array of pointers to number of lines
if((FileBuff = (char **)malloc(LinesNumber * sizeof(char *)))==NULL)
{
printf("malloc error!\n");
return -1;
}
rewind(fp);
// Read line by line from file, allocate a memory to it and copy line
to allocation
for(i=0; i<LinesNumber; i++)
{
fgets(line,MAX_LINE,fp);
if((FileBuff = (char *)malloc(strlen(line) *
sizeof(char)))==NULL)
{
printf("malloc error!\n");
return -1;
}
strcpy(FileBuff,line);
}
// Allocate an array of pointers to number of lines
AddStruct = (IndexAddress*)malloc(LinesNumber *
sizeof(IndexAddress));
if(AddStruct ==NULL)
{
printf("malloc error!\n");
return -1;
}
// Prepare for sorting
for(i=0; i<LinesNumber; i++)
{
AddStruct.FlashAddress=Address(i);
AddStruct.index=i;
}
PrintAddIndex();
QuickSort(AddStruct,0,LinesNumber);
PrintAddIndex();
for(i=0; i<LinesNumber; i++)
free(FileBuff); <====== IN THIS POINT THE PROGRAM WAS
THROWN.
free(FileBuff);
/* Close fp */
if( fclose( fp ) )
printf( "The file %s was not closed\n",FileName );
return 0;
}
====================================
Hi!
I have written a program in C on PC with Windows 2000 in a Visual C
environment.
I have an error in freeing multiple allocation as follows:
1. I allocated an array of pointer.
2. I Read line by line from a text file.
3. I allocated memory for the read line.
4. I wrote the pointer from malloc() to the array of pointers.
5. The problem is in the free() function. When I tried to free in a
loop the
allocated lines, the Visual C throw me with an exception.
6. I wanted to know why?. I tried also to free in the opposite order I
have
allocated them, but this also doesn't work.
7. Does anyone has an idea?
8. Please send answer to (e-mail address removed) .
9. Thanks in advance.
Ariel Ze'evi.
Parts of the code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define byte unsigned char
#define word unsigned short
typedef struct {
word FlashAddress, index;
} IndexAddress;
FILE *fp;
char **FileBuff;
IndexAddress *AddStruct;
word LinesNumber=0;
int main(int argc, char* argv[])
{
char FileName[]={"c:\\ariel\\flash196\\modbus.hex"};
char line[MAX_LINE];
long BufSize=0;
word i,LineLen=0;
// Open for read (will fail if file "FileName" does not exist)
if( (fp = fopen( FileName, "r" )) == NULL )
printf( "The file %s was not opened\n",FileName );
else
printf( "The file '%s was opened\n",FileName );
// Finds Number of lines in the Hex File
while (!feof(fp))
{
fgets(line,MAX_LINE,fp);
LinesNumber++;
}
printf("\nLinesNumber=%d\n",LinesNumber);
// Allocate an array of pointers to number of lines
if((FileBuff = (char **)malloc(LinesNumber * sizeof(char *)))==NULL)
{
printf("malloc error!\n");
return -1;
}
rewind(fp);
// Read line by line from file, allocate a memory to it and copy line
to allocation
for(i=0; i<LinesNumber; i++)
{
fgets(line,MAX_LINE,fp);
if((FileBuff = (char *)malloc(strlen(line) *
sizeof(char)))==NULL)
{
printf("malloc error!\n");
return -1;
}
strcpy(FileBuff,line);
}
// Allocate an array of pointers to number of lines
AddStruct = (IndexAddress*)malloc(LinesNumber *
sizeof(IndexAddress));
if(AddStruct ==NULL)
{
printf("malloc error!\n");
return -1;
}
// Prepare for sorting
for(i=0; i<LinesNumber; i++)
{
AddStruct.FlashAddress=Address(i);
AddStruct.index=i;
}
PrintAddIndex();
QuickSort(AddStruct,0,LinesNumber);
PrintAddIndex();
for(i=0; i<LinesNumber; i++)
free(FileBuff); <====== IN THIS POINT THE PROGRAM WAS
THROWN.
free(FileBuff);
/* Close fp */
if( fclose( fp ) )
printf( "The file %s was not closed\n",FileName );
return 0;
}