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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:36:23+00:00 2026-05-28T04:36:23+00:00

Is there an exact equivalent to strncpy in the C++ Standard Library? I mean

  • 0

Is there an exact equivalent to strncpy in the C++ Standard Library? I mean a function, that copies a string from one buffer to another until it hits the terminating 0? For instance when I have to parse strings from an unsafe source, such as TCP packets, so I’m able to perform checks in length while coping the data.

I already searched a lot regarding this topic and I also found some interesting topics, but all of those people were happy with std::string::assign, which is also able to take a size of characters to copy as a parameter. My problem with this function is, that it doesn’t perform any checks if a terminating null was already hit – it takes the given size serious and copies the data just like memcpy would do it into the string’s buffer. This way there is much more memory allocated and copied than it had to be done, if there were such a check while coping. 

That’s the way I’m working around this problem currently, but there is some overhead I’d wish to avoid:

    // Get RVA of export name
    const ExportDirectory_t *pED = (const ExportDirectory_t*)rva2ptr(exportRVA);
    sSRA nameSra = rva2sra(pED->Name);

    // Copy it into my buffer
    char *szExportName = new char[nameSra.numBytesToSectionsEnd];
    strncpy(szExportName, 
            nameSra.pSection->pRawData->constPtr<char>(nameSra.offset),
            nameSra.numBytesToSectionsEnd);
    szExportName[nameSra.numBytesToSectionsEnd - 1] = 0;

    m_exportName = szExportName;
    delete [] szExportName;

This piece of code is part of my parser for PE-binaries (of the routine parsing the export table, to be exact). rva2sra converts a relative virtual address into a PE-section relative address. The ExportDirectory_t structure contains the RVA to the export name of the binary, which should be a zero-terminated string. But that doesn’t always have to be the case – if someone would like it, it would be able to omit the terminating zero which would make my program run into memory which doesn’t belong to the section, where it would finally crash (in the best case…).

It wouldn’t be a big problem to implement such a function by myself, but I’d prefer it if there were a solution for this implemented in the C++ Standard Library.

  • 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-28T04:36:24+00:00Added an answer on May 28, 2026 at 4:36 am

    If you know that the buffer you want to make a string out of has at least one NUL in it then you can just pass it to the constructor:

    const char[] buffer = "hello\0there";
    
    std::string s(buffer);
    
    // s contains "hello"
    

    If you’re not sure, then you just have to search the string for the first null, and tell the constructor of string to make a copy of that much data:

    int len_of_buffer = something;
    const char* buffer = somethingelse;
    
    const char* copyupto = std::find(buffer, buffer + len_of_buffer, 0); // find the first NUL
    
    std::string s(buffer, copyupto);
    
    // s now contains all the characters up to the first NUL from buffer, or if there
    // was no NUL, it contains the entire contents of buffer
    

    You can wrap the second version (which always works, even if there isn’t a NUL in the buffer) up into a tidy little function:

    std::string string_ncopy(const char* buffer, std::size_t buffer_size) {
        const char* copyupto = std::find(buffer, buffer + buffer_size, 0);
    
        return std::string(buffer, copyupto);
    }
    

    But one thing to note: if you hand the single-argument constructor a const char* by itself, it will go until it finds a NUL. It is important that you know there is at least one NUL in the buffer if you use the single-argument constructor of std::string.

    Unfortunately (or fortunately), there is no built in perfect equivalent of strncpy for std::string.

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

Sidebar

Related Questions

Is there a tool that can list the exact versions and public keys of
Is there a way to get the exact name of the object that this
Is there a way to provide a method implementation (that bears the exact same
Exact duplicate of Function name for creating something if it's not there yet I
I'm getting an error from a deterministic PG function that seems dependent based on
Is there a verbose exact equivalent for the '&' call operator to use with
I haven't found an existing question on this exact problem (there were several that
I'm not sure there is this exact situation in another question, so sorry if
Is there an exact overview what has changed in the SP1 for .NET 3.5?
Is there an exact opposite action to join in linQ to SQL? I want

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.