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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:09:09+00:00 2026-05-30T03:09:09+00:00

I am learning some new things in C++, and I was trying to test

  • 0

I am learning some new things in C++, and I was trying to test this strncpy_s function from Visual C++. However I am running into some problems as program crashes and I dunno whats going on but I am sure it is a pretty stupid problem. The source code is something like this:

#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <cstring>

int main()
{
    char *p;
    p=(char *)malloc(sizeof(char)*strlen("Hello!\n"));
    strncpy_s(p,strlen("Hello!\n"),"Hello!\n",strlen("Hello!\n"));
    std::cout << p;
    std::cout << strlen("Hello!\n") << std::endl;
    return 0;
}

As I said I am not using std::string coz I want to try this new function and know how it works.

  • 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-30T03:09:11+00:00Added an answer on May 30, 2026 at 3:09 am

    I take my comments back, I read the documentation more carefully. Your code is passing invalid parameters, and is invoking the invalid parameter handler. Maybe that’s what’s happening. Namely:

    p=(char *)malloc(sizeof(char)*strlen("Hello!\n"));
    

    This line allocates room for 7 characters, which is the length of the string, but not enough room for a null terminator. (This is generally an error)

    The documentation for strncpy_s says: These functions try to copy the first D characters of strSource to strDest, where D is the lesser of count and the length of strSource. If those D characters will fit within strDest (whose size is given as numberOfElements) and still leave room for a null terminator, then those characters are copied and a terminating null is appended; otherwise, strDest[0] is set to the null character and the invalid parameter handler is invoked, as described in Parameter Validation.

    Are you possibly seeing the “invalid parameter handler”?

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

Sidebar

Related Questions

I'm learning Silverlight and am trying to ingest some new concepts. Just so I
I am very new to C and I have some problems learning about pointers.
I'm currently learning OCaml, and it's giving me some problems. I'm trying to implement
Just wondering if anyone has some good resources for learning how to create new
I'm a beginner learning some assembly, when preserving the ESP register before a function
I'm new to Xcode and trying to learning myself, my problem may be naive
I'm new to Objective-C here, and still learning its syntax, so I'm experimenting some
For the last few months, I've been putting some serious effort into learning Haskell
I've been learning F# and functional programming and trying to do things the functional
I'm using a script from a ListView tutorial and am trying to get some

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.