2

Keep going around circles, but I am still unclear about this. Have a feeling about the answer; but not sure. Which code below consumes more memory? [Should be the former, if I am correct.]

double x;
double* y = new double(x);

OR

double x;
double* y = &x;

6 Answers 6

7

In the former, two doubles exist (x, and the one pointed to by y). x is allocated on the stack, and y on the heap.

In the latter, only one double exists (x, also pointed to by y). There are no heap allocations involved here.

So, on the face of it, you are correct.

In both cases, there exists one double on the stack, and one double* also on the stack. The difference between the two is that in the first case, there is also a double allocated on the heap (the one allocated by new double(x)). Therefore the first case requires more storage.

Sign up to request clarification or add additional context in comments.

2 Comments

Are you sure? If we had to consider the RAM memory consumed overall by such a code (think millions of such doubles)...I need to know for sure if the former is correct. I understand that a pointer itself consumes a fixed 8 bytes (say, on a 64-bit system); so when you look at it like that, the first code appears to uses 2 x 8 (or is it 3 x 8 because of the new on the right hand side of the assignment in the second line?) bytes, while the second also uses 2 x 8 bytes. Which is correct?
You ended your comment saying "second case requires more storage". Is that a typo? Did you mean, "first case.." instead?
7

The following consumes sizeof( double ) + sizeof( double* ) plus sizeof( double ) on the heap:

double x;
double* y = new double(x);

The following consumes sizeof( double ) + sizeof( double* ):

double x;
double* y = &x;

Comments

1

The first one. There are two doubles and one pointer (usually long int)

In the second one you have only one double and one pointer

Comments

1

In this example:

double x;
double* y = new double(x);

you have the memory space for x, for the pointer y, and the new allocated memory that stores a copy of x, and is pointed by y.

In this example:

double x;
double* y = &x;

you have the memory space for x, for the pointer y which points to x. This uses less space.

Comments

1
double x;
double* y = &x;

will take sizeof(double) + sizeof(void*)

double x;
double* y = new double(x);

will take sizeof(double) + sizeof(double) + sizeof(void*). Also allocates memory from the heap via new. There will also be more bookkeeping overhead based on the heap allocator (especially if it breaks up a contiguous free chunk), and it will be slower.

Comments

0

first allocate space for 1 double at first line, then at second line allocates space for 1 pointer and another double, and copies value from the old one. The latter allocate space for 1 double and for a pointer. So first is more memory consuming.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.