Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 18, 2013, 10:56 AM   #1
cjmac19
macrumors newbie
 
Join Date: Apr 2013
Easy Question - new to programming

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?

Code:
#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";
}

Last edited by maflynn; Apr 18, 2013 at 12:22 PM. Reason: code tags added
cjmac19 is offline   0 Reply With Quote
Old Apr 18, 2013, 01:39 PM   #2
cqexbesd
macrumors member
 
Join Date: Jun 2009
Quote:
Originally Posted by cjmac19 View Post
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
cqexbesd is offline   0 Reply With Quote
Old Apr 18, 2013, 01:46 PM   #3
willieva
macrumors 6502
 
Join Date: Mar 2010
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.
willieva is offline   0 Reply With Quote
Old Apr 18, 2013, 02:13 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
This is a sticky at the top of the forum:
http://guides.macrumors.com/Why_simp...ng_std::string

Sounds like what you're seeing.

-Lee
lee1210 is offline   0 Reply With Quote
Old Apr 18, 2013, 09:39 PM   #5
ArtOfWarfare
macrumors 601
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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?
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed.
Including the ones paired with other Macs on your network.
ArtOfWarfare is offline   0 Reply With Quote
Old Apr 18, 2013, 11:18 PM   #6
ytk
macrumors regular
 
Join Date: Jul 2010
Well, why not give it a try and see if you're right or not?
ytk is offline   0 Reply With Quote
Old Apr 19, 2013, 12:45 PM   #7
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by ArtOfWarfare View Post
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.
gnasher729 is offline   0 Reply With Quote
Old Apr 19, 2013, 01:38 PM   #8
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
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?
lloyddean is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 12:51 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC