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 7874001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:40:49+00:00 2026-06-03T02:40:49+00:00

Functions like wcsdup , implicitly calls malloc to allocate memory for the destination buffer.

  • 0

Functions like wcsdup, implicitly calls malloc to allocate memory for the destination buffer. I was wondering as the memory allocation is not very explicit, so does it seems logical to explicitly free the storage?
This is more like a design dilemma and the reasons for and against are as follows

Should be freed because

  1. Not freeing it would cause Memory Leak.
  2. It is well documented that wcsdup/_wcsdup calls malloc to allocate memory even when its called from a C++ Program.

Should not be freed because

  1. Memory accumulated by wcsdup would eventually be freed when program exits. We always live with some memory leaks through out the program lifetime(Unless we are heavily calling wcsdup for large buffer size).
  2. It can be confusing as free was not preceded by an explicit malloc.
  3. As its not part of the standard but posix compliant, Microsoft implementation may not use malloc for allocating destination buffer.

What should be the approach?

  • 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-06-03T02:40:50+00:00Added an answer on June 3, 2026 at 2:40 am

    From MSDN:

    it is good practice always to release this memory by calling the free routine on the pointer returned

    From the page you linked:

    The returned pointer can be passed to free()

    It seems fairly explicit: if you care about memory leaks, then you should free the memory by using free.

    To be honest, I’m concerned about the cavalier attitude hinted at with this:

    We always live with some memory leaks through out the program lifetime

    There are very rarely good reasons to leak memory. Even if the code you write today is a one-off, and it’s not a long-lived process, can you be sure that someone’s not going to copy-and-paste it into some other program?

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

Sidebar

Related Questions

We know that calls to functions like fprintf or fwrite will not write data
Sometimes I write very short assembly functions like function SeniorBit(Value: LongWord): Integer; asm OR
I'm wondering if writing functions like this is considered good or bad form. def
Python's len() and padding functions like string.ljust() are not tabstop-aware, i.e. they treat '\t'
Just wondering if this and other related functions like those Integer is one of
buffer functions like GetWindowText(), GetModuleFileName(), SHGetFolderPath() make me angry because you almost always waste
Where should I place functions like, for example, sum_it_all() in Catalyst project? It's not
I was just wondering why trivial functions like String.contains() aren't part of the JavaScript
If you have 2 functions like: def A def B and A calls B,
I'm wondering whether functions like strip_tags() and trim() UTF-8 aware? I found this on

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.