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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:31:59+00:00 2026-06-08T02:31:59+00:00

I have the following problem: I am writing a C++ program that has to

  • 0

I have the following problem: I am writing a C++ program that has to wrap around a C library, so when I interact with the library I always have to use char* instead of std::string for all operations. In order to avoid working with char* as much as possible, I do the formatting with stringstreams, for example like this:

#include <iostream>
#include <sstream>
#include <string.h>
#include <cstdlib>

using namespace std;

int main(int argc, char** argv)
{
  ostringstream str;

  str << argv[0] << "+" << "hello";

  const char *s = str.str().c_str();

  char *y = strdup(s);

  // this I would give to a library function

  cout << y << endl;

  free(y);

  return 0;
}

As far as the output goes, the program correctly outputs “./test+hello”. However, valgrind gives me a lot of errors of the type

==30350== Invalid read of size 1
==30350==    at 0x402B858: __GI_strlen (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==30350==    by 0x4213475: strdup (in /usr/lib/libc-2.16.so)
==30350==    by 0x41B2604: (below main) (in /usr/lib/libc-2.16.so)
==30350==  Address 0x4341274 is 12 bytes inside a block of size 25 free'd
==30350==    at 0x4029F8C: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==30350==    by 0x410387A: std::string::_Rep::_M_destroy(std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.17)
==30350==    by 0x41B2604: (below main) (in /usr/lib/libc-2.16.so)

What am I doing wrong?

  • 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-08T02:32:02+00:00Added an answer on June 8, 2026 at 2:32 am
     const char *s = str.str().c_str();
    

    The str() returns a string object. You get a pointer to some internal data of it using c_str, then at the end of the line the string object is deleted. But you still have a pointer to the deleted internal string.

    You need to do it like this –

    std::string s = str.str();
    const char* s = s.c_str()
    

    to ensure that the string isn’t deleted.

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

Sidebar

Related Questions

A WPF program I'm writing in C# has the following interface on the back-end:
So I have a library I'm working on that has, or will have, optimised
I am writing Classic ASP program.In one function, I have to use 2 update
Please consider the following problem. I'm writing a quick Manipulate[] program to display a
I am writing a Log Unifier program. That is, I have a system that
I am writing a little test program, and I have a problem regarding the
I have following problem: I have to make a ASP.NET Webapplication with a two-row
I have following problem: I have built a tabbar application with 4 tabs. I
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
Hi i have following Problem. I write a Mediawiki Extension where i need 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.