Monday, July 27, 2009

What r the advantages &disadvantages of pointers in c?

The main advantages of using pointers are


1.) Function cannot return more than one value. But when the same function can modify many pointer variables and function as if it is returning more than one variable.


2.) In the case of arrays, we can decide the size of th array at runtime by allocating the necessary space.





Coming to the disadvantages of pointers


1.) If sufficient memory is not available during runtime for the storage of pointers, the program may crash (least possible)

What r the advantages %26amp;disadvantages of pointers in c?
C has a minimum number of fundamental data types - a single character, a single integer or float, and a few derived data types such as a structure, an enumerated list, and an array. If you want to do much more than that, you make use of pointers.





Pointers allow you to dynamically request memory to store off information for use later. They allow you to create linked lists and other algorithmically oriented data structures.





Pointers are a fundamental tool in developing code in C.





And the main disadvantage is that C provides very little in the way of "protection" for the programmer. It takes the worldview that a programmer that needs care will program carefully. So pointers can be set to values which are not really addresses - and if used, that will result in your application crashing.





Also, pointers are not managed - so if you dynamically allocate some space, store the address in a pointer, and later someone frees that memory, the pointer will continue to have the (now invalid) address as its value. If you use the address, you might cause all sorts of havok.
Reply:The advantage is that you can use them for anything. The disadvantage is that you can use them for anything.





On the up side: You need to use pointers to pass an array or anything larger than a few values to a function or if you want the function to return an array or anything more than a single value. They are small so they use little memory and they are quick to copy. You can add (ptr2 = ptr1 + 10) and subtract them (distance = ptr1 - ptr2). A pointer could be used to access static (hardcoded) data in one case and dynamic (malloc) data in another cases. You can change their type. For instance, you can read bytes from a file and then cast a pointer to int or double depending on the file contents.





On the down side: You need to be careful about the life of a pointer. They should be initialized to zero if you use it by mistake before its initialized with a valid value, your program will crash right away, making it easier to debug. Dynamically allocated memory (malloc) has to be freed when you're done with it. If you don't free it, nobody will. After freeing memory, the pointer should be set to zero for the same reasons as initializing it with zero. There are no limits to what you can point to. For instance, you increment a pointer past the end of an array and there's no way for the pointer to know it. Since you can change the type of a pointer, you can change it to a type that isn't correct for what its pointing to.
Reply:Advantages are that you can do almost anything with them.





Disadvantage is that you must manage all the memory. C let's you do most anything. It assumes that you know what you're doing and have accounted for all memory allocation and deallocation. It also let's you assign any section of memory to your variables. So if you're not careful, you can get into alot of trouble. Basically, it makes it harder to find bugs.





If you absolutely MUST have low level access to the system, stay with C. Otherwise, it's safer to use C++ or C# or even VB.NET. All of these, to some degree or another, will protect you from these disadvantages...


No comments:

Post a Comment