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

  • Home
  • SEARCH
  • 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 1001351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:39:27+00:00 2026-05-16T07:39:27+00:00

When in doubt, turn to Stackoverflow… I’m having a problem with string allocation. My

  • 0

When in doubt, turn to Stackoverflow…

I’m having a problem with string allocation. My goal is to store n length of a passed quoted string. I check m_p for null because I think in debug mode MS likes to set the address to 0xcccccccc instead of 0x00000000.

I passed 1 into length. But when I allocate it using new, I’m getting about 15 symbol characters in m_p. How can this be if m_size = lenth + 1 is 2? I would expect it to allocate only two cells. How can I limit it to length + 1?

 String::String(const char *str, int length) {
 m_size = length + 1;  // make room for null-terminated string

 if (m_p != NULL)
  m_p = NULL;

 try
 { 
  m_p = new char[m_size]; 
 }
 catch (bad_alloc e)
 {
  throw e;
 }

 strncpy(m_p, str, m_size); 
}
  • 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-16T07:39:28+00:00Added an answer on May 16, 2026 at 7:39 am

    If you want to store n characters of a passed quoted string, you need to take into account that strncpy doesn’t append null character if the length of the source string is greater than or equal to the length argument. So you have to take care of it:

    strncpy(m_p, str, length); // or strncpy(m_p, str, m_size-1);
    m_p[length] = '\0'; // or m_p[m_size-1] = '\0'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No doubt elements of this question have been asked before, but I'm having trouble
I have little doubt about adapter class. I know what's the goal of adapter
No doubt I'm doing something stupid, but I've been having problems running a simple
I am having one doubt regarding the use of static variable in Asp.net pages.
Is there a C++ function to turn off the computer? And since I doubt
My doubt is to skip a . from the input stream and read the
I doubt I'm using the correct vocabulary (or tags) to explain what I'm looking
Basic doubt...If QTP generates vbscript code as we record actions, can't we directly write
I doubt this is possible, and I'm certain to do it in a production
I doubt this is possible, but here's what I want to do. If my

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.