Saturday, May 22, 2010

C++ for beginners?

what is a pointer


why can it be useful, disadvantage or disadvantage.


how is it similar to the goto command that is not recommended in language

C++ for beginners?
A pointer is a variable, it's similar to an integer in that it is a 32 bit number. But the value of this number is the location of a memory cell where a piece of data begins. That data can be anything, another pointer, an integer, a character string, or a data structure.





They are advantageous because if you are passing large structures to a function, you can pass the pointer instead and avoid the large memory copy onto the stack. Also, if you need to modify the contents of the memory, you can pass the pointer to a function and modify away.





Some of the disadvantages are that typically when allocating pointers you use the new or malloc functions which require you to call delete or free on that piece of memory when you are done using it. Failure to do so creates memory leaks. Using a non-allocated pointer can also lead to confusing bugs where you think a piece of memory is valid but it isn't.





It is not the least bit similar to the goto command. Goto is not recommended because with all the available loop choices, only a beginner programmer wouldn't know how to write loops w/o using goto.
Reply:A pointer is a way to store lists of data in memory by pointing to the next memory address the program should go to find the next required data.





Pointer %26gt; data1 Pointer %26gt; data 2 Pointer%26gt; data3... etc...





It's better then an array because it uses less memory and processing power.





You can even take it farther when you start to make link lists using pointers...








The goto command refers to the line number or subroutine name the program should hop too for the next insturctions(some languages use line numbers and others use a subroutine like name).





It's similar because like the goto command is pointing to where the next instructions should be and the pointer is pointing to where the next memory address can be found.





I hope this helps if not send me a line at


admin@chaoticpulse.com





edit* just in case your curious a pointer knows to stop when it finds a nil at the last memory address.
Reply:lol.... Why dont you just look it up in your textbook,instead of asking us... We wouldn't be there during your programming test either.LOL
Reply:Hi. A pointer is like any other variable, except it holds a memory address. Pointers are the core for c programming. They are extremely useful for communication, and sharing data with control. For example, with pointers, you can get multiple return values, in a manner of speaking. A function is only allowed to return one value. But what if you want more then that? Pointers and references are the answers.





bool AllocInt(int * ptr)


{


ptr = 0;


ptr = new int;





if (!ptr)


return false;


else


return true;


}





The above function can now be used to allocate some memory from the heap to hold an int. But that function will also let you know if the allocation was succesful, or of it failed.





int * Num;





if(AllocInt(Num))


{


std::cout%26lt;%26lt; "Allocation successful!";


}else


std::cout%26lt;%26lt;"No more memory available!";





Without pointers, you would have to check if the allocation was succesful yourself:





int * AllocInt(void)


{


return new int;


}





int * ptr = 0;


ptr = AllocInt();





if (ptr)


{


std::cout%26lt;%26lt;"Allocation successful";


}else





std::cout%26lt;%26lt;No more memory available!";
Reply:In C++ pointers are special variables which store the address( where the variable is located in the memory) of other variables .


No comments:

Post a Comment