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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:35:08+00:00 2026-05-20T06:35:08+00:00

while I was reading nVidia CUDA source code, I stumbled upon these two lines:

  • 0

while I was reading nVidia CUDA source code, I stumbled upon these two lines:

    std::string stdDevString;

    stdDevString = std::string(device_string);

Note that device_string is a char[1024]. The question is: Why construct an empty std::string, then construct it again with a C string as an argument? Why didn’t they call std::string stdDevString = std::string(device_string); in just one line?

Is there a hidden string initialization behavior that this code tries to evade/use? Is to ensure that the C string inside stdDevString remains null terminated no matter what? Because as far as I know, initializing an std::string to a C string that’s not null terminated will still exhibit problems.

  • 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-20T06:35:08+00:00Added an answer on May 20, 2026 at 6:35 am

    Why didn’t they call std::string stdDevString = std::string(device_string); in just one line?

    No good reason for what they did. Given the std::string::string(const char*) constructor, you can simply use any of:

    std::string stdDevString = device_string;
    std::string stdDevString(device_string);
    std::string stdDevString{device_string}; // C++11 { } syntax
    

    The two-step default construction then assignment is just (bad) programmer style or oversight. Sans optimisation, it does do a little unnecessary construction, but that’s still pretty cheap. It’s likely removed by optimisation. Not a biggie – I doubt if I’d bother to mention it in a code review unless it was in an extremely performance sensitive area, but it’s definitely best to defer declaring variables until a useful initial value is available to construct them with, localising it all in one place: not only is it less error prone and cross-referenceable, but it minimises the scope of the variable simplifying the reasoning about its use.

    Is to ensure that the C string inside stdDevString remains null terminated no matter what?

    No – it made no difference to that. Since C++11 the internal buffer in stdDevString would be kept NUL terminated regardless of which constructor is used, while for C++03 isn’t not necessarily terminated – see dedicated heading for C++03 details below – but there’s no guarantees regardless of how construction / assignment is done.

    Because as far as I know, initializing an std::string to a C string that’s not null terminated will still exhibit problems.

    You’re right – any of the construction options you’ve listed will only copy ASCIIZ text into the std::string – considering the first NUL ('\0') the terminator. If the char array isn’t NUL-terminated there will be problems.

    (That’s a separate issue to whether the buffer inside the std::string is kept NUL terminated – discussed above).

    Note that there’s a separate string(const char*, size_type) constructor that can create strings with embedded NULs, and won’t try to read further than told (Constructor (4) here)

    C++03 std::strings were not guaranteed NUL-terminated internally

    Whichever way the std::string is constructed and initialised, before C++11 the Standard did not require it to be NUL-terminated within the string’s buffer. std::string was best imagined as containing a bunch of potentially non-printable (loosely speaking, binary in the ftp/file I/O sense) characters starting at address data() and extending for size() characters. So, if you had:

    std::string x("help");
    x[4];  // undefined behaviour: only [0]..[3] are safe
    x.at(4); // will throw rather than return '\0'
    x.data()[4]; // undefined behaviour, equivalent to x[4] above
    x.c_str()[4]; // safely returns '\0', (perhaps because a NUL was always
                  // at x[4], one was just added, or a new NUL-terminated
                  // buffer was just prepared - in which case data() may
                  // or may not start returning it too)
    

    Note that the std::string API requires c_str() to return a pointer to a NUL-terminated value. To do so, it can either:

    • proactively keep an extra NUL on the end of the string buffer at all times (in which case data[5] would happen to be safe on that implementation, but the code could break if the implementation changed or the code was ported to another Standard library implementation etc.)
    • reactively wait until c_str() is called, then:

      • if it has enough capacity at the current address (i.e. data()), append a NUL and return the same pointer value that data() would return
      • otherwise, allocate a new, larger buffer, copy the data over, NUL terminate it, and return a pointer to it (typically but optionally this buffer would replace the old buffer which would be deleted, such that calling data() immediately afterwards would return the same pointer returned by c_str())
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While reading about ASP.NET Ajax toolkit I stumbled upon the term AJAH. What is
While reading a binary file using DJGPP on DOS this code hangs. This happens
While reading C++ Primer Plus 5th edition, I saw this piece of code: cin.get(ch);
While reading a book on C#, I have come across code that uses the
While reading an Asp.Net MVC code sample that used MbUnit as it's testing framework,
I came across this class while reading a C# book and have some questions.
I am a novice-intermediate programmer taking a stab at AJAX. While reading up on
Functional programming .. is like classic ( Mark Twain's type ). While reading another
Hai in vc++6.0 MFC, i connected a serial port, while reading and displaying a
While I'm googling/reading for this answer I thought I would also ask here. I

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.