Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6638863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:29:15+00:00 2026-05-25T23:29:15+00:00

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

  • 0

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);       
   }      
   return s;       
 }       
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:29:15+00:00Added an answer on May 25, 2026 at 11:29 pm
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen a few posts regarding this issue but not one specific to
I have a few doubts regarding the use of attributes. By reading the ARC
Hi i am new to hibernate technology and i have few issues. Suppose if
I have a few issues with the following php functions (part of a bigger
I have a few routing issues with my ASP.NET MVC2 website and was wondering
This is not an issue with a few tables, but when I have a
I have few question in this regard When you create an internet page, does
I have few small basic problems : How to format : int i =
I have few Question for which I am not able to get a proper
i have a design issue regarding sending user data from a mobile phone app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.