some questions

Once again, I have amassed some questions.

-Besides not needing to completely copy an object, what other uses does a pointer have that can't be done in some other way?

-Is it possible to see the ml of your .exe when you compile?

-Is it possible to make a program that when compiled, turns it into another language,like say ruby?

-how do you read a certain line from an opened file from <fstream>?

-I made a text editor and have four questions
*How do you make it so that you can actually take input; meaning when I press enter it won't end letting me input?
*I made a delete option but how would I be able to do that?
*Is it possible to enter a path to store the file in?
*Can you use a command to make a file?

-I can't find any sites to teach me the following (suggestions please)
*javascript and calling it in c++
*same with lua
*how to write php
Pointers are useful in many ways.

a) As function parameter: if you pass a variable to a function, the function creates a copy of that variable. The original variable is never changed by anything happening inside the scope of the function. If you want the function to change its argument(s), you pass the variable by pointer (or reference).

b) As flexible 'memory': imagine you have a list of 5000 items and you want to keep track of the minimum item. You can copy the value of the minimum, but if the item changes, you'll have to make sure the copy reflects this, which is difficult to track. If it's an array, you can keep the index, but that's not alwas an option. Instead, creating a pointer to the minimum pointer can work just fine. In minimal memory (4bytes, regardless of object size), you have full information on the minimum item.

c) As a way to organize data in data structures: once you go beyond arrays to store data, you'll most likely need pointers to organize it. Think of a so-called "linked list": it can be stretched to any size without any overhead (unlike an array/vector, which needs to allocate new memory and copy!), it can delete (and insert) an item from its location in constant time (unlike an array, which needs to "shift" all elements after the deleted element one space left) and other handy things. The linked list works by keeping a pointer to the next [and to the previous, if you want a doubly-linked list) item in the row. Deleting and inserting an item is done by updating the pointer(s) of its neighbors. Many "advanced" data structures are a more complex implementation of the same principle: replacing copy/swap/assignment of heavy items by simply updating pointers.

d) It's the only way to use Heap memory: the "new" keyword allocates memory on the heap, but since it doesn't actually exist on the stack, there's no way to find it without keeping a pointer on the stack with the address of the heap memory. 4bytes on the stack for "infinite" memory on the heap is a pretty sweet deal!

I'm sure there are other uses, but these are the major ones I know of.
Another major use of pointers is for OOP polymorphysm: generaly you benefit of virtual calls with a pointer or reference(which is a hidden pointer) on a base type. And also to make use of RTTI infos(typeid and dynamic_cast).

I don't understand you second question, could you elaborate?
a compiler turns c++ into machine language so that the computer can understand it
Topic archived. No new replies allowed.