Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ sizeof operator - pointer to double

I have unexpected outcome with sizeof operator (C++). In main class, I have these lines

 double * arguments_ = new double();
*arguments_  = 2.1;
*(arguments_+1) = 3.45;
 cout <<  (sizeof arguments_) << ' ' <<  (sizeof arguments_[0]) << ' '<< (sizeof arguments_)/(sizeof arguments_[0]);

which give me output 4 8 0

Double size is 8 bytes, and (sizeof arguments_[0]) = 8. However, why is (sizeof arguments_) not expressed in bytes as well (2*8 = 16)? Is sizeof operator applica

like image 769
kiriloff Avatar asked Dec 22 '22 01:12

kiriloff


1 Answers

Both values are expressed in the same units. You have a 32-bit system, so the size of an address is 32 bits, or 4 bytes. The size of double on your system is 8 bytes. The result of an integer division 4/8 is zero.

like image 79
Sergey Kalinichenko Avatar answered Dec 24 '22 01:12

Sergey Kalinichenko