0

I have a ostringstream object that I am trying to insert some characters into, but the ostringstream object is in a shared_ptr called pOut. When I try to dereference pOut, I always get an Access Violation error.

This is a simplified version of what I am trying to do:

#include <iostream>
#include <sstream>

int main()
{
  std::shared_ptr<std::ostringstream> pOut;
  *pOut << "Hello";
  std::cout << pOut->str();
}

In my head this should work because the program seen below compiles and runs with no issues:

#include <iostream>
#include <sstream>

int main()
{
  std::ostringstream out;
  out << "Hello";
  std::cout << out.str();
}

How come dereferencing the object raises an Access Violation error, and how can I solve this problem? Below is the error I am getting.

Exception thrown at 0x00A22112 in MemoryIssueTest.exe: 0xC0000005: Access violation reading location 0x00000000.

M--
  • 25,431
  • 8
  • 61
  • 93
bmb
  • 361
  • 2
  • 13
  • 3
    `std::shared_ptr pOut;` what do you think this points to? – UnholySheep Sep 25 '20 at 21:52
  • 5
    Use `std::shared_ptr pOut = std::make_shared();` – πάντα ῥεῖ Sep 25 '20 at 21:52
  • 3
    *How come dereferencing the object raises an Access Violation error* -- Because it doesn't point to anything valid? Regardless of how smart the pointer is, it has to point to somewhere valid first. They're not *that* smart. – PaulMcKenzie Sep 25 '20 at 21:54
  • @UnholySheep I think it points to an uninitialized `std::ostringstream` object. However, in the second example I am able to insert characters into an unitialized `std::ostringstream` object. That is where my confusion stems from – bmb Sep 25 '20 at 21:57
  • 1
    Does this answer your question? [0xC0000005: Access violation reading location 0x00000000](https://stackoverflow.com/questions/10478941/0xc0000005-access-violation-reading-location-0x00000000). In any case, please search for the error message online before asking yet another question. – Ulrich Eckhardt Sep 25 '20 at 21:58
  • 2
    Well both your assumptions are wrong. Your pointer is a null-pointer, so it doesn't point to any object. And the second example uses the default constructor, so it is not an uninitialized object – UnholySheep Sep 25 '20 at 21:58
  • C++ is not a language that rewards assumptions with logically correct, executable code. – user4581301 Sep 25 '20 at 21:59
  • @UnholySheep I see.. Thank you for clarifying. I thought to use the default constructor, you had to include the empty braces. – bmb Sep 25 '20 at 22:02
  • 1
    Unfortunately you may find that C++ has some peculiar aspects when it comes to the different forms of initializing objects (since the behavior of not having braces versus having empty braces changes depending on whether the type defines an explicit default constructor) - it's something that has to be learnt. – UnholySheep Sep 25 '20 at 22:08

1 Answers1

1

You created the pointer object, but it set to nullptr or NULL or 0 initially. So accessing that memory would surely and certainly cause segmentation fault or access violation. You need to give a value to it. So instead of this:

std::shared_ptr<std::ostringstream> pOut;

Use this:

std::shared_ptr<std::ostringstream> pOut = std::make_shared<std::ostringstream>();

This should solve your problem.

Akib Azmain Turja
  • 1,142
  • 7
  • 27