Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > junk pointer

Reply
Thread Tools

junk pointer

 
 
Lie Algebra
Guest
Posts: n/a
 
      04-03-2009
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
 
Reply With Quote
 
 
 
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      04-06-2009
On 3 Apr, 07:44, Lie Algebra <(E-Mail Removed)> wrote:

> 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()?

<snip>

> 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).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[OT] Conjectures? on the "constant pointer on Turbo C programming"junk Francois Grieu C Programming 12 05-19-2010 09:58 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
junk pointer ???? Laszlo Zsolt Nagy Python 0 12-06-2005 08:09 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments