Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 66551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:07:16+00:00 2026-05-10T19:07:16+00:00

I keep reading that, in C, using pointer arithmetic is generally faster than subscripting

  • 0

I keep reading that, in C, using pointer arithmetic is generally faster than subscripting for array access. Is this true even with modern (supposedly-optimizing) compilers?

If so, is this still the case as I begin to move away from learning C into Objective-C and Cocoa on Macs?

Which is the preferred coding style for array access, in both C and Objective-C? Which is considered (by professionals of their respective languages) more legible, more ‘correct’ (for lack of a better term)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:07:17+00:00Added an answer on May 10, 2026 at 7:07 pm

    You need to understand the reason behind this claim. Have you ever questioned yourself why it is faster? Let’s compare some code:

    int i; int a[20];  // Init all values to zero memset(a, 0, sizeof(a)); for (i = 0; i < 20; i++) {     printf("Value of %d is %d\n", i, a[i]); } 

    They are all zero, what a surprise 😛 The question is, what means a[i] actually in low level machine code? It means

    1. Take the address of a in memory.

    2. Add i times the size of a single item of a to that address (int usually is four bytes).

    3. Fetch the value from that address.

    So each time you fetch a value from a, the base address of a is added to the result of the multiplication of i by four. If you just dereference a pointer, step 1. and 2. don’t need to be performed, only step 3.

    Consider the code below.

    int i; int a[20]; int * b;  memset(a, 0, sizeof(a)); b = a; for (i = 0; i < 20; i++) {     printf("Value of %d is %d\n", i, *b);     b++; } 

    This code might be faster… but even if it is, the difference is tiny. Why might it be faster? "*b" is the same as step 3. of above. However, "b++" is not the same as step 1. and step 2. "b++" will increase the pointer by 4.

    (important for newbies: running ++ on a pointer will not increase the pointer one byte in memory! It will increase the pointer by as many bytes in memory as the data it points to is in size. It points to an int and the int is four bytes on my machine, so b++ increases b by four!)

    Okay, but why might it be faster? Because adding four to a pointer is faster than multiplying i by four and adding that to a pointer. You have an addition in either case, but in the second one, you have no multiplication (you avoid the CPU time needed for one multiplication). Considering the speed of modern CPUs, even if the array was 1 mio elements, I wonder if you could really benchmark a difference, though.

    That a modern compiler can optimize either one to be equally fast is something you can check by looking at the assembly output it produces. You do so by passing the "-S" option (capital S) to GCC.

    Here’s the code of first C code (optimization level -Os has been used, which means optimize for code size and speed, but don’t do speed optimizations that will increase code size noticeably, unlike -O2 and much unlike -O3):

    _main:     pushl   %ebp     movl    %esp, %ebp     pushl   %edi     pushl   %esi     pushl   %ebx     subl    $108, %esp     call    ___i686.get_pc_thunk.bx "L00000000001$pb":     leal    -104(%ebp), %eax     movl    $80, 8(%esp)     movl    $0, 4(%esp)     movl    %eax, (%esp)     call    L_memset$stub     xorl    %esi, %esi     leal    LC0-"L00000000001$pb"(%ebx), %edi L2:     movl    -104(%ebp,%esi,4), %eax     movl    %eax, 8(%esp)     movl    %esi, 4(%esp)     movl    %edi, (%esp)     call    L_printf$stub     addl    $1, %esi     cmpl    $20, %esi     jne L2     addl    $108, %esp     popl    %ebx     popl    %esi     popl    %edi     popl    %ebp     ret 

    Same with the second code:

    _main:     pushl   %ebp     movl    %esp, %ebp     pushl   %edi     pushl   %esi     pushl   %ebx     subl    $124, %esp     call    ___i686.get_pc_thunk.bx "L00000000001$pb":     leal    -104(%ebp), %eax     movl    %eax, -108(%ebp)     movl    $80, 8(%esp)     movl    $0, 4(%esp)     movl    %eax, (%esp)     call    L_memset$stub     xorl    %esi, %esi     leal    LC0-"L00000000001$pb"(%ebx), %edi L2:     movl    -108(%ebp), %edx     movl    (%edx,%esi,4), %eax     movl    %eax, 8(%esp)     movl    %esi, 4(%esp)     movl    %edi, (%esp)     call    L_printf$stub     addl    $1, %esi     cmpl    $20, %esi     jne L2     addl    $124, %esp     popl    %ebx     popl    %esi     popl    %edi     popl    %ebp     ret 

    Well, it’s different, that’s for sure. The 104 and 108 number difference comes of the variable b (in the first code there was one variable less on stack, now we have one more, changing stack addresses). The real code difference in the for loop is

    movl    -104(%ebp,%esi,4), %eax 

    compared to

    movl    -108(%ebp), %edx movl    (%edx,%esi,4), %eax 

    Actually to me it rather looks like the first approach is faster(!), since it issues one CPU machine code to perform all the work (the CPU does it all for us), instead of having two machine codes. On the other hand, the two assembly commands below might have a lower runtime altogether than the one above.

    As a closing word, I’d say depending on your compiler and the CPU capabilities (what commands CPUs offer to access memory in what way), the result might be either way. Either one might be faster/slower. You cannot say for sure unless you limit yourself exactly to one compiler (meaning also one version) and one specific CPU. As CPUs can do more and more in a single assembly command (ages ago, a compiler really had to manually fetch the address, multiply i by four and add both together before fetching the value), statements that used to be an absolute truth ages ago are nowadays more and more questionable. Also who knows how CPUs work internally? Above I compare one assembly instructions to two other ones.

    I can see that the number of instructions is different and the time such an instruction needs can be different as well. Also how much memory these instructions needs in their machine presentation (they need to be transferred from memory to CPU cache after all) is different. However modern CPUs don’t execute instructions the way you feed them. They split big instructions (often referred to as CISC) into small sub-instructions (often referred to as RISC), which also allows them to better optimize program flow for speed internally. In fact, the first, single instruction and the two other instructions below might result in the same set of sub-instructions, in which case there is no measurable speed difference whatsoever.

    Regarding Objective-C, it is just C with extensions. So everything that holds true for C will hold true for Objective-C as well in terms of pointers and arrays. If you use Objects on the other hand (for example, an NSArray or NSMutableArray), this is a completely different beast. However in that case you must access these arrays with methods anyway, there is no pointer/array access to choose from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You'll have to strip the "0x" part, but this snippet… May 11, 2026 at 11:26 pm
  • Editorial Team
    Editorial Team added an answer Design work, I find, flows much easier than just coding.… May 11, 2026 at 11:26 pm
  • Editorial Team
    Editorial Team added an answer This is a bit of a hack, but it appears… May 11, 2026 at 11:26 pm

Related Questions

C++ is all about memory ownership - aka ownership semantics . It is the
I will begin to use C for an Operating Systems course soon and I'm
I'm a programmer with 2 years experience, I worked in 4 places and I
Alright. So I wanted to use a file written in c in c++. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.