Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you explain C++ pointers to a C#/Java developer? [closed]

I am a C#/Java developer trying to learn C++. As I try to learn the concept of pointers, I am struck with the thought that I must have dealt with this concept before. How can pointers be explained using only concepts that are familiar to a .NET or Java developer? Have I really never dealt with this, is it just hidden to me, or do I use it all the time without calling it that?

like image 857
Michael Hedgpeth Avatar asked Mar 02 '11 23:03

Michael Hedgpeth


People also ask

How do you explain pointers?

A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory items. Pointers are very useful for another type of parameter passing, usually referred to as Pass By Address. Pointers are essential for dynamic memory allocation.

What is pointer in C explain?

A pointer is a variable that stores the memory address of another variable as its value. A pointer variable points to a data type (like int ) of the same type, and is created with the * operator.

What is pointer in C and its types?

A pointer is a variable whose value is the address of another variable of the same type. The value of the variable that the pointer points to by dereferencing using the * operator. The different types of pointers are void, null, dangling, wild, near, far, huge. A pointer can be typecasted to different data types.

How is pointer declared in C and give example?

For example, if you want a pointer to point to a variable of data type int, i.e. int var=5 then the pointer must also be of datatype 'int', i.e. int *ptr1=&var. The * symbol indicates that the variable is a pointer. To declare a variable as a pointer, you must prefix it with *.


1 Answers

Java objects in C++

A Java object is the equivalent of a C++ shared pointer.

A C++ pointer is like a Java object without the garbage collection built in.

C++ objects.

C++ has three ways of allocating objects:

  • Static Storage Duration objects.
    • These are created at startup (before main) and die after main exits.
      There are some technical caveats to that but that is the basics.
  • Automatic Storage Duration objects.
    • These are created when declared and destroyed when they go out of scope.
      I believe these are like C# structs
  • Dynamic Storage Duration objects

    • These are created via new and the closest to a C#/Java object (AKA pointers)
      Technically pointers need to be destroyed manually via delete. But this is considered bad practice and under normal situations they are put inside Automatic Storage Duration Objects (usually called smart pointers) that control their lifespan. When the smart pointer goes out of scope it is destroyed and its destructor can call delete on the pointer. Smart pointers can be though of as fine grain garbage collectors.

      The closest to Java is the shared_ptr, this is a smart pointer that keeps a count of the number of users of the pointer and deletes it when nobody is using it.

like image 189
Martin York Avatar answered Oct 08 '22 19:10

Martin York