# Memory access

3

Can someone tell me what the following lines do?

``````num = *((int*)(direccion+sizeof(char)));
(*(ST_DATA*)(direccion+posTabla*sizeof(ST_DATA))).speed = num;
``````

Do the next two lines mean the same thing?

``````*((int*)(direccion + OFF_TABLE_KEY+sizeof(int))) = sec2;
((int*)(direccion + OFF_TABLE_KEY))[1] = sec2
``````

asked by Gloria 24.05.2018 в 12:01
source

9

Can someone tell me what the following lines do?

On the one hand we can assume that the typical value returned by `sizeof(char)` is 1. So assuming that `direccion` is going to be a pointer:

``````char* direccion;
``````

Then we have this:

``````direccion+sizeof(char)
``````

It is arithmetic of pointers and what it does is move a position with respect to the direction pointed to by `dirección` :

``````direccion
v
0x100 0x101 0x102
^
direccion+sizeof(char)
``````

Another way to put it is as follows:

``````&direccion[sizeof(char)]
``````

Note the% initial% co to retrieve the memory location.

What surrounds it is nothing more than a conversion of type:

``````(int*)(direccion+sizeof(char))
^^^^
Conversion de char* a int*
``````

And finally we get the value pointed by the pointer. What happens is that since the pointer is now type `&` , we will recover 4 bytes instead of only 1

``````*((int*)(direccion+sizeof(char)))
^
``````

The equivalent in independent operations would be:

``````char* ptr = direcciones+sizeof(char);
int* intPtr = (int*)ptr;
int num = *intPtr;
``````

And the second line is equivalent but with another type of data.

Do the next two lines mean the same thing?

source
7

The next two lines mean the same thing?

``````*((int*)(direccion + OFF_TABLE_KEY+sizeof(int))) = sec2;
((int*)(direccion + OFF_TABLE_KEY))[1] = sec2
``````

The syntax `puntero[entero]` equals `*(puntero + entero)` , and the pointer arithmetic causes that `entero` to be computed as `entero*sizeof(tipo)` , where `tipo` is pointed by the pointer.

In the previous case, `puntero` would be the expression `((int*)(direccion+OFF_TABLE_KEY))` . Even without knowing the type pointed to by `direccion` , the result of `direccion+OFF_TABLE_KEY` will return another pointer (to an unknown type), but since the casting then forces the interpretation of that pointer as `int*` , then putting a `[1]` after is equivalent to adding the size of an integer to the result.

Therefore, yes , are equivalent, provided that `direccion` is a pointer to a type that occupies 1 byte (ex: `char*` ). Otherwise the address to which the first expression would access would be different, since the compiler would still be multiplying `sizeof(int)` by the size of the type pointed to by `direccion` , because the complete expression `(direccion + OFF_TABLE_KEY+sizeof(int))` is evaluated using pointer arithmetic .

In the absence of information on the type of `direccion` , it can not be guaranteed that they are equivalent, but I can assure you that the following two expressions would be equivalent (by forcing the type of `direccion` to `char*` ):

``````*((int*)((char*)direccion + OFF_TABLE_KEY+sizeof(int))) = sec2;
((int*)((char*)direccion + OFF_TABLE_KEY))[1] = sec2
``````

Can someone tell me what the following lines do?

``````num = *((int*)(direccion+sizeof(char)));
(*(ST_DATA*)(direccion+posTabla*sizeof(ST_DATA))).speed = num;
``````

The first one has already explained it to you perfectly @eferion . As for the second, again I will make the hypothesis that `direccion` is a pointer to `char` , otherwise the expression would not make much sense.

The expression is understood if we assume that `direccion` has been declared as pointer to `char` , but at this moment we have it pointing to a memory area where there is an array of structures, each of type `ST_DATA` . What that expression wants is to access one of those structures (the one that is in%% of%) to save something in its `posTabla` field.

If `speed` were a pointer to structures, the thing would be as simple as:

``````direccion[posTabla].speed = num;
``````

but the fact that `direccion` is a pointer to `direccion` forces us to make certain juggling with pointers and castings. If we "force" the compiler to see `char` as a pointer to `direccion` , then we can use a syntax very similar to the previous one:

``````((ST_DATA*)direccion)[posTabla].speed = num;
``````

And the line that you have copied is just another (a bit more twisted) way of doing the same thing, doing in the explicit expression the arithmetic of pointers that in the expression that I have set the compiler would implicitly solve .

One more way to say the same thing would be:

``````(ST_DATA*)(direccion+posTabla*sizeof(ST_DATA))->speed = num;
``````

in this case if dereferencing the pointer obtained by the expression, but using that pointer to access a member of the structure. ( `ST_DATA` is equivalent to `p->a` )