Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 18, 2013, 11: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 01:22 PM. Reason: code tags added
cjmac19 is offline   0 Reply With Quote
Old Apr 18, 2013, 02: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, 02: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, 03: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, 10:39 PM   #5
ArtOfWarfare
macrumors 603
 
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?
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   0 Reply With Quote
Old Apr 19, 2013, 12:18 AM   #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, 01:45 PM   #7
gnasher729
In Time-Out
 
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, 02: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

Similar Threads
thread Thread Starter Forum Replies Last Post
iMovie '09 - SUCH an easy question for someone... TheGreatMuta Digital Video 2 Mar 17, 2013 10:06 PM
Basic C Programming Question - Can anyone help? 007bond Mac Programming 3 Feb 6, 2013 01:15 PM
UNIX question (easy) thenightwatch OS X 5 Oct 4, 2012 09:40 AM
Programming Career Question macuser1232 Mac Programming 47 Jul 23, 2012 06:20 PM

Forum Jump

All times are GMT -5. The time now is 11:29 AM.

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

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