2

I'm an amateur programmer learning how to use c++ in xcode, and I've been trying to create a program where you answer asked questions, and the questions are different depending on your answers. The thing is, I keep getting the error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0), and I have no idea what caused it. Here's my code currently:

#include <iostream>
#include <string>
using namespace std;
int main() {


    string name;
    cout << "what is your name ";
    getline (std::cin, name);
    string yes;
    cout << "ok, " << name << ", do you want to play a game?  ";
    getline (std::cin, yes);
    cout << "\nno " << std::endl;

    string input =0;
    cin >> input;
    string Yes = "yes";
    string No = "no";

    if (input == No)
    {
        cout << "ok, sorry" << endl;
    }
    else if (input == Yes)
    {
        cout << " a question" << endl;
    }
}
j_lo2004
  • 21
  • 1
  • 2
  • `string input =0;` [Rubber ducky](https://en.wikipedia.org/wiki/Rubber_duck_debugging) wants to know what you want to happen here. – user4581301 May 11 '18 at 01:33
  • Also, if you're an amateur programmer, I suggest reading good C++ material. No C++ book has the error pointed out by user4581301 in any code examples, unless it is a typo on your part. – PaulMcKenzie May 11 '18 at 01:35
  • its to declare "input" as the identifier for the rest of the code. most of this is going off of random tutorials pieced together, so I'm not entirely sure. sorry. – j_lo2004 May 11 '18 at 01:38
  • @j_lo2004 -- Why are you setting the string to '0'? Remove the setting to 0 stuff. – PaulMcKenzie May 11 '18 at 01:41
  • A `string` is a sequence of characters. Typically you would assign something in quotes. `string input = "Nothing to see here. Move along.";` Assigning a number to it is unusual. In this case it's fatal because of some really neat stuff happening behind the scenes. I think I know what happened and I'm running a few experiments to see if I'm right. – user4581301 May 11 '18 at 01:42
  • @PaulMcKenzie what do you suggest I read? also, is there any other way I could write this program? I really want to learn this language, but I feel like a complete idiot when I try and understand basic code or tutorials. – j_lo2004 May 11 '18 at 01:43
  • [Fortunately Stack Overflow maintains a semi-curated list of recommended texts](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for times like this. I'll also recomment a look at https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines for some excellent hints, suggestions and guidelines. – user4581301 May 11 '18 at 01:44
  • If you don't have the cash for a good quality book, [Stroustrup's A Tour of C++](https://isocpp.org/tour) can provide a decent bulwark protecting you from absorbing bad tutorials on the Internet, but it's very terse. – user4581301 May 11 '18 at 01:47
  • @PaulMcKenzie when I remove it, there are undeclared identifier errors, and when I change the value it says "No viable conversion from 'int' to 'string' (aka 'basic_string, allocator >') ". I think I'm just going to start from scratch. – j_lo2004 May 11 '18 at 01:52
  • What did you remove? Just get rid of the assignment. All you need is `std::string input;`. – PaulMcKenzie May 11 '18 at 01:53
  • @PaulMcKenzie when I do that, I still get a "No viable conversion from 'int' to 'string' (aka 'basic_string, allocator >') ". I don't know what I'm doing wrong. – j_lo2004 May 11 '18 at 02:00
  • @j_lo2004 [This compiles with no errors](http://coliru.stacked-crooked.com/a/e9f6728b5d0ab344). I took your code, did exactly as was directed to do, and no compiler error. – PaulMcKenzie May 11 '18 at 02:02

1 Answers1

3

Solution

Change

string input =0;

to

string input;

And input will be constructed as an empty string.

So What Just Happened?

string input =0;

invokes a string constructor with the number 0. There is no direct constructor for string that takes an integer, but

string(const CharT* s, 
       const Allocator& alloc = Allocator() );

is close enough. Integer 0 is treated as a pointer to 0, NULL, and the poor string tries to construct itself from a null pointer. This means it's going to copy characters from NULL until it finds a string-terminating null character.

Fortunately that march to nowhere is halted quickly because the first few thousand bytes of memory are almost always tagged as a no-go zone, crashing the program the instant you try to access anywhere in the zone. This makes a null pointer bug almost instantly detectable and very easy to debug. Just wait for the program to crash and then walk back up the stack to see where the null pointer came from. It might take a bit more work to find out why it was null, but if programming was easy, everybody'd be doing it.

To test the hypothesis, I've thrown together a simple class to simulate the behaviour without crashing

#include <iostream>

class test
{
public:
    test (const char * valp)
    {
        std::cout << "in char * constructor. Got: " << (void*) valp;
    }
};
int main() {
    test input =0;
}

The output is

in char * constructor. Got: 0

Showing that 0 was indeed enough to convince test to accept 0 as a char pointer.

Note that this is just a special case around zero, most likely to support the good ol' #define NULL 0 macro and

test input =1;

is successfully caught by the compiler as nonsense and rejected.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54