-2

Third argument in memcmp should be of type sizeof(). I read this code with the following

memcmp(st.magic, "outpt_01",sizeof(st.magic)) == 0 && st.version == 0 )

where st is a struct.

What is the type of the third parameter in the call above? what is it doing ?

kiriloff
  • 25,609
  • 37
  • 148
  • 229
  • 3
    The type of the third argument is `std::size_t` (see e.g. [this `memcmp` reference](http://en.cppreference.com/w/cpp/string/byte/memcmp)), which is exactly what [`sizeof`](http://en.cppreference.com/w/cpp/language/sizeof) gives you. Could you please elaborate more on what problem you have? – Some programmer dude Aug 25 '14 at 08:19
  • 2
    Oh, and unless the size of `st.magic` is equal or smaller than 9 bytes, the `memcmp` call will read data out of bounds of the string literal, and you have [*undefined behavior*](http://en.wikipedia.org/wiki/Undefined_behavior). – Some programmer dude Aug 25 '14 at 08:22
  • 1
    For clarification, can you please show the *complete* statement where the call is in? Because right now you have more closing parentheses than opening. – Some programmer dude Aug 25 '14 at 08:30
  • If you don't have an IDE with builtin help, that's pretty well documented online. – molbdnilo Aug 25 '14 at 08:49

1 Answers1

1

What is the type of the third parameter in the call above?

size_t Number of bytes to compare.

what is it doing ?

if (sizeof(st.magic) == 0 && st.version == 0)
    memcmp(st.magic, "outpt_01", 1);
else
    memcmp(st.magic, "outpt_01", 0);

As pointed out by @JoachimPileborg, there is a typo in:

memcmp(st.magic, "outpt_01",sizeof(st.magic)) == 0 && st.version == 0 )

------------------------------------------------------------------------^ extra closing parenthesis

or it is part of a condition like (makes more sense):

if (memcmp(st.magic, "outpt_01",sizeof(st.magic)) == 0 && st.version == 0) {
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • No, that's not what's happening (unless the OP added an extra closing parenthesis by mistake). Instead it looks to be part of a condition, and checks if `memcmp` returns zero or not. – Some programmer dude Aug 25 '14 at 08:28