Thread: free on modified pointer

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    56

    free on modified pointer

    Hi,

    here there's an example code that creates a memory leak.
    My problem is to call free on modified pointer.

    Code:
      int num=10;
      char *test;
      int i=0;
    
      if ((test = calloc(num, sizeof(char *))) == NULL) {
        puts("allocation error!");
        return 1;
      }
      puts("Mem allocated");
      
      printf("Insert name: ");
      scanf("%s",test);
      printf("your name is %s\n", test);
      //NEED TO PERFORM SOME OPERATIONS
      test=&test[1];   <-------- pointer changed
      printf("%s\n",test);
    
     //free(test);
    
      return 0;
    }
    I mean if I need to perform some operations on 'test' so I change it to
    point to the next char there's no way to free up the memory correctly? So Do I always have to use a backup pointer (a temporary pointer) without change the original one?

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You don't have to, but it would be easier and safer that way.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    Hi,
    thx for reply.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Dedalus View Post
    My problem is to call free on modified pointer.
    ...
    I mean if I need to perform some operations on 'test' so I change it to
    point to the next char there's no way to free up the memory correctly? So Do I always have to use a backup pointer (a temporary pointer) without change the original one?
    If you want free() to work, yes you do. And you don't need to change pointers to strings... simply use array notation to access the next character as in...
    Code:
    char *str = malloc(100 * sizeof(char));
    char chr;
    strcpy (str, "This is a silly old test to learn from");
    
    chr = str[8];
    
    free (str);
    There is seldom a real need to disturb the root pointer of an allocated memory block.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Every malloc/calloc needs a free on unchanged initial value (from malloc/calloc).

    Code:
      int num=10;
      char *test;
      int i=0;
    
      if ((test = malloc(num)) == NULL) {
        puts("allocation error!");
        return 1;
      }
      puts("Mem allocated");
      
      printf("Insert name: ");
      scanf("%9s",test);
      printf("your name is %s\n", test);
    
      printf("%s\n",test+0);
      printf("%s\n",test+1);
      printf("%s\n",test+2);
      printf("%s\n",test+3);
      printf("%s\n",test+8);
      printf("%s\n",test+9);
    
      free(test);
    
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    Hi
    thx all for reply.
    I tried to work out a solution.
    If I would remove the first letter of a string I could then copy back the rest of the string without modify the pointer and miss the reference.
    Here is the code:
    Code:
      int num=10;
      char *test;
      int i=0;
    
      if ((test = calloc(num, sizeof(char *))) == NULL) {
        puts("allocation error!");
        return 1;
      }
      puts("Mem allocated");
      printf("Insert name: ");
      scanf("%s",test);
      printf("your name is %s\n", test);
      memmove(test,&test[1],strlen(&test[1]));
      memset(&test[strlen(test)-1],'\0', 1);
      printf("%s\n",test);
    
     free(test);
    
      return 0;
    }
    Do you think is clean act like that?
    In this way I don't have to use a backup pointer.
    Thx in advance

  7. #7
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Quote Originally Posted by Dedalus View Post
    Hi
    thx all for reply.
    I tried to work out a solution.
    If I would remove the first letter of a string I could then copy back the rest of the string without modify the pointer and miss the reference.
    Here is the code:
    Code:
      int num=10;
      char *test;
      int i=0;
    
      if ((test = calloc(num, sizeof(char *))) == NULL) {
        puts("allocation error!");
        return 1;
      }
      puts("Mem allocated");
      printf("Insert name: ");
      scanf("%s",test);
      printf("your name is %s\n", test);
      memmove(test,&test[1],strlen(&test[1]));
      memset(&test[strlen(test)-1],'\0', 1);
      printf("%s\n",test);
    
     free(test);
    
      return 0;
    }
    Do you think is clean act like that?
    In this way I don't have to use a backup pointer.
    Thx in advance
    I see what you do, but why? Looks like you're getting rid of the first letter of name?

    memmove is heavyish operation. So I would like to ask why you're doing it? Maybe, just maybe you could go on without memmove, and later using &(test[1]) instead of memmoving and removal of last character. If you insist on going with memmove, then at least replace

    memset(&test[strlen(test)-1],'\0', 1);

    1 byte wide memset with test[strlen(test)-1]='\0';
    And if you use memset, use 0 instead of '\0' - since memset has int as 2.nd argument (although I really don't understand why).

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    56
    Hi

    thx for the advice
    I see your point. So the cleanest way seems to be:
    Code:
      int num=10;
      char *test, *tmp;
      int i=0;
    
      if ((test = calloc(num, sizeof(char *))) == NULL) {
        puts("allocation error!");
        return 1;
      }
      tmp=test;
      puts("Mem allocated");
      printf("Insert name: ");
      scanf("%s",test);
      printf("your name is %s\n", test);
      test=&test[1];
      printf("%s\n",test);
    
     free(tmp);
    
      return 0;
    }

  9. #9
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    well, I give up. It's of course your business why you want to manipulate text ptr. you could propably avoid temp ptr, but we cant help unless we know what you plan. Just do not use allocated memory after free...

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if ((test = calloc(num, sizeof(char *))) == NULL)
    It should be sizeof(char), not sizeof(char*).
    Or better yet, sizeof(*test), then you don't have to resolve the type yourself.
    Here, you're just being lucky in choosing a type which is larger than char, so you have sufficient memory.

    > printf("%s\n",test);
    You could always do
    printf("%s\n",&test[1]);
    without the need to muck about with your pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free pointer to chars
    By cfdprogrammer in forum C Programming
    Replies: 4
    Last Post: 09-26-2010, 09:07 AM
  2. Free the pointer
    By dunxton in forum C Programming
    Replies: 2
    Last Post: 03-20-2009, 03:50 AM
  3. Free pointer?
    By jordanguyoflove in forum C Programming
    Replies: 9
    Last Post: 10-17-2008, 01:38 AM
  4. Free a pointer
    By nemo3110 in forum C Programming
    Replies: 8
    Last Post: 01-21-2008, 09:28 AM
  5. Free a pointer
    By nemo3110 in forum C Programming
    Replies: 1
    Last Post: 01-21-2008, 02:34 AM