#1
  1. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13

    memory free() error


    how come I get an error if I try to free p. It compiles if I don't try to free it.

    Code:
    i = oVector[2] ;
    j = oVector[3] - i ;
    
    p = (char*) malloc(sizeof(char)* j);
    
    strncpy(p,line + (i), j);
    p[j] = '\0';
    
    //ll.insertKeywords(p);
    keywords = false;
    
    printf("this prints \n");
    //free(p);
    p =NULL;
    Some day I'll create a smart quote to put here.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    USA
    Posts
    286
    Rep Power
    13
    What error are you getting?
    Jon Sagara

    "Me fail English? That's unpossible!"
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    BTW you also have a bug here:
    p[j] = '\0';

    This should be
    p[j-1] = '\0';

    since arrays in C are 0 based. If you allocate a 100 element array, it has element indices from p[0]...p[99]. You may have a similar problem with the strncpy statement above that.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. pogremar
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2003
    Location
    At Work
    Posts
    958
    Rep Power
    13
    thanks guys, it was the error that scorpion pointed out that I overlooked. I don't think I have an error in the strncpy. strncpy is not zero base so strncpy(p1, p2, 8) is not gonna copy 9 chars. If I'm overlooking something, scorp, feel free to pointer it out.

    You guys always have the answers.
    thanks

IMN logo majestic logo threadwatch logo seochat tools logo