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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:15:47+00:00 2026-06-17T17:15:47+00:00

Is there any memory leak ? const char * pname = NAME; char *

  • 0

Is there any memory leak ?

    const char * pname = "NAME";
    char * temp = new char[strlen(Name) + 64];
    sprintf(temp,"%s", pname);

    delete [] temp; // is there any memory leaks because now length of temp is 4.
  • 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-17T17:15:49+00:00Added an answer on June 17, 2026 at 5:15 pm

    No there won’t be any memory leak. sprintf will only use the bytes of temp that it requires, BUT, all of the bytes initially created will remain allocated (even though some are unused).

    The call to delete[] temp will then deallocate all the bytes originally allocated.

    As others have pointed out though, do not free pname. You should only call delete and delete[] on pointers which were created with new and new[] respectively.

    Additional information:

    When you created temp, new[] allocated an array of contiguous bytes in memory PLUS an additional (small) space where it stores the information about the allocation (how large the allocation is, for instance). When you called delete[] it examined that information and found that strlen(Name)+64 bytes were allocated, and so it knows it has to deallocate all of them. The fact that you only used a small fraction of the allocated space does not make a difference.

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

Sidebar

Related Questions

Are there any memory leaks when I throw an exception from a constructor like
Hi please tell me if there is any memory leak in my code or
1) Is there also any memory leak tool called Boehmgc ? How is that
I read about lexical-let's memory leak, for example here: Are there any problems with
Possible Duplicate: Is there any working memory profiler for Python3 I have some script
is there any way to reserve memory space to be used later by default
Is there any way to allocate memory on host, that is accessible directly from
Is there any way to get how much memory a service application is using
Is there any library for distributed in-memory cache, distributed tasks, publish/subscribe messaging? I have
Is there any mechanism in Java to reduce the memory usage while reading large

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.