Closed Thread
Results 1 to 5 of 5

Thread: pointers....or arrays

  1. #1
    the_code_charmer is offline Learning Programmer
    Join Date
    Jan 2008
    Posts
    68
    Rep Power
    0

    pointers....or arrays

    Good day to all

    What kind of tradeoffs am i looking at when using either an array or a pointer in a data structure?

    Thanks for your time

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21
    An array is a vector of values. A pointer is something that refers to a different part of memory (usually dynamically allocated memory but not always). They are totally different things in concept. As it is, arrays are implemented as pointers in C (the pointer is to the first array element). There are no performance benefits to using one over the other because the one is a special case of the other in C (but not necessarily other languages).

    Personally I think you can have pointer arithmetic, or good code but never bother at the same time. There is zero reason to use pointer arithmetic, not performance and certainly not readability.

    If you doubt this then take a modern compiler, write the same piece of code twice but once using pointer arithmetic and the other using array notation. Then compile them to ASM and compare the output. If the outputs are different at all then get a better compiler and then use array notation.

    //edit - if of course you weren't looking for a comparison of array index notation to array pointer notation then ignore all this.//

  4. #3
    the_code_charmer is offline Learning Programmer
    Join Date
    Jan 2008
    Posts
    68
    Rep Power
    0
    i was looking for something more along the lines of performance with searching, adding and deleting.

  5. #4
    Join Date
    Oct 2007
    Posts
    538
    Rep Power
    21
    Well you can't add or delete from an array (it has constant size) and a pointer is literally just an address that points to another entity (so search time is constant since a pointer only every points to one thing).

    Search time for an array depends on the data. If it is sorted it can be done quickly via a binary search. Otherwise the worse case is linear performance for a linear search (i.e. search time is proportional to the length of the array on average).

    If you want fast add, delete and search you are better off with a hash table.

    //edit - naturally you will have to find a C implementation of a hash table first.//

  6. #5
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143
    Generally, it will depend highly on what you're planning to do. If you're working in C++ then I would use a vector instead of an array, and save pointers for linking data together in things like nodes in linked lists.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate Pointers and Arrays in C
    By fayyazlodhi in forum C Tutorials
    Replies: 22
    Last Post: 06-29-2011, 11:21 AM
  2. Array of pointers to int arrays
    By DMK741 in forum C and C++
    Replies: 27
    Last Post: 01-13-2011, 01:35 AM
  3. A bug, pointers to arrays
    By AdvMutant in forum C and C++
    Replies: 5
    Last Post: 12-18-2010, 09:58 AM
  4. Using pointers to pointers to arrays as parameters
    By ThemePark in forum C and C++
    Replies: 6
    Last Post: 02-05-2010, 05:21 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts