C Programming: malloc and free within a loop
        Posted  
        
            by kouei
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by kouei
        
        
        
        Published on 2010-03-17T16:03:39Z
        Indexed on 
            2010/03/17
            16:11 UTC
        
        
        Read the original article
        Hit count: 522
        
Hi all,
I just started out with C and have very little knowledge about performance issues with malloc() and free(). My question is as such: if I were to call malloc() followed by free() in a while-loop that loops for, say, 20 times, would it run slower compared to if I were to call free() outside the loop?
I am actually using the first method to allocate memory to a buffer, read a string of variable length in a file, perform some string operation, and then clear the buffer after every iteration. If my method results in a lot of overhead then I'd like to ask for a better way for me to achieve the same results.
Sorry for my bad English.
Thanks and regards, K
© Stack Overflow or respective owner