Thread: Undefined Behaviour (Palindromic Number Finder)

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    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:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
      char * string = new char[30];
      int highest = 0;
      int temp = 0;
    
         for (int x = 0; x < 1000; x++)
         for (int y = 0; y < 1000; y++) {
    	itoa(x * y, string, 10);
    	   if ( ((int)string[0] == (int)string[5]) && ((int)string[1] == (int)string[4]) )
    	      if ((int)string[2] == (int)string[3])
    	         temp = (int)string;
    		 if (highest < temp)
    		    highest = temp;
    	}
       cout << highest << endl;
    }
    Cheers.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > temp = (int)string;
    What's this do?
    Surely not "1234" into 1234

    Also consider that for small numbers, your array access are past the end of the string, and so you're reading garbage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by Salem View Post
    > temp = (int)string;
    What's this do?
    Surely not "1234" into 1234

    Also consider that for small numbers, your array access are past the end of the string, and so you're reading garbage.
    Hmmm that's what I originally thought temp = (int)string; would do, but now when I look at it that doesn't really make sense, you're right. Now to get around this :/
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    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:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
    
    	char * string = new char[7];
    	int product = 0;
    	int palin = 0;
    
    	for (int x = 1; x < 999; x++)
    		for (int y = 1; y < 999; y++) {
    			product = x * y;
    			itoa(product, string, 10);
    				if ( ((int)string[0] == (int)string[5]) && ((int)string[1] == (int)string[4]) )
    					if ((int)string[2] == (int)string[3])
    						if (product > palin)
    							palin = product;
    		}
    	cout << palin;
    	delete string;
    }
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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[]).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    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.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by pobri19 View Post
    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 ^_^
    You can use a local array. It works just the same, you'll see.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by Elysia View Post
    You can use a local array. It works just the same, you'll see.
    I tried that, it wouldn't work
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But it does. What did you code look like?
    Did you do something like:
    Code:
    char buf[30];
    itoa(&buf, ...)
    ?
    In such case, the correct code would be:
    Code:
    char buf[30];
    itoa(buf, ...)
    (Lost the &.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Oh wow I'm an idiot LOL, I did:

    Code:
    char * string[7];
    I forgot to take out the pointer when I backspaced the new ;p haha. Yeah it works lol, thanks!
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your comparisons are still comparing past the end of the string.

    Eg.
    1*1 = 1

    Which in a string is
    "1\0"

    Comparing past string[2] is garbage data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Is this undefined behaviour?
    By caduardo21 in forum C Programming
    Replies: 4
    Last Post: 01-15-2006, 12:55 PM
  3. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  4. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM