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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:06:35+00:00 2026-06-18T11:06:35+00:00

I have some function like this: void MClass::GetS(char* buf, int max) const { char

  • 0

I have some function like this:

void MClass::GetS(char* buf, int max) const  {
char *temp = new char[max];
temp[max] = '\0';
for (int i = 0; i < max - 1; i++)
    temp[i] = src[i]; // src is class member (char *)
buf = temp; // buf is null after this o.O
}

So, I can’t change buf value in this function. Why does it take place?

  • 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-18T11:06:36+00:00Added an answer on June 18, 2026 at 11:06 am

    Problems:

    1. You are writing off the end of your heap array with temp[max] = '\0'; – this places a 0 1 spot after your buffer.

    2. Who knows how big src is. You are passing in a “max” and reading against a src without check
      size – very dangerous.

    3. You are returning a pointer with a pointer. You need to pass in a double pointer and to get the actual pointer value returned correctly (or return the pointer in the return statement.)

    When in doubt, trace your code in a debugger. My guess is that your compiler is helping you by opimizing the last assignment away and leaving it as NULL for you – then letting you merrily crash later on with your now corrupted heap and memory leak…

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

Sidebar

Related Questions

I have some code in a function that goes something like this: void foo()
I have main function like this: void main() { char *s; inputString(s); printf(%s,s); }
Let's say I have a function that looks like this: public void saveBooking(/* some
I have declared some local variable in one function like this: void* thread_function (void*
I have some code that outputs like this: void exec_prompt(char * usr_name, char *
I need to pass some structure as function argument like this void myFunc(unsigned char
Inside my .aspx I have some JSON code that looks like this: function someFunctionName()
Here is the situation. I have some javascript that looks like this: function onSubmit()
I have some jQuery code like this : $(function($) { $('input.autonumeric').autoNumeric({aSep: ',', aDec: '.',vMax:'1000000000000'});
I have some code that looks like this: var MyObject = function () {

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.