开发者

when to malloc and when not to -- c programming

I have few issues regarding when to use malloc or in this case strdup.

Below is the small function which I have stolen from internet.

I am trying to understand the code but stuck with few issues.

1. the code has assigned value to psrc and pdest. example

char* psrc = dups;  
char* pdest = s;  

Doubt: don't we need to use malloc to allocate space for psrc and pdest.? If not then why.

  1. pdest [0] = '\0'; The above line allocates termination character at the starting of pdest string. then previously why we have assigned pdest to s . example char* pdest = s;?

Any help or criticism will be helpful to me. Thanks and regards, Sam

 char* deldupchars (char* s)    
 {      
   char* dups = strdup (s);      
   if (dups)     
   {      
    char* psrc = dups;      
    char* pdest = s;      
    char ch;      

    pdest [0] = '\0';      
    while ((ch = *psrc++) != '\0')    
    {            
     if (! strchr (pdest, ch))    
     {           
       *pdest++ = ch;           
     }             
    }       
    pdest [0] = '\0';      
    free (dups);       
   }      
 开发者_Python百科  return s;       
 }       


char* psrc = dups;      

No need to allocate here since dups was allocated by strdup.

char* pdest = s;

No need to allocate here since s was allocated by the caller.

pdest[0] = '\0';

This writes into the contents of the string. The assignment pdest = s assigns pointers but not contents.


We don't have to allocate memory for for dups because the char* psrc = dups statement does not copy what dups points to, just where it points. This means the allocation done in strdup is enough for both pointers


strdup will do the malloc inside it and return the allocated memory (that's why you need to call free(dups) in the end of the function).

pdest/psrc are just a pointers (i.e.: just references the memory that was previously allocated from 's', and later is used to walk in that allocated memory). I think the main confusion is that pointers are just that: they point to an address in memory -- so, you can have many pointers to the same memory address and you can use that pointer to walk -- in the previously malloc'ed memory).

See: http://www.mkssoftware.com/docs/man3/strdup.3.asp


1) What you get in psrc and pdest is no more than another pointer to the character string. This doesn't mean you have 2 character strings, but two pointers to the same string. String is not duplicated. You don't need to allocate psrc and pdest because the compiler is already allocating memory for those variables when you declare them (just like you don't need to allocate memory for int a, for example). They're just 32 bits unsigned integers (for 32-bit systems, that is).

2) pdest is used to not modify original s value, since pdest is modified inside the while loop. Again, the string is NOT duplicated, so actually pdest is modifying the string pointed by s.


  1. You don't have value assignment there, but pointers assignment. This means that psrc points at the same memory location as dups. If you want to copy content of that memory location then you need to allocate memory and then copy. (malloc + memcpy)
  2. pdest [0] = '\0'; - no allocation here. This just places null-terminator at the beginning of array it points to. char* pdest = s; made pdest pointing at the same memory as s.


strdup() malloc()s the memory for the string, which is why you can store through the pointer that strdup() returns. But you have to free() that memory, which you correctly do.

From the strdup() man page:

The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3).


#include <stdio.h>

size_t undup (char *str);
size_t undup (char *str)
{
size_t src, dst;

for (src=dst=0; str[dst] = str[src++] ; ) {
  if (str[dst] != str[src] ) dst++;
  } 
return dst;
}

int main (int argc, char **argv)
{

  printf ("Before:%s\n", argv[1] );
  (void) undup ( argv[1] );
  printf ("After:%s\n", argv[1] );

  return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜