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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:05:16+00:00 2026-06-10T15:05:16+00:00

When I compile and run my program it seems as if everything works fine

  • 0

When I compile and run my program it seems as if everything works fine with the task in hand except I get an ” Run-time check failure #2″ error. This is my hw assignment by the way and it’s my first time trying to use any of the cstring functions so I’m sure that’s where I went wrong. Basically I’m appending 2 strings together and I’m almost 100% sure it has something to do with my result parameter getting overrun. Just don’t know how to fix it.

#include <iostream>
#include <cstring>
using namespace std;

void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength);
int main()
{
  char a[] = "Woozle";
  char b[] = "Heffalump";
  char c[5];
  char d[10];
  char e[20];
  concat(a, b, c, 5);
  concat(a, b, d, 10);
  concat(a, b, e, 20);
  cout << c << "\n";
  cout << d << "\n";
  cout << e << "\n";
  return 0;
}
void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength)
{
strncpy (result,a, result_maxlength);
strncat (result, b, result_maxlength);
result[result_maxlength-1] = '\0';
}
  • 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-10T15:05:17+00:00Added an answer on June 10, 2026 at 3:05 pm

    You have several issues with this code that several have pointed out thus far. A very subtle one that few seem to talk about is this: Per the POSIX spec, the behavior of strncpy will NOT set the null-terminator on your string if the number of non-null chars moved reaches the limit specified (the ‘n’). It WILL tail-fill nulls to reach ‘n’, however. Thus:

    char ar[3];
    strncpy(ar, "food", sizeof(ar)/sizeof(ar[0]));
    

    This will end up with a(0) = ‘f’, a(1) = ‘o’ and ar(2) = ‘o’. There will be no null terminator appended. with the last char ignored It is for this reason people are so minus-1 giddy about invoking this runtime library function. Accounting for this is important. Thus you will often see code like this instead.

    char ar[3];
    strncpy(ar, "food", sizeof(ar)/sizeof(ar[0])-1);
    ar[sizeof(ar)/sizeof(ar[0])-1] = 0;
    

    This will likely get what you were really looking for, namely ar = “fo”. The plenty-o-space situation reverses this. If you have a buffer with ample room the ‘n’ will tail-fill with nulls until ‘n’ is reached if the source string of the copy reaches its own terminator prior to reaching ‘n’. Thus:

    char ar[30];
    strncpy(ar, "food", sizeof(ar)/sizeof(ar[0])-1);
    ar[sizeof(ar)/sizeof(ar[0])-1] = 0;
    

    Will result in ar = “food” with twenty-six zero-chars following the ‘d’. Keeping people on their toes, the not-so-apparent memset-to-zero invoke:

    char ar[30];
    strncpy(ar, "", sizeof(ar)/sizeof(ar[0]));
    

    Yeah, its just wrong, I know.

    All of that being said it should come as no surprise that clearly strncat() behaves differently than your code appears to reflect you may currently believe as well. For example the last parameter does NOT specify the number of chars to limit in the destination. Rather, it describes the number of chars to limit from copying from the source. In other words, as you continue to tack on new strings, that tail space limiter should be continually getting shorter. Of course, to know how much involves keeping track of chars-copied with each step, and as someone pointed out, this makes strncat borderline-useless in many cases, offering up more confusion than utility.

    For reference, the precise definitions with defined behavior can be seen here strncpy and strncat. I would highly advise a clear read of both before continuing with your homework.

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

Sidebar

Related Questions

Is it possible to enter an infinite loop at compile time? My program seems
How do I compile and run a program in Java on my mac? I'm
When I compile and run a simple Win32 GUI program in MinGW+MSys with command
'??' gets converted into '^' if I compile mn VC++ program and run it
How can I check and conditionally only compile / run code if iOS5 is
I used to build and run automatically with NetBean IDE. It works fine with
I'm trying to compile a simple c++ program to run inside ESXi 3.5 console
Whether or not I compile a Racket program seems to make no difference to
HI, I would like to run a cygwin compiled program in visual studio using
Having this errors when trying to compile/run my app using Maven. I've succeeded to

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.