Question:
what r the advantages &disadvantages of pointers in c?
nani
2007-10-29 09:30:10 UTC
what r the advantages &disadvantages of pointers in c?
Five answers:
Larry V
2007-10-29 11:56:53 UTC
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.
Neebler
2007-10-29 12:26:59 UTC
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.
Kausik kumaar
2007-10-29 09:33:51 UTC
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)
2007-10-29 10:12:06 UTC
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...
2016-03-13 15:37:34 UTC
all are independent, all are dependend - depends on how you use them. if your C/C++ code is very clean and does not use platform-dependend functions, then you can compile it on all systems with a C/C++ compiler. (note that the source code is portable, not the binary file). in java, the binary file is portable, too - but maybe it won't run, if you use platform-specific functions. .NET and its full functionality is so far only available on windows. Theoretically it's portable, practically it's not.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...