Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why its not recommended to use pointer for array access in C

Tags:

I'm learning C programming and I cam across this tutorial online, which state that you should always prefer using [] operator over pointer arithmetic as much as possible.

https://www.cs.swarthmore.edu/~newhall/unixhelp/C_arrays.html#dynamic

you can use pointer arithmetic (but in general don't)

consider the following code in C

int    *p_array;
p_array = (int *)malloc(sizeof(int)*50);

for(i=0; i < 50; i++) {
  p_array[i] = 0;
}

What is the difference in doing it using pointer arithmetic like the following code (and why its not recommended)?

int    *p_array;
p_array = (int *)malloc(sizeof(int)*50);      // allocate 50 ints

int *dptr = p_array;

for(i=0; i < 50; i++) {
  *dptr = 0;
  dptr++;
}

What are the cases where using pointer arithmetic can cause issues in the software? is it bad practice or is it inexperienced engineer can be not paying attention?

like image 250
ROOT Avatar asked Jan 11 '20 07:01

ROOT


People also ask

Can a pointer access the array in C?

Access Array Elements Using Pointers Then, the elements of the array are accessed using the pointer notation. By the way, data[0] is equivalent to *data and &data[0] is equivalent to data. data[1] is equivalent to *(data + 1) and &data[1] is equivalent to data + 1.

Can we use pointers for accessing array elements?

we can access the array elements using pointers.

Can I use a pointer as an array?

Given a starting address a , compute the address of the i 'th object of the pointed-to type (not the i 'th byte) following that address and dereference the result. So the upshot of that is yes, you can use the [] subscript operator on a pointer expression as well as an array expression.

Which is better to use pointer or array?

Difference Between Arrays and Pointers in C/C++ The pointer can be used to access the array elements, accessing the whole array using pointer arithmetic, makes the accessing faster. The main difference between Array and Pointers is the fixed size of the memory block.


1 Answers

Since there seems to be all out confusion on this:

In the old days, we had 16bit CPU's think 8088, 268 etc. To formulate an address you had to load your segment register (16 bit register) and your address register. if accessing an array, you could load your array base into the segment register and the address register would be the index. C compilers for these platforms did exist but pointer arithmetic involved checking the address for overruns and bumping the segment register if necessary (inefficient) Flat addressed pointers simply weren't possible in hardware.

Fast forward to the 80386 Now we have a full 32 bit space. Hardware pointers are possible Index + base addressing incurs a 1 clock cycle penalty. The segments are also 32 bit though, so arrays can be loaded using segments avoiding this penalty even if you are running 32 bit mode. The 368 also increases the number of segment registers by 2. (No idea why Intel thought that was a good idea) There was still a lot of 16bit code around though

These days, segment registers are disabled in 64 bit mode, Base+Index addressing is free.

Is there any platform where a flat pointer can outperform array addressing in hardware ? Well yes. the Motorola 68000 released in 1979 has a flat 32 bit address space, no segments and the Base + Index addressing mode incurs an 8 clock cycle penalty over immediate addressing. So if you're programming a early 80's era Sun station, Apple Lisa etc. this might be relevant.

In short. If you want an array, use an array. If you want a pointer use a pointer. Don't try and out smart your compiler. Convoluted code to turn arrays into pointers is exceedingly unlikely to provide any benefit, and may be slower.

like image 129
camelccc Avatar answered Sep 20 '22 05:09

camelccc