I have written this program in C++, its not quite finished yet, but I am testing it and I keep getting this error Hyphen(36636,0x7fff7d65b300) malloc: *** error for object 0x10d31858e: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug
and I'm not quite sure what to do about it, I'm new to C++, I know my code isn't the greatest, but any help is greatly appreciated. Thank you.

- 21
- 1
- 5
-
That's too much code to dig through. Please post a [mcve]. – R Sahu Sep 21 '16 at 05:39
-
Can you include the main function as well. – nishantsingh Sep 21 '16 at 05:40
2 Answers
Here is a major problem:
char *tempCharArray = returnArray;
free(returnArray);
return tempCharArray;
You copy the pointer into tempCharArray
, free the memory pointed to by the pointer, and then return the pointer. When using the pointer it no longer points to allocated memory and you will have undefined behavior.
Then there's this:
char* goHyphen(const char* input) {
...
string *finWords = new string[numWords];
...
return (char*)finWords;
}
This is really really wrong. Especially considering how you use it:
char* actual = goHyphen( input );
bool equal = strcmp( expected, actual ) == 0;
That is another major problem right there, and again undefined behavior.
And you also do
delete actual;
which is another problem that leads to undefined behavior (you should be using delete[]
here).
By the way, it might be this last delete
that causes your problem (you really need to learn how to use a debugger): You pass a pointer to a string literal to the goHyphen
function, and it's possible that the goHyphen
function returns this pointer. Then you try to delete
this pointer, even if you haven't allocated it (i.e. when it points to the string literal).
You seem to rely to much on pointers, stop doing that. Use std::vector
instead of dynamic allocation. Also you mix new[]
/delete[]
(and in some cases new[]
and delete
) with malloc
/free
, don't do that (and if you use std::vector
you don't have to do that).
And here's a good rule of thumb: If you need to do a C-style cast, that's a sign of you doing something you should not be doing.
Another rule of thumb: If you need a string use std::string
instances (i.e. objects), and if you need a "dynamic array" use a std::vector
object.
Lastly: Stay away from pointers as much as possible. In modern C++ there is almost no need for pointers outside of polymorphism.

- 400,186
- 35
- 402
- 621
-
Thank you for your help. For the first part, I forgot to remove that before posting this, sorry about that, I was trying anything to get rid of the error and that was my last shot in the dark. As for the issue with the method 'goHyphen', I'm not too sure what the issue is when you say it is wrong, again I'm brand new to C++, this is for a project in class and the code from the main is provided by our professor so I cannot really change that. – Patrick Hennis Sep 21 '16 at 05:54
-
@PatrickHennis If you're "brand new to C++" you're doing something way to complicated IMO. Stop. Take a few steps back, [find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start over. – Some programmer dude Sep 21 '16 at 05:55
-
@PatrickHennis Oh, and tell your professor to change that `delete actual` to `delete[] actual`. That might be a good starting point, along with you needing to do string duplication (i.e. create memory using `new[]`) when returning the argument passed in. – Some programmer dude Sep 21 '16 at 05:59
your code
char *tempCharArray = returnArray;
free(returnArray);
return tempCharArray;
is anyway want to take tempCharArray and convert it into std::string at
std::string tempString(hyphenated);
You can directly use std::string instead of char array in your function and change it as
std:: string insertHyphenNorm(const char* word, int positionToInsertAt) {
Then you don't need any unnecessary memory allocations and deletions

- 7,554
- 2
- 25
- 36