3

I am using the multiprecision complex number library (zkcm) and want to compare some results using complex<double>.

At one point I try to double check the memory of the variables using sizeof() but I get the same answer (32) no matter how big a memory I allocate the variables to; i.e. the following snippet prints 32 no matter what I use inside zkcm_set_default_prec():

zkcm_set_default_prec(128);
zkcm_class z;
cout << sizeof(z) << endl;

Is there another way than sizeof() to get the memory size of a variable?

whoan
  • 8,143
  • 4
  • 39
  • 48
jorgen
  • 3,425
  • 4
  • 31
  • 53

2 Answers2

1

I can't test it, and the documentation is a bit vague, but there's a method with this signature:

int zkcm class::get_prec ( void ) const;

Which is described like so:

Get the internal precision of the object, namely, the precision used for each part of "this" complex number

This might return the number of digits, which should be proportional to the amount of memory used. Of course the exact relationship is an implementation detail. The class itself probably just holds a pointer to a heap-allocated buffer where the digits live and some bookkeeping information. The sizeof operator (in C++) is fully static, i.e. evaluated at compile-time.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

At one point I try to double check the memory of the variables using sizeof() but I get the same answer (32) no matter how big a memory I allocate the variables to

sizeof() is evaluated at compile time and there is no way in C++ language to change that value at runtime, period. There could be some interface that provides information you need (either directly or indirectly), could be not. But sizeof() definitely is not the way to achieve what you want.

Slava
  • 43,454
  • 1
  • 47
  • 90
  • sizeof returns the size of the object itself. If that object has data allocated through new () or malloc () then sizeof doesn't count the allocated data, just the size of the pointer to the allocated data. Just like sizeof (char*) returns the size of the pointer itself, not the size of the allocated data. – gnasher729 Jan 14 '16 at 14:27