-1

what does this line do? I don't understand its syntax, the buffer is the name of a dynamic variable. buffer type char

(unsigned int*)buffer

Marat
  • 11
  • 3
  • 3
    It's a type cast: http://www.cplusplus.com/doc/tutorial/typecasting/#type_casting – Andrey Semashev Feb 13 '20 at 17:58
  • 1
    This should be of use: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Feb 13 '20 at 17:59
  • 1
    We cannot tell without context, but using this is more often than not causing undefined behavior. In particular the `buffer` may be too weakly aligned or a later access through the cast pointer ma be an aliasing violation. In addition to that C-style casts should not be used in C++ because they can do dangerous things like casting away `const`ness without warning. – walnut Feb 13 '20 at 19:12
  • @walnut: Upped your comment although I do disagree with the final sentence: the behaviour of the c-style cast is well-documented in C++ and should be committed to memory. – Bathsheba Feb 13 '20 at 19:40
  • 1
    @Bathsheba Yes, it should be committed to memory, because of how often it is encountered, but I think the more specific C++ casts are practically always expressing intend better and limiting the possible scope of mistakes. I think the only place where a C style cast would be *required* is casting a pointer to an inaccessible base class. – walnut Feb 13 '20 at 19:55
  • @walnut: Interesting. In my codes base I allow it but I expect all programmers to know the rules. Personally I find the c style cast easier on the eye. – Bathsheba Feb 13 '20 at 19:57
  • @Bathsheba [Some](https://softwareengineering.stackexchange.com/questions/50442/c-style-casts-or-c-style-casts) see that as plus for the C++ casts ;D. But I shouldn't have stated it as strongly as I did. Probably something like "avoid if possible, especially as a beginner and when pointers or reference types are involved" would have been less opinionated. Complete avoidance isn't possible anyway given the functional cast syntax. – walnut Feb 13 '20 at 20:29

2 Answers2

6

It's a C-style cast to the type unsigned int *.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • 3
    Addendum: A C-Style cast turns off a number of the compiler's defenses, so using a cast like this can blow up in in your face in strange and interesting ways. You can always cast to a `char *` and view an object as an array of bytes, but the reverse is not true. Two common problems converting a bytes to integers are 1) The alignment of `buffer` could prevent interpretation as an `int` (typically causes a crash or a performance penalty), and 2) the byte ordering of the data in `buffer` could be different than the ordering used by integers on the system interpreting `buffer`. – user4581301 Feb 13 '20 at 18:50
1
unsigned int *

This is a pointer to an unsigned int in C. Casting is used when you have a variable of a different type and you want it to be a different type. A pointer is a variable that holds the address of a variable. So, casting buffer as an unsigned int * tells the computer to interpret buffer (a pointer of 1 byte) as a pointer that points to memory of the size unsigned int (4 bytes).

cwbusacker
  • 507
  • 2
  • 12