+ 3

When do we use pointers in c++ ?

8th May 2022, 11:09 AM
ahmed hossam
ahmed hossam - avatar
10 Antworten
+ 9
-to allocate new objects on the heap -to pass functions to other functions. -to iterate over elements in arrays or other data structures. -to pass array as function argument or return an array
8th May 2022, 3:34 PM
Zakaria Elalaoui
Zakaria Elalaoui - avatar
+ 8
JaScript beg to disagree. c++ have lots of other options. Why use c++ if you are going to write C code? I would say, use pointers only if you absolutely need them. Here is a Stackoverflow topic: https://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself
8th May 2022, 11:57 AM
Bob_Li
Bob_Li - avatar
+ 4
I've been learning programming myself for a relatively short time and I think that there are people here in the community who studied and closed it at university and have the most knowledge about it. Hopefully they will contribute something soon here. The question Bob_Li quotes was asked and answered a good 8 years ago and I would only consider it as an opinion. There are many situations, where you need acceess / change data memory contents such as arrays, customised data structures etc. From other learning sources and my small experience, it is seen as an advantage to build the algorithms this way using pointers. The best to understood why that is better can be showing with pictures, not with words. By the way, if you want to examine your solution paths, you can simply measure the execution time of your algorithm and decide what you take.
8th May 2022, 3:32 PM
JaScript
JaScript - avatar
+ 2
As often as possible, because it is faster then most other solutions.
8th May 2022, 11:34 AM
JaScript
JaScript - avatar
+ 2
JaScript True, you can optimize your code if you dive deep into pointers, but that depends on your use case. Ask yourself what your code is going to be used for? Is the gain you get for optimization worth the effort? And cryptic codes are things no one wants to touch. Ever heard of the quote "Premature optimization is the Root of all Evil"? I would think writing everything in pointers falls in that category. But if you really must, then go with pointers, sure. https://stackify.com/premature-optimization-evil/
8th May 2022, 4:27 PM
Bob_Li
Bob_Li - avatar
+ 2
JaScript While studying, pointers had 80% of my classmates drop out or fail C++ entirely. It's an awesome feature which with any power comes great responsibility, the good and the bad. You could declare a pointer variable to hold the address in memory for a huge 1 GB array. Passing the pointers to and from classes or functions carries less weight than the actual value, increased speed and efficiency. Hopefully cleaning up after the use of such variable is complete, as leaking memory is not recommended.
9th May 2022, 10:07 PM
l4t3nc1
l4t3nc1 - avatar
+ 1
Bob_Li thanks <3
8th May 2022, 1:49 PM
ahmed hossam
ahmed hossam - avatar
+ 1
JaScript well ... Is there's any particular ideas where we use them ? Or it's just about how we solve the problem ?
8th May 2022, 1:50 PM
ahmed hossam
ahmed hossam - avatar
+ 1
Zakaria Elalaoui -to use C instead of wasting time to learn C++😁 I mean, why bother with C++ at all. If you want total control, C is sufficient.
8th May 2022, 4:42 PM
Bob_Li
Bob_Li - avatar
+ 1
pointer refer to a variable which hold the address of another variable Like regular variables, pointers have a data type. It also point to function or array Example:: int*p =&a;
9th May 2022, 5:19 AM
Mihir Lalwani
Mihir Lalwani - avatar