Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-15-2008, 01:17 PM
the_code_charmer the_code_charmer is offline
Learning Programmer
 
Join Date: Jan 2008
Posts: 34
Rep Power: 4
the_code_charmer is on a distinguished road
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-15-2008, 02:12 PM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 507
Last Blog:
Just over the next hil...
Rep Power: 10
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default

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.//
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-15-2008, 07:51 PM
the_code_charmer the_code_charmer is offline
Learning Programmer
 
Join Date: Jan 2008
Posts: 34
Rep Power: 4
the_code_charmer is on a distinguished road
Default

i was looking for something more along the lines of performance with searching, adding and deleting.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-16-2008, 06:39 AM
G_Morgan G_Morgan is offline
Guru
 
Join Date: Oct 2007
Age: 24
Posts: 507
Last Blog:
Just over the next hil...
Rep Power: 10
G_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura aboutG_Morgan has a spectacular aura about
Default

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.//
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-16-2008, 12:00 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Arrays Fedex C and C++ 3 12-02-2007 05:45 PM
Derived Classes Can't Set Function Pointers dargueta C and C++ 11 11-05-2007 10:04 PM
Issue writing to file: pointer to a class which contains pointers to other classes Sheemer C and C++ 0 08-21-2007 02:17 AM
Arrays clookid PHP Tutorials 1 01-11-2007 09:30 PM
Arrays Sionofdarkness C and C++ 5 07-26-2006 06:35 PM


All times are GMT -5. The time now is 11:33 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads