2018-01-07 14:22:11

I know the basics of C++. I know pointers, but I have a problem with this.
I know how to create pointers. I know how to output the address of a variable in memory. But what for it is necessary, where it is necessary to use, I do not know.
I know the only example of using pointers. Dynamic arrays. But at this time I can not realize this myself.
I've heard that I can write values into memory through pointers, because if you write a value into a variable through the variable itself, it will not be as fast as the pointer. But I do not know if it's true.
What other examples are there?
Thanks in advance!

Thumbs up

2018-01-07 20:43:07

read about heep and stack
when you declare a variable, it will be allocated on the stack while pointers are allocated into heep
you can manage memory with it (allocate and free when it is needed) hence faster speed and lower memory usage
in OS development, it is needed for working with devices etc
nd many many more!

bitcoin address: 1LyQ3hziMC2DTnCtgM3V1zfuZ73P3CYT9P

2018-01-08 00:11:13

I was reading a book for C++ today, and some of the important rules when dealing with pointers are:
1. When using new keyword, also always use delete, in case when you no longer need that pointer to prevent memory leaks.
2. After using delete, also reset that pointer to null, in order to prevent crashing if you accidentally recall the deleted pointer somewhere in the code.
4. Always check if pointer is null or not null before trying to recall it.
5. The keyword delete *must* always be used also when you want to reinitialize pointer, in other words, when you wanna make it point to another variable.

2018-01-08 07:49:29

hello again,
i recommend you to use smart pointers (they use rII to new and/or delete the pointers and you wont get memory leek)

bitcoin address: 1LyQ3hziMC2DTnCtgM3V1zfuZ73P3CYT9P

2018-01-08 08:48:20

I agree with visualstudio, use smartpointers, this pointers will delete the conten or the pointed-object when you are outsite the block. This is recommented, because when a exception is thrown, it clean automaticly up the pointers. Also the smartpointers throw a exception when you try to access on a nullptr.
Btw. use nullptr, not NULL. Because Null is defined as follows:
#define NULL 0
This macro can cause problems, when you have overloaded functions or something else.
Use google to read more about smartpointers and which do you should use.

best regards,

sorry for my english, I hope you have understood me.

Thumbs up

2018-01-08 22:29:48

Jonikster the reason you aren't understanding the many ways that you can use pointers is because you switch around and don't learn one tool. C++ is powerful. some of that is due to pointers. there are many ways to use pointers: dynamic memory allocation is just one of the many things as you mentioned. The more you use pointers and write code in c++ that uses pointers the more you will become comfortable understanding where they can be used
Please pick a tool and stick with it. all the languages are going to be able to do what you always ask questions about. BGT does have it's limitations but I don't think you want to use it. even BGT could do most of the things you ask about satisfactorily.
It all comes down to experience. Stick with a language and you will learn how to use it. switching back and forth helps no one, especially  not yourself.

I don’t believe in fighting unnecessarily.  But if something is worth fighting for, then its always a fight worth winning.
check me out on Twitter and on GitHub

2018-01-21 08:25:13

Pointers are a pointer to a specific block in memory. When you wish to hold an address to a sequence of characters (char*), or an address to an object pointers are useful. Also realize that c++ has references, which are just as useful and really powerful if used correctly... They work pretty much like pointers, with the bonus being that you can pass objects by reference as opposed to by value. This makes function calls much quicker on objects that are not PoD types which will be more than 4/8 bytes.