Application crash

I'm working on a morse code translator and whenever the letter b is entered the application crashes. More specifically if I I take out the last character from the morse for 'b' (a space) it works fine. Crashes with error -1073741819 (0xC0000005)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;


int main()
{
cout << "Enter sentence\n";
string a;
string out;
getline (cin,a);
int g = a.size();
for(int b=0;b <=g;b++){

switch (a[b]){

case 'a' :
out[out.size()] = '.';
out[out.size()+1] = '-';
out[out.size()+2] = ' ';
cout << out[out.size()] << out[out.size()+1] << out[out.size()+2];


break;


case 'b':
out[out.size()] = '-';
out[out.size()+1] = '.';
out[out.size()+2] = '.';
out[out.size()+3] = '.';
out[out.size()+4] = ' ';
cout << out[out.size()] << out[out.size()+1] << out[out.size()+2] << out[out.size()+3]<< out[out.size()+4];
break;


}


}

}
Array index goes from 0 to n-1.
Accessing out of bounds will not resize them.
Dude!! You are writing memory that you don't own. Do you know what out.size() returns? If you do, then why do you use it to write to out?? If you don't know what out.size() means, then OK, I understand why you're in trouble.

In a sentence: Everywhere you are using out is wrong. You are going beyond the allocated memory and therefore incurring in illegal reads and writes. Let's talk about line 20 for instance: out[out.size()] = '.';. In this line you are saying "I want to write the character that is beyond the last character in the current string held by the variable 'out'". Well, it is cool, but how can you be sure that byte is good for writing? The answer is that you don't know if it is good for writing.

So what's the solution? Simple: out += '.';. Just like that. By using operator+ the string object will ensure the memory is available for the new character and then append it to whatever it has, which is nothing at first, just so you know.

So there you have it. In case you are curious: Why does it seem to work OK for 4 writes? You got lucky! Most likely the implementation of std::string of your C++ automatically reserves a very small amount of bytes (looks like 4 or 5 bytes) to operate fast on requests that require just a few characters.
Thanks for reply but I still don't understand where I'm going wrong. Out.size() returns the number of characters in a string, right? So on line 20 I access the first element (index 0) by referring to the current size of the string (the first time the loop runs this will be 0 because the empty string is 0 characters). I shouldn't be going beyond allocated memory because string class automatically sets the required size of the string required. I hope I'm not coming across as arrogant (I'm a complete beginner at this) but I really want to know what I'm not understanding here.

I shouldn't be going beyond allocated memory because string class automatically sets the required size of the string required

It does not adjust the size if you write directly into the char array buried deep within, which is what you're doing. The string starts with a size of zero, and that never changes in your code.
Last edited on
rozick1 wrote:
Thanks for reply but I still don't understand where I'm going wrong.


Everywhere you see the 'out' variable is incorrectly used.

rozick1 wrote:
I shouldn't be going beyond allocated memory because string class automatically sets the required size of the string required.


It doesn't do this EVERY TIME. You are using operator[] to access the individual characters of the string, and this very particular operator doesn't do bounds check. You can read about it here: http://www.cplusplus.com/reference/string/string/operator[]/ .

Your problem lies in the fact that you think everything you do to the string object will trigger a bound check, but it doesn't happen like that. I suggest that you carefully read the documentation on std::string in order to fully understand.

And finally, you should do out += ".- ";. All characters at once. No need to do one by one.
Since you thought that your string is adapted to the new entered characters you can use push_back to actually do this (add character to your string instead of trying to write to a memory you don't own).

Another option is += operator (also would be fine).

You probably should read the string reference:
http://www.cplusplus.com/reference/string/string/
Topic archived. No new replies allowed.