junk pointer

Discussion in 'C Programming' started by Lie Algebra, Apr 3, 2009.

  1. Lie Algebra

    Lie Algebra Guest

    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
    Lie Algebra, Apr 3, 2009
    #1
    1. Advertising

  2. Lie Algebra

    Guest

    On 3 Apr, 07:44, Lie Algebra <> 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).
    , Apr 6, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Row
    Replies:
    0
    Views:
    280
    Peter Row
    Mar 3, 2004
  2. =?Utf-8?B?TEFDSA==?=

    Re: SmtpMail - Hotmail Junk e-mail

    =?Utf-8?B?TEFDSA==?=, Sep 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    436
    =?Utf-8?B?TEFDSA==?=
    Sep 16, 2004
  3. =?Utf-8?B?TEFDSA==?=

    Re: SmtpMail - Hotmail Junk e-mail

    =?Utf-8?B?TEFDSA==?=, Sep 24, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    474
    =?Utf-8?B?TEFDSA==?=
    Sep 24, 2004
  4. Laszlo Zsolt Nagy

    junk pointer ????

    Laszlo Zsolt Nagy, Dec 6, 2005, in forum: Python
    Replies:
    0
    Views:
    410
    Laszlo Zsolt Nagy
    Dec 6, 2005
  5. Francois Grieu
    Replies:
    12
    Views:
    684
    Alan J
    May 19, 2010
Loading...

Share This Page