-3

I was trying to understand this function. Will the following function return the sum of ASCII values of characters in the string url?

int map(char* url) {
    int key;
    memcpy(key, url, sizeof(int));
    return key;
}
tims
  • 1
  • 3
  • what are you trying to achieve? Incrementing letters and/or numbers? You can do that via statements like `char c = 'A'; c++` and the likes ... mind you : once you increment past 0x126 you will start with 0x0 *(NUL)* again - and there are quite a few non-printable characters until you get to the printable ones ... – specializt Dec 20 '16 at 06:41
  • 1
    Can you explain what you want the function to do? – 599644 Dec 20 '16 at 06:41
  • 1
    No, it will return the first character times 2^(8*3) plus the second character times 2^(8*2) plus the third character times 2^(8*1) plus the fourth character times 2^(8*0), assuming `CHAR_BIT == 8` and `sizeof int == 4` and `strlen(url) >= 4`. Another assumption of mine is that your program runs on Big-Endian architecture. If it runs on Little-Endian architecture, then the result will be "opposite" of what I mentioned. Therefore, in short, your code is platform-dependent (because of the endian-ness issue, the `sizeof int` issue and the `CHAR_BIT` issue). – barak manos Dec 20 '16 at 06:44
  • 1
    The signature of `memcpy` is `void *memcpy(void *dest, const void *src, size_t n);`. Wouldn't the OP's code cause UB? – babon Dec 20 '16 at 06:46
  • What makes you think it would return the sum of ASCII values of characters in the string url? – Jabberwocky Dec 20 '16 at 06:48
  • In order to avoid memory-access violations, make sure that `strlen(url) >= 4` before calling `memcpy`. – barak manos Dec 20 '16 at 06:52

2 Answers2

1

No. I will simply copy bytes from url into key. Enough to fill it.
Now the first sizeof(int) bytes of url can be reinterpreted as an integer.

It seems like a very basic way to generate numeric keys from strings.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • As long as you don't mind all the keys being the same (for typical web URLs). – Michael Geary Dec 20 '16 at 06:38
  • @MichaelGeary - If your purpose is to separate URLs into numeric values that represent the protocol used, I don't see why you'd mind :) – StoryTeller - Unslander Monica Dec 20 '16 at 06:41
  • 1
    Now that sounds real useful. And of course `http` and `https` count as one protocol. ;-) – Michael Geary Dec 20 '16 at 06:41
  • @MichaelGeary - You assume `sizeof(int) == 4`. For shame. – StoryTeller - Unslander Monica Dec 20 '16 at 07:02
  • You got me there. I am duly shamed. On a 64 bit machine where the compiler uses `sizeof int == 8` (if we can find one of those), for an `http` protocol we'll pick up the first character of the domain as part of our numeric key, because `http://` is only seven characters. This provides an appealing bit of uncertainty, much in the same way that a human drummer's deliberate changes and slight imperfections are more listenable than a robotic drummer. – Michael Geary Dec 20 '16 at 07:12
  • So if it copies first 4 bytes into the key, the key will be same in every case, that is, the value of "http". Is that correct? – tims Dec 20 '16 at 07:15
  • @tims - It copies the first `sizeo(int)` bytes into key (assuming the actual code is `memcpy(&key, url, sizeof(int));` and not as you've written. The function in your post is woefully unsafe in many regards. – StoryTeller - Unslander Monica Dec 20 '16 at 07:17
  • Yes I know. I did not right this function, just trying to understand if it can be used as a good hash function for web URLs or not. Because if it just copies first few characters, it is useless as a hash function. – tims Dec 20 '16 at 07:20
  • @tims: It is a terrible function in every way. It shouldn't even compile because of the type mismatch on the first argument to `memcpy()`, and if it does compile it won't do anything useful. It would just copy the first 2, 4, or 8 characters from the URL, depending on value of `sizeof int`. It certainly does nothing resembling a hash function. Why don't you search for [C hash function](https://www.google.com/search?q=c+hash+function) to get some ideas? – Michael Geary Dec 20 '16 at 07:21
  • Yes. Thanks. But can I change this in any way to make it a good hash function? – tims Dec 20 '16 at 07:23
  • @tims - Yes. Throw away `memcpy` and lookup a decent hash function on strings online. Then implement it. – StoryTeller - Unslander Monica Dec 20 '16 at 07:24
  • I can very well do that with Horner's rule then. Just wanted to use memcpy(). But I guess I will have to use Horner's rule now. Thanks anyways :) – tims Dec 20 '16 at 07:27
  • @tims - You can use `memcpy` still, if you really want. Extract all the bytes of the string in groups of `sizeof(unsigned int)`. And bitwise XOR them all together into a single key. Perhaps adding a salt to improve the distribution. – StoryTeller - Unslander Monica Dec 20 '16 at 07:28
  • Thanks a lot :) :) – tims Dec 20 '16 at 07:37
  • Honestly, @tims, I recommend that at this point you _read_ some code for hash functions, instead of trying to write one. Use the search link I gave you a few comments above and start reading the code you find. You will learn a lot about the topic. – Michael Geary Dec 20 '16 at 07:39
  • For example, here is a [djb2 implementation](http://stackoverflow.com/a/7666577/1202830) that is very short and simple and easy to understand. I recommend stepping through this function in a debugger to see how it operates. – Michael Geary Dec 20 '16 at 07:42
  • @MichaelGeary I will. Thanks :) – tims Dec 20 '16 at 07:44
-1

No, It won't return the sum, it will copy ASCII values of 2 characters of url to key if size of int is 2, otherwise it will copy ASCII values of 4 characters of url.

Sankaran
  • 21
  • 3