Reusing pointers

This is a discussion on Reusing pointers within the C Programming forums, part of the General Programming Boards category; Hi I have a situation here in which i malloc a char_t* pt and free it. If I want to ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    31

    Reusing pointers

    Hi I have a situation here in which i malloc a char_t* pt and free it. If I want to reuse this pt and malloc it of another size. Will there be any risk involved?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Yes, there is risk involved. The further apart the second malloc is from the first free, the higher the chance you might blunder (though setting the pointer to be a null pointer after the free may mitigate potential blunders). But if you talk about correctness rather than risk, then indeed it can be done correctly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Edelweiss View Post
    Hi I have a situation here in which i malloc a char_t* pt and free it. If I want to reuse this pt and malloc it of another size. Will there be any risk involved?
    One tactic you might explore to mitigate some of the risk is the creation of intermediate scopes in your programs...
    Code:
    // lots of code
    
    {                                    <-- scope starts here
       char *pchar = malloc(100 * sizeof(char);
       // do stuff with pchar
       free(pchar);
    }                                    <-- scope ends here pchar is destroyed
    
    // more code
    This will let you create variables use them and forget about them on the fly.

    Here's a live code example... (it's Windows API code, but I think you'll get the idea)
    Code:
    void GoButton( void )
      {  
         WCHAR password[25];
    
        { 
          WORD port;
          GetWindowText(Wind[4],password,24);
          port = _wtoi(password); 
          InitNetwork(port); 
         }
        
        { 
          DWORD pcode;
          WCHAR ltxt[MAX_LOGLINE];
          GetWindowText(Wind[2],password,24);
          pcode = SetPassCode(password);
          if (!pcode)
           { 
              lstrcpy(ltxt,L"Password improperly entered");
              AddToLog(L"Error",ltxt); 
           } 
         }
    
        EnableWindow(Wind[2],0);
        EnableWindow(Wind[4],0);
        EnableWindow(Wind[5],0); 
    }
    Last edited by CommonTater; 08-21-2011 at 09:49 PM. Reason: Edited braces so nit pickers won't complain about my formatting style

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Haha.....nice comment. Don't give in Tater, stay strong!
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Haha.....nice comment. Don't give in Tater, stay strong!
    As they say... Iligitimi non carborundum ...

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Illigitimate carborator?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    Illigitimate carborator?
    Oh boy... sharp as a tack on C Code ... not so good with humour, I see...

    It's an old engineering joke in faux latin... Iligitimi = Illigitimate = Child born out of wedlock. Carborundum is a brand name for grindstones. Non = negative, no, not...

    Thus ... "Don't let the b**tards wear you down"
    Last edited by CommonTater; 08-22-2011 at 10:05 AM.

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    If you can get around freeing the pointer, you could use realloc(), but check its returns for any pitfalls.
    realloc

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by slingerland3g View Post
    If you can get around freeing the pointer, you could use realloc(), but check its returns for any pitfalls.
    realloc
    Except that realloc() retains whatever content was already in the buffer... I doubt that's what the OP wants...

  10. #10
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by CommonTater View Post
    Except that realloc() retains whatever content was already in the buffer... I doubt that's what the OP wants...
    Ah, very true!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reusing a 'deleted' record
    By spongefreddie in forum C Programming
    Replies: 16
    Last Post: 11-05-2010, 11:41 PM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  4. Reusing dialog procedure and resource file code
    By elad in forum Windows Programming
    Replies: 3
    Last Post: 12-23-2006, 08:45 AM
  5. Reusing code blocks for different variables
    By Queue in forum C Programming
    Replies: 16
    Last Post: 09-16-2006, 11:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21