Undefined Behaviour (Palindromic Number Finder)

This is a discussion on Undefined Behaviour (Palindromic Number Finder) within the C++ Programming forums, part of the General Programming Boards category; Hi, so I'm trying to get the highest palindromic number from the results of a 3digit multiplied by 3 digit ...

Undefined Behaviour (Palindromic Number Finder)

Hi, so I'm trying to get the highest palindromic number from the results of a 3digit multiplied by 3 digit result. Aka 1*1 - 999*999, or any combination of those numbers.

I think there's some weird undefined behaviour going on, because I swear that I saw 2 different results from the same run. The problem is, it's giving a 7 digit result (And it's not a palindromic number). 999*999 is a 6 digit number, therefore it's impossible for the result to be a 7 digit number... So something really strange is going on, and I can't see what's wrong. Here's the code:

You really should not be allocating your memory on the heap, because it's not necessary and you never free it. You can just as well create it on the stack.
But even so, unless really necessary, you should use std::string instead of char.
It's possible to convert strings to number through boost (best C++ solution I know of) or strtol (or some other function?).

It's all good now The simple solution was staring me in the face the whole time, I had to store x * y in an integer >< Instead of trying to screw around with a way to store it BACK into an integer to do comparison, I don't know why I didn't think of it earlier lol:

Why do you use a string in the first place? You only perform integer operations.
Also, itoa is not really standard, so it's better to use stringstreams or sprintf.
Also, your delete string should really be delete [] string (because you use new[]).

Because I needed to store each digit of the decimal number in an array element. The reason I'm using a pointer to a new character array is because itoa takes a pointer to a character array. I could use something else. But there's not much need anymore lol ^_^ I'll keep it in mind for future reference though. Cheers for the help.

Just be sure to not use numbers > 999 999, or you'll get buffer overruns!
You can, of course, increase the space of the array. The biggest number a 64-bit can hold is 18 446 744 073 709 551 615 (20 digits). So an array of 21 elements or bigger should keep you safe.