L
Lie Algebra
Hi fellows,
in the following code, I am trying to parse an ascii file made of two
columns which are tab separated. The goal is to feed a cfg object which
will hold an array of record objects as well as the size of this array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct {
int key;
char *word;
} record;
typedef struct {
int counter;
record *rec;
} cfg;
void free_records ( cfg config, int pos ) {
for ( ; pos >= 1 ; pos-- )
free ( (config.rec)[pos-1].word );
free ( &config );
}
char *file_to_string ( const char *filepath ) {
char *line = NULL;
[..]
/* load the file content into a single string, line being allocated
dynamically */
[..]
return line;
}
cfg *build_conf ( char *line ) {
cfg *config = NULL;
[..]
/* allocate memory for a cfg object and feed data with it */
[..]
free(line);
return config;
}
int main() {
const char *path = "/somepath/file.ini";
cfg *config = NULL;
if (NULL == (config = build_conf (file_to_string (path)))) {
perror("Cannot load file ");
return 1;
}
[..]
/* print config */
[..]
free_records ( *config, (*config).counter );
return 0;
}
I have made a test with a file.ini being 100000 lines long and
right afer the last record display, I get :
load in free(): warning: junk pointer, too high to make sense.
I have then modify my free_records function since its now:
void free_records ( cfg *config, int pos ) {
for ( ; pos >= 1 ; pos-- )
free ( (config->rec)[pos-1].word );
free ( config );
}
The function takes now a pointer to the cfg struct as a parameter
and not the cfg object itself.
Running the same test again doesn't show the message anymore.
In fact I am not really understanding the difference between my two
approachs ?
Could someone share some light here ?
Thks,
E.S
in the following code, I am trying to parse an ascii file made of two
columns which are tab separated. The goal is to feed a cfg object which
will hold an array of record objects as well as the size of this array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct {
int key;
char *word;
} record;
typedef struct {
int counter;
record *rec;
} cfg;
void free_records ( cfg config, int pos ) {
for ( ; pos >= 1 ; pos-- )
free ( (config.rec)[pos-1].word );
free ( &config );
}
char *file_to_string ( const char *filepath ) {
char *line = NULL;
[..]
/* load the file content into a single string, line being allocated
dynamically */
[..]
return line;
}
cfg *build_conf ( char *line ) {
cfg *config = NULL;
[..]
/* allocate memory for a cfg object and feed data with it */
[..]
free(line);
return config;
}
int main() {
const char *path = "/somepath/file.ini";
cfg *config = NULL;
if (NULL == (config = build_conf (file_to_string (path)))) {
perror("Cannot load file ");
return 1;
}
[..]
/* print config */
[..]
free_records ( *config, (*config).counter );
return 0;
}
I have made a test with a file.ini being 100000 lines long and
right afer the last record display, I get :
load in free(): warning: junk pointer, too high to make sense.
I have then modify my free_records function since its now:
void free_records ( cfg *config, int pos ) {
for ( ; pos >= 1 ; pos-- )
free ( (config->rec)[pos-1].word );
free ( config );
}
The function takes now a pointer to the cfg struct as a parameter
and not the cfg object itself.
Running the same test again doesn't show the message anymore.
In fact I am not really understanding the difference between my two
approachs ?
Could someone share some light here ?
Thks,
E.S