What is the point of pointers?

后端 未结 12 1315
梦谈多话
梦谈多话 2021-02-01 18:49

What is the point of pointers in C++ when I can just declare variables? When is it appropriate to use them?

相关标签:
12条回答
  • 2021-02-01 19:34

    Pointers store a memory address, so you would use them whenever you needed the memory address of something. This can be useful for things such as memory management, knowing where your data is stored. My professor told us this is a beautiful thing for advance programs in terms of ensuring your data are contiguously located in memory (like c-style strings are).

    Hope this is some form of help for you!

    -Zen, a newbie at c++

    0 讨论(0)
  • 2021-02-01 19:38

    Pointers are most useful when dealing with data structures whose size and shape are not known at compile-time (think lists, trees, graphs, arrays, strings, ...).

    Edit

    These related answers might also help (the top answer in the second link is definitely worth a look):

    In C++ I Cannot Grasp Pointers and Classes

    What are the barriers to understanding pointers and what can be done to overcome them?

    0 讨论(0)
  • 2021-02-01 19:38

    Sometimes you have a function that needs to return some amount of memory that isn't a set amount, such as reading from a file.

    bool ReadDataFromFile(char** contents);
    

    You would declare a char* contents and pass the address of that pointer to the function. That function would then allocate the memory and your pointer would point to the contents upon return.

    0 讨论(0)
  • 2021-02-01 19:46

    Apart from efficiency and flexibility.The main point of pointers in C/C++ is that is how the hardware works, you could not wite a device driver, memory manager or efficient cache without using pointers somewhere along the line.

    One of the main design goals for the first C compiler was to be a "portable assembly language" and to be able to do in a higher level language anything you could do with traditional assembly/machine code. This means being able to manipulate addresses directly - which is the point of pointers.

    However following the KISS principle dont use pointers unless they really are making things simpler.

    0 讨论(0)
  • 2021-02-01 19:47

    Pointers are best understood by C & C++'s differences in variable passing to functions.

    Yes, you can pass either an entire variable or just a pointer to it (jargon is by value or reference, respectively).

    But what if the variable is 20 meg array of bytes, like you decided to read an entire file in to one array? Passing it by value would be foolish: why would you copy 20 megs for this operation, and if you end up modifying it (i.e. it's an out-parameter) you have to copy that 20 megs BACK?

    Better is to just "point" to it. You say, "here's a pointer to a big blob of memory". And that little indirection saves a ton of time.

    Once you understand that, everything else is basically the same. Rearranging items in a list becomes just swapping pointers rather than copying every item around, you don't need to know how big things are when you start out, etc

    0 讨论(0)
  • 2021-02-01 19:48

    I had the exact same question when I was learning pointers, they just didn't seem important, but as you progress, you find out they are some times very useful.

    Pointers are used often in programming situations. For example, when you reference an array by name, such as array[i] = 3; The compiler is doing some fancy math that ends up turning that code into

    (address of array) + (sizeof(array elements) * i) = 3;

    They also make trees, linked-lists and other data structures possible, which you will find out as you learn more.

    0 讨论(0)
提交回复
热议问题