junk pointer

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
 
G

Guest

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.

have you looked at fgets() and sscanf()?

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.

did it work with smaller numbers (like 3)? It may be the file
is just too big to process.

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 ?

I'm too lazy to debug your code (which isn't compilable anyway).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top