What does * (int *) (buffer) mean?

The following is found in the C ++ code I am reading. Can someone help me understand what the following statements are doing?

char buffer[4096]; // some code int size = *(int*)(buffer); 
+6
source share
4 answers

TL DR: this code is bad, forget about it and move on.


(buffer) This bracket means that the programmer is not confident in his own programming abilities.

Since buffer is an array of characters, the buffer identifier itself gives you a pointer to the first element: a char pointer.

(int*) This is a cast, converting a char pointer to an int pointer.

* takes the contents of this integer pointer, and the result is stored in integer size .

Please note that this code is completely dangerous. Many pointer conversions cause bad behavior. There may be alignment problems. There may be problems with pointer aliases (Google’s strict anti-aliasing rule). This particular code also depends on the sequence, meaning that it requires that the contents of the character array have a given byte order.

In general, it makes no sense to use signed types, such as int or char (possibly signed), when performing such actions. In particular, the char type is very problematic since it has a signature defined by the application and should be avoided. Use unsigned char or uint8_t .

A slightly less bad code would look something like this:

 #include <stdint.h> uint8_t buffer[4096]; // some code uint32_t size = *(uint32_t*)buffer; 
+6
source
 char buffer[4096];//this is an array of 4096 characters // some code int size = *(int*)(buffer); 

Will cast a character (decaying) pointer, which buffer , to an integer pointer. Then he casts it to get an integer value . The integer value that you get from this will consist of the first four-digit values ​​of the buffer array, provided that the size of int is 4 bytes on your computer or will generally consist of sizeof(int) characters.

In other words, the memory representation of the first sizeof(int) characters of the buffer array will be processed as if they represented a single integer value, since now an integer pointer is pointed to it, and this will be stored in the size integer variable if this integer pointer is dereferenced.

At the same time, as was repeatedly indicated in the comments section, this code is unsafe. One thing that comes to mind is that some processors have strict alignment requirements (see this answer ), and in this case there is no guarantee that the address of the first element of the buffer array matches the alignment requirement of an integer, resulting in an undefined operation on these CPUs.

See @Lundin's answer for an even greater reason why this code is unsafe and might not give you the result you were looking for.

+14
source

Can someone help me understand what the following statements are doing?

First statement:

 char buffer[4096]; 

declares a 4096 chars array.

Second statement:

 int size = *(int*)(buffer); 

1. First, it takes a fading pointer character to a buffer array (also called buffer ), which is a pointer pointing to its first element set during its declaration

2. Then translates it into a pointer to int or int*

3. Finally, assigns the contents of this pointer (which will be of type int ) to the variable size .

+3
source

It takes the address buffer[0] , discards it to int* , dereferences, and uses the dereferenced value to initialize size . In other words, it takes the first sizeof(int) bytes of the buffer , pretends that these bytes are int , and stores this int value in size .

+1
source

All Articles