what size do we consider the pointer of 2d array in space complexity?

173 Views Asked by At

enter image description here

This is the code given in my algorithms book.We need to calculate its space complexity.

this is the answer given-

enter image description here

The space complexity is S=C+Sp and in this case Sp is zero as the code is independent of n. But I wanted to calculate the C part of the code and in this case it would be 5*2bytes=10 bytes considering a,x,n,0 and -1.

So my question is what would be the C in case a was a 2d matrix,do we consider it as 2 bytes only as in the case of 1d array or we take it as 4 bytes?

1

There are 1 best solutions below

0
AudioBubble On

Pointers and integers are no more 16 bits since the mid-eighties so I wonder what you mean with your 2 bytes.

Also note that your accounting of space is a little weird. You don't count the variable i, but you do count literal constants, which usually appear as immediate arguments in the assembly code.

Anyway, on a 32 bits machine, all addresses (and pointers) are represented on 4 bytes and the extra accounting of array dimensions is performed by the compiler and hard-coded into the assembly.

On a 64 bits machine, count 8 bytes per pointer and 4 bytes per int.