简体   繁体   中英

Pointer math vs. Array index

I know this has been hashed over a number of time, but I have come across a case today that shook my understanding of the pointer math/ array index.

As I have allways understood it, &mybuff[10] and (&mybuff+10) are equivilent ways of referancing the same memory.

However I spent the morning fighting a case where:

memcpy(&mybuff+10,&in,8); 

overflowed the buffer, when compiled with optimization on and worked just fine when compiled for debugging.

While athe same time,

memcpy(&mybuff[10],&in,8); 

worked just fine in both cases.

Many thanks for any ideas or pointers.

see http://cplusplus.com/doc/tutorial/operators/ for priority order of operators
&mybuff+10 like (&mybuff)+10
&mybuff[10] like &(mybuff[10])

Edit
also some pointers http://cplusplus.com/doc/tutorial/pointers/

I'll invent a declaration for mybuff for the sake of the example:

char mybuff[123];

Now, &mybuff+10 does pointer arithmic on &mybuff , which has the type "pointer to array of 123 chars". This is different from plain mybuff which (after pointer decay) has type "pointer to char". The bit value of these two expressions is the same, but because they are pointers to things of different sizes, they behave differently under pointer arithmetic.

&mybuff+10 means that you want to step past ten of the 123-char arrays in the type (which is meaningless given the declaration and may segfault), whereas mybuff+10 just says that you want to step past ten individual chars.

&mybuff[10] is equivalent to &mybuff[0] + 10 which is equivalent to mybuff + 10

Array indexing is defined in terms of pointer arithmetic. p[i] means *(p+i) (I'm ignoring the need for extra parentheses in case p or i is a more complex expression), where p is a pointer value and i is an integer value.

Trivia: Since addition, even pointer+integer addition, is commutative, p[i] can also be written as i[p] . Yes, 4["Hello"] == 'o' . For the sake of anyone who reads your code in the future, please do not make use of this knowledge.

An excellent reference on the relationship between arrays and pointer in C (and C++, where the rules are nearly identical) is section 6 of the comp.lang.c FAQ . (I could have linked directly to section 6, but I like to encourage people to browse it; the whole thing is worth reading.)

I think you've got some pointer problems.
If mybuff is a pointer then mybuff + 10 == &(mybuff[10]) .
That's not the same as &mybuff + 10 like you've got there.

(&mybuff+10)&mybuff[10] 相同,但是&mybuff[10]mybuff + 10都是。

The correct syntax is mybuff+10 not &mybuff+10 which says move to position 10 in your array and copy 8 bytes (per your memcpy statement). It is still unknown whether you can actually hold an additional 8 bytes though.

The reason it worked in debug mode is because in debug mode, the memory is initialized for you and allocations are also larger than the actual size. http://msdn.microsoft.com/en-us/library/bebs9zyz(v=vs.80).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM