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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:06:40+00:00 2026-06-03T13:06:40+00:00

If I have a std::vector buffer that has room for 100 characters and I

  • 0

If I have a std::vector buffer that has room for 100 characters and I call a C function that will write a NULL terminated string like writestring(&buffer[0], buffer.size()) from what I’ve read that’s ok if there is already data in buffer. But if the vector is empty the behavior is undefined. How do I get around that, can I write something to the buffer like “qwerty” and then call writestring() so that I know the behavior is always defined? What kind of storage do you guys use when calling C functions? I just read about .data but I don’t know how common or portable that is.

Thanks

  • 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-03T13:06:44+00:00Added an answer on June 3, 2026 at 1:06 pm

    C++

    std::vector<char> v;
    v.resize(100);
    writestring(&v[0], v.size());
    

    C

    void writestring(char*p, size_t len) {
      strncpy(p, "Doofi", len);
    }
    

    if the vector is empty the behavior is undefined. How do I get around that?

    By passing a non-empty buffer. Use the .resize() method to quickly fill the vector, or construct it to be the right size initially: std::vector<int> v(100);.

    What kind of storage do you guys use when calling C functions?

    std::vector is a fine choice for this application. To C, it looks just like an array or a malloc’d region. But, realize that C’s pointer is only valid as long as the vector is neither reallocated or destroyed.

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

Sidebar

Related Questions

I have a (void*) buffer that I need to convert to std::vector<unsigned char> before
I have a std::vector. I want to create iterators representing a slice of that
I have a std::vector<uint8_t> that contains strings at specific offsets. Here's a shortened dump:
I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as
I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
Suppose I have the following: std::string TestFragmentation() { std::vector<char> buffer(500); SomeCApiFunction( &buffer[0], buffer.size() );
I have a std::vector<int> , and I want to delete the n th element.
I have a std::vector with the element as pair and need to find all
I have an std::vector<unsigned char> with binary data in. I just would like to
Say, I have a std::vector<SomeClass *> v; in my code and I need 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.