Thread: URGENT! problem with [free()]

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    15

    URGENT! problem with [free()]

    hello

    this has to work in about an hour :O!

    Code:
    pSzenenGraph 
    findeSzenenGraphKnoten(pSzenenGraph wurzelzeiger, char* objektname){
      char* sub_string = malloc(strlen(objektname));
      char* rest_string = malloc(strlen(objektname));
      pSzenenGraph ergebnis;
      
      strcpy ( sub_string, objektname);
      rest_string=strchr(objektname,'/');
      strtok(sub_string,"/");
      
      if (!istLeer(wurzelzeiger) && (sub_string!=NULL)){
        if (strcmp(sub_string,wurzelzeiger->objekt.name)==0){
          if (rest_string!=NULL){
            ++rest_string;
            ergebnis = findeSzenenGraphKnoten(wurzelzeiger->kind,rest_string);       
            free(rest_string);
            free(sub_string);
            return ergebnis;
          }
          free(rest_string);
          free(sub_string);
          return wurzelzeiger;
        }
        ergebnis = findeSzenenGraphKnoten(wurzelzeiger->next,objektname);
        free(rest_string);
        free(sub_string);
        return ergebnis;
    
      }
      free(rest_string);
      free(sub_string);
      return (pSzenenGraph) 0;
    }
    the problem is:
    no problem under windows, but under linux i get segmentation fault with free() in.
    especially here:
    Code:
          if (rest_string!=NULL){
            ++rest_string;
            ergebnis = findeSzenenGraphKnoten(wurzelzeiger->kind,rest_string);       
            free(rest_string);
            free(sub_string);
            return ergebnis;
          }
    if i dont free, my progamm runs for about 30 sec, until i run out of memory.

    any idea?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
      char* sub_string = malloc(strlen(objektname));
      char* rest_string = malloc(strlen(objektname));
      pSzenenGraph ergebnis;
      
      strcpy ( sub_string, objektname);
    You're not allocating enough space to store the \0 at the end of the string. Try allocating strlen(objectname)+1 bytes instead.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Two things make this tough for me: I can't compile your code, and I don't know German.

    However, I tend to think that your problem is here:
    Code:
    if (rest_string!=NULL){
            ++rest_string;
            ergebnis = findeSzenenGraphKnoten(wurzelzeiger->kind,rest_string);       
            free(rest_string);
            free(sub_string);
            return ergebnis;
          }
    Freeing that incremented pointer probably doesn't work. Try decrementing it before freeing it. Even better, try not incrementing it at all:
    Code:
    if (rest_string!=NULL){
            ergebnis = findeSzenenGraphKnoten(wurzelzeiger->kind,rest_string+1);       
            free(rest_string);
            free(sub_string);
            return ergebnis;
          }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char* sub_string = malloc(strlen(objektname));
    This should be (to make room for string terminator):
    char* sub_string = malloc(strlen(objektname)+1);

    > char* rest_string = malloc(strlen(objektname));
    You shouldn't use malloc() here as later on you use strchr(), which returns a pointer.
    char* rest_string;

    > free(rest_string);
    And remove any free's of rest_string.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    Code:
            ergebnis = findeSzenenGraphKnoten(wurzelzeiger->kind,rest_string+1);       
            free(rest_string);
            free(sub_string);
            return ergebnis;
    this seems to do the job!

    thanks a lot guys!!


    >>..and I don't know German
    sorry pianorain, i would've tried to post better code, but i ran almoust out of time.
    not speaking german, you managed to find out it is german after all .

    greets martin

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Truth is, swoopy's correction is the most correct. If you use mine, you'll have little memory leaks of strlen(objektname) size. Since rest_string is pointing to a substring of sub_string, I don't really know what you'll get when you free it.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    right. works perfect.
    thanks swoopy!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault Problem: Urgent Help
    By bodydrop in forum C Programming
    Replies: 3
    Last Post: 05-05-2006, 08:02 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Problem in loop. Please Help. Urgent!
    By jjbuchan in forum C Programming
    Replies: 4
    Last Post: 11-10-2005, 04:46 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM