1

I wanna use a magic number as a constant in checking that a memory block has not been violated, is there a method of "reverse-checking" to bring back the signature into the hexadecimal format of MAGIC_32BIT

 #define MAGIC_32BIT 0x77A5844CU
 int signature = (int)MAGIC_32BIT;

Also, i wanna use a more creative magic number, any ideas on generating them or Rules to follow? No offence but i heard of the microsoft's 0xB16B00B5 and would like mine to be more human "readable".

ryantata
  • 137
  • 1
  • 3
  • 12
  • 3
    `0xBADF00D`? Not my invention though, its already used by Apple for their crashlogs, but it is pretty human readable. – JustSid Aug 19 '12 at 05:23
  • Just be creative. Look at hexadecimal digits that appear to map to alphabetical letters (almost all of them, if you cross your eyes enough) and make some words. Experiment. – Michael Petrotta Aug 19 '12 at 05:25
  • 1
    Here is a list: [http://en.wikipedia.org/wiki/Hexspeak#Notable_magic_numbers](http://en.wikipedia.org/wiki/Hexspeak#Notable_magic_numbers) – cegfault Aug 19 '12 at 05:26

2 Answers2

1

Yeah i found the answer, straight up checking with macros, cegfault's wiki comment showed that int variables are inter-changeable without the use of casting needed.

#define MAGIC_NUM 0x8BADF00D
#define CHECK_SIG(A) (A == MAGIC_NUM)
ryantata
  • 137
  • 1
  • 3
  • 12
  • 4
    Hm, sounds like you are going to do some low level memory stuff without knowing the very essential basics of C. Maybe you should start with learning everything about the type system and the promotion rules first. C gives you all the freedom to do what you want, but you should always know what you are doing. – Secure Aug 19 '12 at 07:13
1

I realize this response may not answer your question, but I still hope that it may be of some help.

Your 'magic number' should really depend upon both your application, and the type of memory corruption you want to detect or are expecting.

I've seen OSes that initialized a task's entire stack with 0xEE--a value that is both easily recognizable and unlikely to be used by most people. This method could be used to guesstimate the amount of unused stack space by counting the the 0xEE bytes. Is it perfect--no; but it is quick, (fairly) cheap and easy to do. One of the benefits to this is that you can sometimes easily identify which bytes are getting corrupted (say if you have a couple of non-0xEE bytes in a sea of 0xEE bytes). The basic idea should be transferrable to other areas.

You could go the custom route and have a unique magic number per data structure--say a CRC. It's more expensive, but it will be better at detecting whether the data structure has been corrupted or not. It will not tell you where/how/when it was corrupted, but only whether it was or wasn't. This unfortunately would fail your human readable request.

If your memory blocks are large enough, it might be possible and practical to take advantage of the MMU and protect your memory blocks by disabling writes to them by default, and enabling them only for the duration of time you need to make changes. This method would have some write performance penalties, but it can help detect when, where and by whom the corruption is occurring. This completely eliminates the magic number.

Hope this helps.

Sparky
  • 13,505
  • 4
  • 26
  • 27