3

Pointers make the program more efficient and faster. But how do structures effect the efficiency of the program? Does it make it faster? Is it for just the readability of the code, or what? And may I have an example of how it does so?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
John
  • 73
  • 2
  • 8

4 Answers4

1

Pointers are just pointing a memory address, they have nothing to do with efficiency and speed(it is just a variable who stores some address which is required/helpful for some instruction to execute, nothing more) Yes but Data structures affect the efficiency of program/code in multiple ways.they can increase/decrease time complexity and space complexity of your algorithm and ultimately your code (in which you are implementing your algo.)

for example, let take example of array and linked list

Array : some amount of space allocated sequentially in memory

Linkedlist : some space allocated randomly in memory but connected via pointers

in all cases both can be used (assuming not much heavy space allocation). but as array is continuous allocation retrieval is faster than random allocation in linked list (every time get address of next allocated block and then fetch the data)

thus this improves speed/efficiency of your code

there are many such examples which will prove you why data sructures are more important (if they were not so important why new algorithms are designed and mostly why you are learning them)

Link to refer, What are the lesser known but useful data structures?

Community
  • 1
  • 1
Nachiket Kate
  • 8,473
  • 2
  • 27
  • 45
0

Structures have little to do with efficiency, they're used for abstraction. They allow you to keep all related data together, and refer to it by a single name.

There are some performance-related features, though. If you have all your data in a structure, you can pass a pointer to that structure as one argument to a function. This is better than passing lots of separate arguments to the function, for each value that would have been a member of the structure. But this isn't the primary reason we use structures, it's mainly an added benefit.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • So going by programmers logic how and when should I use structures? – John Feb 03 '14 at 05:32
  • 1
    Whenever you have related data that should be used together. Any decent programming textbook should cover this. – Barmar Feb 03 '14 at 05:33
  • Even in seperate functions i'm more then capable of using them like that as if they were global variables? – John Feb 03 '14 at 05:34
  • Yes. For instance, it's common to make arrays of structures. The alternative would be to have a separate array for each member. – Barmar Feb 03 '14 at 05:38
  • This is all part of data abstraction, which should be covered in any decent programming course or texbook. – Barmar Feb 03 '14 at 05:38
  • structure is used to group related data, your code is not just for your understanding but its readability and its extensibility not just by you but by other programmers too matters the most, so by structures we can have a good understandable code written and its good practice... – Trilok M Feb 03 '14 at 05:38
  • Thank you guys. I do appreciate your help more then you know. One last thing.. What exactly does structures do behind the scenes? Is it internally an array itself, or how does C make a structure. For example an pointer points to the memory address and copies a value behind the scenes. Is a structure an array which holds variables and their values? Thanks -john – John Feb 03 '14 at 05:44
  • Yes, it's like a little array, and each member name refers to a position in the array where the first byte of that member is. – Barmar Feb 03 '14 at 05:49
0

Pointers do not contribute anything in program's efficiency and execution time/speed. Structure provides a way of storing different variables under the same name. These variables can be of different types, and each has a name which is used to select it from the structure. For example, if you want to store data about student, it may consist of Student_Id, Name, Sex, School, Address etc. where Student_Id is int, Name is string, Sex is char (M/F) etc. but all variables are grouped together as a single structure 'Student' at a 'single block of memory'. So everytime you need a student data to fetch or update, you need to deal with structured data only. Now imagine, how much problem you may face if you try to store all those int, char, char[] variables separately and update them individually. Because you need to update everything at different memory locations for each student's record.

But if you consider data structure to structure your whole data in abstract data types where you may go for different kind of linked list, tree, graph etc. or array implementation then your algorithm plays a vital role in deciding time and space complexity of the program. So in that sense you can make your program more efficient.

arin1405
  • 677
  • 1
  • 7
  • 18
-1

When you want to optimize your memory/cache usage structures can increase the efficiency of your code (make it faster). This is because when data is loaded from memory to the cache it done in words (32-64bits) by fitting you data to these word boundaries you can ensure when your first int is loaded so is your second for a two int structure (maybe a serious of coordinates).

clancer
  • 613
  • 4
  • 10