1

If non-class types, like pointers and int, do not have member functions like constructors, how are values assigned to them? For instance:

int x = 1;
int p = x;
p = 2;
int a = int();

When I did int p = x; would that simply perform a shallow copy, like an implicit copy constructor? Same goes for all other implicit member functions, since non-class types tend to work in the same way as with class types in those situations.

Also, when you have a class, that class would probably have an object that stores a certain value(if the class is supposed to represent a value), but is there some sort of buffer for these non-class types? Otherwise, how can a value be copied from one to another?

  • 3
    Buy yourself a good [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and read it. Then if you still have such basic questions, come and ask. – Walter Jul 07 '17 at 14:55
  • 6
    @Walter How do you know OP hasn't already read one and might still have that question? I don't think the question is basic at all. – Hatted Rooster Jul 07 '17 at 14:56
  • 1
    Regarding Build-in constructors: https://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors – Outshined Jul 07 '17 at 14:56
  • 2
    counter question: How do you tell the difference between a shallow copy and a deep copy of an `int` ? – 463035818_is_not_an_ai Jul 07 '17 at 14:56
  • @Walter Neither of the things you said is true. –  Jul 07 '17 at 15:01
  • I feel like this is somehow too broad as there can be so many interpretations of the question, it is doubtful there can be a subjectively "good answer", or rather, the range of possible answers is humongous – Passer By Jul 07 '17 at 15:42
  • Everything has a size - that is where the values go. (Some thing have a size of zero, but that's more advanced magic than creating ints etc). The size is determined by the type. An long will be at least as big (if not bigger) than an int. – doctorlove Jul 07 '17 at 15:56

3 Answers3

1

To better understand the semantics of built-in types, consider the design and evolution of C++ language.

Initially there was no C++, and there was only C. In C, there were no classes, but only built-in types - such as ints, floats, pointers and so on. Every definition of the variable would mean a space sufficient to store variable of this type was allocated somehow (that allocation could be real memory reserved or might simply be a designation of the CPU register). Later on all access to this variable would be routed to the designated space. No constructors or destructors existed at all, and copying one variable to another simply meant copying the contents of source 'space' into target.

Later, when C++ was introduced, the great efforts were taken to make legacy C programs compatible with new language as much as possible. That mean, that for built-in types behavior remained exactly the same as in C. And it still continues to be true. Whenever you define a variable of built-in type (which doesn't get optimized out by optimizer, since it could), a space is allocated for it and all access is routed to this space. Copying those types is always direct space copy from target to the source.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
1

If non-class types, like pointers and int, do not have member functions like constructors, how are values assigned to them?

A constructor is not magic. The compiler and computer do not randomly find themselves incapable of performing basic memory copy operations just because a type does not have the concept of constructors.

Internally the logic is similar but the copy operation is simpler. A copy constructor is a way to compose operations over the members of a class, which themselves may be other class instances or built-in types. The built-in types are the "base case" and I can assure you that the compiler knows how to generate code to copy them.

Of course, because they are the "base case", there is no meaningful application of the terms "shallow copy" and "deep copy" here — a copy is a copy is a copy. An int has no indirect resources that could be copied or not copied, which is where shallow/deep copying comes in with classes and pointers.

A more in-depth analysis of how compilers work is out of scope.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
-2

In C++ primitive types, (bool, char, short, int, long, double, float) are values that can be directly stored in CPU registers.

When the CPU gets to the declaration of your variable, it acquires RAM to store the value. In C++11 and later the additional step of value-initialization is taken.

To break down your code:

int x = 1;

This will acquire enough RAM for an int (usually 4 bytes), associates the symbol x with the address of the acquired RAM and assigns the RAM at that address the value 1.

int p = x;

This will acquire enough RAM for another int, associate it with the symbol p, and copy the value stored at the address associated with the symbol x into the RAM at the new address.

p = 2;

This goes to the RAM address associated with the symbol p, and overwrites the value there with 2.

int a = int();

This acquires an another int-sized piece of RAM, associates the address of the RAM with a, calls the no-argument int conversion function, and assigns the return value (0) to the RAM at the address of a.

Enter pointers

To get deeper into the underlying mechanics, C++ allocates memory for variables in two locations in RAM: the stack and the heap. When you declare a variable with the syntax int g;, the associated ram is a stack address, and is acquired for use when the scope (a section of code delimited by {}) the variable is declared in is entered, and remains allocated until the scope is exited, at which point it is deallocated.

Pointers operate similarly: If you declare

int* k;

the space for a pointer is allocated on the stack, and the value at the address is either garbage, or with C++11 value-initialized to null. To assign an object to a pointer, you use the new operator,

k = new int(2)

which allocates enough heap RAM to hold an int, initializes that RAM with the value 2, and assigns the address of the allocated RAM as a value at the stack address of k.

Tzalumen
  • 652
  • 3
  • 16