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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:37:26+00:00 2026-06-13T20:37:26+00:00

I was wondering the protocol for dealing with memory leaks with pointers to dynamic

  • 0

I was wondering the protocol for dealing with memory leaks with pointers to dynamic memory being returned in C and C++. For example, strtok returns a char*. Presumably, the pointer that is returned must eventually be freed/deleted. I note that the reference page doesn’t even mention this. Is that because this is simply assumed? Also, how do you know whether to delete or free? Does one have to do research to find out what language each function was originally in, and then assume that all C programs use malloc/free and C++ uses new/delete?

  • 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-13T20:37:27+00:00Added an answer on June 13, 2026 at 8:37 pm

    strtok does NOT return a pointer to a newly allocated memory, but rather a pointer to a memory location, which has been previously allocated.

    Let’s assume this:

    char String[1024];
    strcpy(String, "This is a string");
    char *Ptr = strtok(String, " ");
    

    Ptr will not point to a newly allocated memory location, but rather to the location of String (if my counting doesn’t fail me right now), with the space getting replaced by a ‘\0’. (

    From the reference:
    This end of the token is automatically replaced by a null-character by the function, and the beginning of the token is returned by the function.

    That also means, that if you were to print out ‘String’ again after strtok has done its work, it would only contain ‘This’, since the string is terminated after that.

    As a rule of thumb, you are safe to say, that you only need to free/delete memory you’ve explicitly allocated yourself.

    That means:

    For each ‘new’ put a ‘delete’, and for each ‘malloc’ put a ‘free’ and you’re good.

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

Sidebar

Related Questions

I am wondering how I can use google protocol buffers to accept a request
I was wondering what protocol does yahoo use for VOIP,File Transfer and Chat?
I was wondering if anyone had experience retrieving data with the 3270 protocol. My
I'm looking at MeteorJS at the moment and I was wondering which protocol they
I am designing an application protocol, and i am wondering if i still need
I was wondering if protocol buffer's serialized data format remains constant across protobuf compiler
I am using Restlet 2.0, I am wondering which http protocol is used? HTTP
I was wondering if there are any sample wireshark pcaps for diameter protocol out
I was wondering what made this protocol so useful as opposed to the other
I am wondering if I could use XML as the application-level protocol. The XML

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.