magic-numbers

What are “magic numbers” in computer programming?

烂漫一生 提交于 2019-11-27 04:40:26
问题 When people talk about the use of "magic numbers" in computer programming, what do they mean? 回答1: Magic numbers are any number in your code that isn't immediately obvious to someone with very little knowledge. For example, the following piece of code: sz = sz + 729; has a magic number in it and would be far better written as: sz = sz + CAPACITY_INCREMENT; Some extreme views state that you should never have any numbers in your code except -1, 0 and 1 but I prefer a somewhat less dogmatic view

How to get magic number of a binary file

别来无恙 提交于 2019-11-26 21:19:27
问题 There is a magic number associated with each binary file , does anyone know how to retrieve this information from the file? 回答1: Use libmagic from the file package to try and sniff out the type of file if that's your goal. There are no general "magic" numbers in binary files on unix, though different formats might define their own. The above library knows about many of those and also use various other heuristics to try and figure out the format/type of file. 回答2: file <file_name> magic

The cause of “bad magic number” error when loading a workspace and how to avoid it?

让人想犯罪 __ 提交于 2019-11-26 13:49:58
问题 I tried to load my R workspace and received this error: Error: bad restore file magic number (file may be corrupted) -- no data loaded In addition: Warning message: file ‘WORKSPACE_Wedding_Weekend_September’ has magic number '#gets' Use of save versions prior to 2 is deprecated I'm not particularly interested in the technical details, but mostly in how I caused it and how I can prevent it in the future. Here's some notes on the situation: I'm running R 2.15.1 on a MacBook Pro running Windows

Magic number in boost::hash_combine

巧了我就是萌 提交于 2019-11-26 10:14:21
The boost::hash_combine template function takes a reference to a hash (called seed ) and an object v . According to the docs , it combines seed with the hash of v by seed ^= hash_value(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); I can see that this is deterministic. I see why a XOR is used. I bet the addition helps in mapping similar values widely apart so probing hash tables won't break down, but can someone explain what the magic constant is? The magic number is supposed to be 32 random bits, where each is equally likely to be 0 or 1, and with no simple correlation between the bits. A

HowTo extract MimeType from a byte[] [duplicate]

微笑、不失礼 提交于 2019-11-26 06:45:36
问题 This question already has answers here : Getting A File's Mime Type In Java (21 answers) Closed 3 years ago . I\'ve a web page that that can be used to upload files. Now I need to check if the file type is correct (zip, jpg, pdf,...). I can use the mimeType that comes with the request but I don\'t trust the user and let\'s say I want to be sure that nobody is able to upload a .gif file that was renamed in .jpg I think that in this case I should inspect the magic number. This is a java library

Magic number in boost::hash_combine

a 夏天 提交于 2019-11-26 02:06:20
问题 The boost::hash_combine template function takes a reference to a hash (called seed ) and an object v . According to the docs, it combines seed with the hash of v by seed ^= hash_value(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); I can see that this is deterministic. I see why a XOR is used. I bet the addition helps in mapping similar values widely apart so probing hash tables won\'t break down, but can someone explain what the magic constant is? 回答1: The magic number is supposed to be 32

What is a magic number, and why is it bad? [closed]

旧街凉风 提交于 2019-11-25 21:40:02
问题 What is a magic number? Why should it be avoided? Are there cases where it\'s appropriate? 回答1: A magic number is a direct usage of a number in the code. For example, if you have (in Java): public class Foo { public void setPassword(String password) { // don't do this if (password.length() > 7) { throw new InvalidArgumentException("password"); } } } This should be refactored to: public class Foo { public static final int MAX_PASSWORD_SIZE = 7; public void setPassword(String password) { if