Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing variables in allocated memory

Tags:

c

pointers

Lets say I want to allocate memory for 3 integers:

int *pn = malloc(3 * sizeof(*pn));

Now to assign to them values I do:

pn[0] = 5550;
pn[1] = 11;
pn[2] = 70000;

To access 2nd value I do:

pn[1]

But the [n] operator is just a shortcut for *(a+n). Then it would mean that i access first byte after a index. But int is 4 bytes long so shoudn't i do

*(a+sizeof(*a)*n)

instead? How does it work?

like image 552
Michał Tabor Avatar asked Nov 26 '13 14:11

Michał Tabor


3 Answers

No, the compiler takes care of that. There are special rules in pointer arithmetic, and that is one of them.

If you really only want to increment it by one byte, you have to cast the pointer to a pointer to a type which is one byte long (for example char).

like image 171
Juri Robl Avatar answered Nov 16 '22 10:11

Juri Robl


Good question, but C will automatically multiply the offset by the size of the pointed-to type. In other words, when you access

p[n]

for a pointer declared as

T *p;

you will access the address p + (sizeof(T) * n) implicitly.

like image 31
creichen Avatar answered Nov 16 '22 08:11

creichen


For instance, we can use C99 standard to find out what is going on. According to C99 standard:

6.5.2.1 Array subscripting
Constraints
- 1 One of the expressions shall have type ‘‘pointer to object type’’, the other expression shall have integer type, and the result has type ‘‘type’’.
Semantics
- 2 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

And from 6.5.5.8 about conversion rules for + operator:

When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

Thus, all these notes are about your case and it works exactly as you wrote and you don't need special constructions, dereferencing or anything else (pointers arithmetic do that for you):

pn[1] => *((pn)+(1))

Or, in terms of byte pointers (to simplify description what is going on) this operation is similar to :

pn[1] => *(((char*)pn) + (1*sizeof(*pn)))

Moreover you can access this element with 1[pn] and result will be the same.

like image 1
Michael Avatar answered Nov 16 '22 09:11

Michael