• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: C++: std::ostringstream default ctor problems
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++: std::ostringstream default ctor problems


  • Subject: Re: C++: std::ostringstream default ctor problems
  • From: Howard Hinnant <email@hidden>
  • Date: Mon, 28 Sep 2009 16:09:00 -0700

On Sep 27, 2009, at 6:12 PM, Dan Caugherty wrote:

Hey all --

This may or may not be a relevant topic for this group, so please
gently point me to the right place if not. If so, then please read on.

I have the following snippet of code compiling successfully on Xcode
3.2 and GCC 4.2:

const std::string
foobar::to_string() const
{
  std::ostringstream oss;
  oss << *this;       // gdb says odd things happen right here
  return oss.str();
}

Under GCC 4.2, I see the following error:

malloc: *** error for object 0xa0451db0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug


..and sure enough, the ostringstream instance is trying to free a
buffer that it never allocated. Naughty naughty.

Is this a known issue with GCC 4.2, or am I just doing something wrong?

I attempted to replicate this problem:

$ cat test.cpp
#include <iostream>
#include <sstream>

class foobar
{
public:
    const std::string to_string() const;
};

std::ostream&
operator<<(std::ostream& os, const foobar&)
{
    return os << "foobar";
}

const std::string
foobar::to_string() const
{
  std::ostringstream oss;
  oss << *this;
  return oss.str();
}

int main()
{
    foobar fb;
    std::cout << fb.to_string() << '\n';
}
$ g++ test.cpp
$ a.out
foobar

So far I am unable to reproduce your symptom (with g++-4.2).

Btw, for future-proofing your code I recommend that you do not return "const std::string", but return just "std::string" instead. In the future (C++0x) your code will run faster. That advice can be applied to all types, not just std::string.

-Howard



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: C++: std::ostringstream default ctor problems
      • From: Dan Caugherty <email@hidden>
References: 
 >C++: std::ostringstream default ctor problems (From: Dan Caugherty <email@hidden>)

  • Prev by Date: Re: C++ exception breaks inside Apple code
  • Next by Date: Re: C++ exception breaks inside Apple code
  • Previous by thread: Re: C++: std::ostringstream default ctor problems
  • Next by thread: Re: C++: std::ostringstream default ctor problems
  • Index(es):
    • Date
    • Thread