PDA

View Full Version : Easy Question - new to programming




cjmac19
Apr 18, 2013, 10:56 AM
I wrote the following code, a simple string test and I get an error message every time I run it saying "Hey(5031) malloc: *** error for object 0x1000041c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap"

How do I fix this?


#include <iostream>
#include <string>

using namespace std;

int main ()
{
string user_first_name;
string user_last_name;

cout << "First name: ";
cin >> user_first_name;
cout << "Last name: ";
cin >> user_last_name
string user_full_name = user_first_name + " " + user_last_name;

cout << "Your name is " << user_full_name << "\n";
}



cqexbesd
Apr 18, 2013, 01:39 PM
I wrote the following code, a simple string test and I get an error message every time I run it saying "Hey(5031) malloc: *** error for object 0x1000041c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap"

How do I fix this?


I haven't done c++ in a while (thankfully!) but the code looks OK to me (apart from a missing ";" ). I don't see any problems when I run the code either. How are you compiling it? I just used clang++ -Wall blah.cpp.

/me now waits for the more regular C++ users to point out what we both missed.

Andrew

willieva
Apr 18, 2013, 01:46 PM
Since you couldn't have actually compiled the code you posted as it's missing a semicolon, my guess is your error is from something else.

If you were to get that code to compile it would run fine.

lee1210
Apr 18, 2013, 02:13 PM
This is a sticky at the top of the forum:
http://guides.macrumors.com/Why_simple_CPlusPlus_programs_in_XCode_crash_when_using_std::string

Sounds like what you're seeing.

-Lee

ArtOfWarfare
Apr 18, 2013, 09:39 PM
I wasn't aware of + working on strings without the user implementing it themselves in C++... I thought you had to use a bunch of << into a string buffer and then use the string function to convert that buffer to a new string if you wanted to concatenate strings?

ytk
Apr 18, 2013, 11:18 PM
Well, why not give it a try and see if you're right or not? :rolleyes:

gnasher729
Apr 19, 2013, 12:45 PM
I wasn't aware of + working on strings without the user implementing it themselves in C++... I thought you had to use a bunch of << into a string buffer and then use the string function to convert that buffer to a new string if you wanted to concatenate strings?

You can. operator+ is implemented, and there is automatic conversion from C string to std::string. You have to be awfully careful. For example

std::string x = "3";
std::string y = "1" + 2 + x;

will compile just fine, but will most likely crash. If you think hard enough about it you'll know why. While

std::string z = x + 2 + "1"; will not compile (I think it won't. If it does it will work).

There are other methods like "append", I think "replace", that are quite useful.

lloyddean
Apr 19, 2013, 01:38 PM
The code is good it compiles and run just fine for me!

Do you have any other issues with your computer?
What is the size and free space available on the hard disk of your computer?
How much RAM do you have?