Tuesday, July 14, 2009

Is it true that pointers in C increase execution speed of the program and why?

I heard 2 arguments.First is that as pointers directly work with memory execution speed increases.Second is that ,memory handling is difficult ,so execution speed decreases.

Is it true that pointers in C increase execution speed of the program and why?
it can be true, depending on how you use them. For example if you pass a pointer to a class or function, you have ONLY passed that memory address to that class/function (and not copied the value in the variable which contains the data) which means that you have passed that variable much faster than if you were to use an actual non pointer variable or pass a variable by value.





Pointers are fast because they contain only memory addresses so the entire contents of the variable is not copied and passed which would be much slower.





Also to answer the second part of your question, yes in large programs memory handling can be difficult, pointers are active for the scope they are placed in (provided the programmer is linking to an object ONLY, and put a deconstructor into that object), put one at the start of your program and forget to delete it, well then now you have a major problem (a memory leak). I had a programmer friend that complained about his boss always saying, son its not your memory you need to stop pretending like it is, he was fresh out of college when he got that job, and his programming skill did increase when he got it.


Honestly though, it really does depend on the way you use them as well as the algorithm you go by.
Reply:pointers unlike variables do not store the value. instead they store the memory address of the variable that stores value. when we want to get the value pointed by the pointer using *ptr then the system do not need to find the memory of the value since it is already stored by the pointer. this makes working fast. u said that execution speed increases which is true.


Also, u said that memory handling is difficult. either u have misunderstood the question is a problem. one of the solution to ur next question is that pointer deal with memory directly. so it is very difficult to debug( find error ) from a program using pointers. so this makes the program development slow and not program efficiency slow. this is other concept beyond programming and is studied in software engineering.


i think u have under stood the use of pointer. it has merit of fast execution but demerit of slow program development.
Reply:Yes, pointers increase the execution speed because it directly points to the location of a variable, the value can be retrieved and processed in a short span of time.
Reply:Pointers don't magically increase or decrease anything. In some instances, pointers can be detrimental. So, what is a pointer? It's a variable that contains an address to a memory location. This is important: pointers are still variables. They take memory as well. If you're pointing to a char byte, using a pointer instead of a char isn't going to increase your efficiency, and may decrease it instead.





Pointers are useful when passing variables around in say, functions. What happens in C is that the variable is copied. So let's say you had a 100KB variable (like an array) in a struct. You don't want to pass that struct around by value, because your program would be forced to copy those 100KB. It's more efficient to refer to that memory location with a pointer, and then use that pointer to refer back to the value.





Note that you can shoot yourself in the foot with pointers. What happens if you have a memory location only known by its pointer, and then forget about the pointer (your pointer var goes out of scope, you accidentally assign another memory address, etc.)? You don't have a way to access the original memory location, and that memory sits around unused. Known as memory leaks.
Reply:Pointers increases the execution speed ,its rite...





it is one of the advantages of pointers,thts y we r going for pointers!!


No comments:

Post a Comment