Thread: Pointers+arrays

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    51

    Pointers+arrays

    I figured out how I can access my array but I have to manually increase the pointer to loop thru the entire array. I'm just wondering is this a good programming practice? or is there a better way to do this.

    Ex.

    int x[10];
    int ptr_x;
    ptr_x=x;
    now if I want to use the pointer to change lets say x[1] I would have to add 1 to ptr_x;

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    it's called pointer arithmetic, but:

    int *ptr_x;

    it's perfectly acceptable to do traversals like that.
    Last edited by robwhit; 09-09-2007 at 12:04 AM.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why use a pointer?
    Code:
    int x[10];
    ...
    for ( int i = 0; i < 10; ++i )
    {
        cout << x[ i ] << endl;
    }

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    I can't use global variables and I need to use like 5 different functions.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why can't you pass the array to the functions?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by cpjust View Post
    Why use a pointer?
    they're faster. using an index means you have to *(arr + i), but using a pointer, it's just *ptr. You'd want to use pointers in inner loops of often-called code, for example.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    they're faster. using an index means you have to *(arr + i), but using a pointer, it's just *ptr.
    Shouldn't a good compiler optimize it down to that anyways?

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You'd want to use pointers in inner loops of often-called code, for example.
    Only if you have proved your point with profiler.
    Otherwise you want everywhere to use code that is simplier to read
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unless you are JUST doing a loop for the purpose of walking through an array without actually doing anything with the array, I would say that there is no measurable difference between the two - if the array is huge, cache-misses will be more noticably than the addition of a pointer and the index. On x86 it is essentially just a more complex addressing mode to get there, which won't affect the time the processor takes to get there, only the size of the code will be different.

    Of course, any modern compiler will understand these type of references, and optimize it.

    There are situations where a pointer is more efficient, but for most purposes, if the code is easy to read, that is more important.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    39

    Post

    Pointer is better than Array , i hv a example

    A array example:-
    Code:
    int  x[10];
    int ans = x[4] + x[5];
    if we see in low level...... than we known that, there is a lots of step before adding these two value , first step to calculate the address of x[4] and How its calculate, look , first of all x array hv only hold its first element's address , and formulae of calculate addressing is { x's address + (type size)*(index no) } .

    in array example , complier calculate more .

    A Pointer Example:-
    Code:
    int x[10];
    int *ptr = &x[4];
    int ans = *ptr + *(ptr+1);
    in this example complier calcute the address only one time, b/c ptr already hv the address of 4th index and if we add in address the we hv 5th index.

    thats why pointer is better then array.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You have to show the assembly result of the compilation with the optimization turned on to prove your statement...

    Otherwise its just a guess... And I belive - you guessed wrong.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Pointer is better than Array , i hv a example
    I compared the assembly output of the MinGW port of g++ 3.4.2 with these two sample programs:
    Code:
    // Sample Program A
    int main()
    {
        int x[10] = {0};
        int ans = x[4] + x[5];
    }
    Code:
    // Sample Program B
    int main()
    {
        int x[10] = {0};
        int *ptr = &x[4];
        int ans = *ptr + *(ptr+1);
    }
    Lo and behold, their assembly output with the -O1 optimisation flag was the same... and again for the -O2 flag!

    (I only checked with -O1 and -O2.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by obaid View Post
    Pointer is better than Array , i hv a example

    A array example:-
    Code:
    int  x[10];
    int ans = x[4] + x[5];
    Compiler may do something like this:
    Code:
        lea  ebx, x
        mov eax, [ebx + 4 *4]
        add eax, [ebx + 5 * 4]
        mov ans, eax
    Note that those 4 * 4 and 5 * 4 are constants that the compiler will calculate during compilation.

    if we see in low level...... than we known that, there is a lots of step before adding these two value , first step to calculate the address of x[4] and How its calculate, look , first of all x array hv only hold its first element's address , and formulae of calculate addressing is { x's address + (type size)*(index no) } .

    in array example , complier calculate more .

    A Pointer Example:-
    Code:
    int x[10];
    int *ptr = &x[4];
    int ans = *ptr + *(ptr+1);
    Code:
       lea  ebx, x
       lea  ebx, [ebx + 4 *4]
       mov eax, [ebx]
       add eax, [ebx+1 * 4]
       mov eax, ans
    I don't really see how this is more efficient. Did you have a different processor architecture in mind?

    in this example complier calcute the address only one time, b/c ptr already hv the address of 4th index and if we add in address the we hv 5th index.

    thats why pointer is better then array.
    If the compiler can figure out how to convert array indexes into pointer references, then it can do that for both of the above cases, so there's really no difference. I have not seen a compiler that does badly at this sort of things for the last several years, at least not GCC or Microsoft versions. If you use a different compiler, then I suggest you take a look at the code the compiler generates, and if there's really different results, then you probably want to look at what other compilers you can use.

    --
    Mats
    Last edited by matsp; 09-10-2007 at 08:28 AM. Reason: Can't do quote/unquote without messing up :-)
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    39

    Red face

    Sir,
    its not a assembly language problem , its a memory adressing management problem which hv to face the complier. okz

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    its not a assembly language problem , its a memory adressing management problem which hv to face the complier. okz
    If you are trying to say that accessing by pointers is better than accessing by array indices because the compiler has to do more work in optimisation for the latter, then I think you are programming in the wrong language for your taste.

    If not, how can your example possibly be valid when even at machine level both programs do exactly the same thing?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 10:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM