Thread: Freeing memory issues

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    9

    Freeing memory issues

    Code:
    char MakeBig(char c) /*input: letter. output: CAP letter.*/
    {
         if (c >=97 && c<=122)
            c-=32;
         return c;
    }
    /*------------------------------------------*/
    char *trim(char *str)
    /*input: a sentence*/
    /*output: the sentence without spaces, and post-space letters capped.*/
    {
    char *str2;
    int str2l=0;
    int Big=1;
    str2=(char *)malloc((strlen(str)+1)*sizeof(char))
    if str2=='\0'
       {
                 printf("Allocation failed!");
                 exit(0);
       }
    for (i=0; i<strlen(str); i++)
       if (str[i]==' ') 
          Big=1;
       else 
            if (Big==1)
               {
                       str2[str2l]=MakeBig(str[i]);
                       Big=0;
                       str2l++;
               }
            else
                {
                       str2[str2l]=str[i];
                       str2l++;
                }
    str2[str2l]='\0';
    /*LINE IN QUESTION FOLLOWS*/
    free(&str2[str2l+1]);
    return str2;
    }
    /*afternote: the idea was of course to release any spare memory.
    Would this be the right way to do it? If not, why? 
    and how to alternatively implement the solution?*/
    Those are 2 functions, the program wasn't written.

    My questions are outlined in the last comment.

    Expecting your reply,

    -Sp00nk.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Don't cast malloc(), it's in the FAQ. Include <stdlib.h> and it should work.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    char *p = malloc(sizeof(char)*100);
    if(!p) exit(1);
    /* ... */
    free(p);
    That should work.

    [edit]
    In other words, you don't have to free each char. You can free the whole string in one swoop.
    [/edit]

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    you were not following. I am not to release the whole string. I have to keep the data.

    I was aiming at releasing the spare allocated memory that wasn't used.

    Thanks for the tip on the malloc casting, but my question still stands.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    realloc()?

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    unfamiliar with it.

    Wouldn't using a pointer be like freeing a complete string from that point on? therefore effectively freeing the spare memory that was allocated and not used?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    realloc() frees the data, and allocates a new string of the specified size. It chops off all the data on the end if the new size is smaller than the old.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Wouldn't using a pointer be like freeing a complete string from that point on?
    Yes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    brilliant. Note taken.
    ---

    However, I still need an answer whether or not the way that I implemented it would work or not. This was in a test, and therefore the question is not directed at "how to fix this program to make it work better" - rather on "should I have points taken off".

    Now, it would seem realloc would be the way to go, but we didn't learn that. Therefore if the implementation above works, I should not be ducked points.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I don't know if it will work, but
    Code:
    char MakeBig(char c) /*input: letter. output: CAP letter.*/
    sounds a lot like toupper(), which is in <ctype.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It won't work if you leave out the parantheses in this:
    Code:
    if str2=='\0'
    i is not declared:
    Code:
    for (i=0; i<strlen(str); i++)
    (And I think that < should be a <=).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    first comment: you're correct. my mistake in copying the code.

    i is undeclared indeed. The person who checked the test didn't notice either. =)

    no, the < is fine. remember that the index starts from 0; therefore it must run only up until the index which is 1 below the length of the string.

    Ch33rs.

    Still waiting to see if anybody knows if the memory freeing could be done in the way that I did it.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What about the toupper() post?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    free(&str2[str2l+1]);
    I don't think that will work. str2l+1 isn't an integer, &str2[x] isn't a pointer.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    erhm, str2l is an integer. look again.

    and &str2[x] creates a pointer that points to that specific variable. That is good enough when sending to different functions.

    Therefore, in conclusion, this is the equivilent of sending a string pointer "str" without an index. It should effectively free all cells in such a string, since the pointer would point to the first link in the chain, and all the rest of the linked boxes will be freed as well. therefore clearing the memory that is unused.

    ----

    This is my take on things, theoretically, it should work.

    However, I really need an expert opinion on this to confirm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. freeing memory
    By bartleby84 in forum C Programming
    Replies: 1
    Last Post: 04-17-2007, 01:29 AM
  4. Memory issues under win 98...
    By deathstryke in forum Tech Board
    Replies: 5
    Last Post: 06-11-2004, 10:03 AM
  5. Debug Assertion error when freeing memory
    By foniks munkee in forum C Programming
    Replies: 2
    Last Post: 02-28-2002, 05:57 PM