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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:17:01+00:00 2026-06-08T21:17:01+00:00

I am having some trouble understanding this output for my code: #define MAX 5

  • 0

I am having some trouble understanding this output for my code:

#define MAX 5

char pipeNames[MAX][1024];
string s1 = "\\\\.\\p\\p1";
string s2 = "\\\\.\\p\\p2";
int len1 = strlen(s1.c_str());
int len2 = strlen(s2.c_str());
memcpy((void*)pipeNames[0], (void*)s1.c_str(), len1);
memcpy((void*)pipeNames[1], (void*)s2.c_str(), len2);
cout<<pipeNames[0];
cout<<endl<<len1;
cout<<endl<<pipeNames[1];
cout<<endl<<len2;

Actual Output:

\\.\p\p1É┼é|
8
\\.\p\p2
8

Expected Output:

\\.\p\p1
8
\\.\p\p2
8

Why are the extra characters being printed at the end of pipeNames[0]. I am using string::c_str() which automatically appends null character, so why the error?

  • 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-08T21:17:02+00:00Added an answer on June 8, 2026 at 9:17 pm

    You’re not copying the null char. strlen does not count the null char. Try this

    memcpy(pipeNames[0], s1.c_str(), len1 + 1);
    memcpy(pipeNames[1], s2.c_str(), len2 + 1);
    

    No need for your casts either. Casts are dangerous, because they can mask compiler errors so don’t use them if you don’t need them.

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

Sidebar

Related Questions

I'm having some trouble understanding why this code won't work. I got it from
I'm having some trouble understanding this piece of code that my professor used as
I'm having some trouble understanding a bit of code. I've got 2 classes Company
I'm having some trouble understanding the IF clause at the end of this function
I am having some trouble understanding the behavior of the following Scheme program: (define
I am having some trouble understanding this brute force approach. I am using Perl
I am having a bit of trouble understanding this bug. Here is the code:
I'm having some trouble understanding this issue. I have a class: class StringProperty {
I'm pretty new to programming and am having some trouble understanding the output of
I'm new to Ruby, so I'm having some trouble understanding this weird exception problem

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.