1

Would a constant-size array int x[20] be smaller than a dynamic array int * x = new int[20], since the dynamic array is a pointer, which takes up space? Or is the constant-sized array a pointer too? Why or why not?

Another question: Something I've never been 100% sure about is, on a 32-bit machine, are pointers 32 bits long? And are pointers treated the same way as regular variables when it comes to the location in the RAM in which they are stored?

Help would be very much appreciated.

slartibartfast
  • 4,348
  • 5
  • 31
  • 46

5 Answers5

1

The dynamic array is not a pointer - but you do need a pointer as well as the array, so the allocated version uses more space. The advantage of the allocated version is that you can allocate the correct amount of space, rather than having to allocate more space than you normally use (e.g. you can allocate exactly 37 integers, rather than having to allocate 100 just in case the size is as big 100).

Yes, 32-bit machines have 32-bit pointers, essentially without exception.

Yes, pointers are treated the same as other variables when located in RAM.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

Would a constant-size array int x[20] be smaller than a dynamic array int * x = new int[20], since the dynamic array is a pointer, which takes up space?

The memory address(es) of both have to be stored, be it the offset of the first element in the constant-sized array or the pointer to the dynamic array. The arrays themselves are equal size and (I believe) the other data is as well.

The constant-sized array variable, x, can be used in some ways similar to a pointer, but is not a pointer in and of itself, it's an array.

The 32-bit and 64-bit typically refer to the memory addressing, so pointers are 32-bits on a 32-bit system and 64 on 64.

Pointers are very much like integers and are stored in memory the same way (you can convert a pointer on a 32-bit machine to a dword and it won't lose information). They simply refer to an offset in memory somewhere else, where the actual data is stored.

ssube
  • 47,010
  • 7
  • 103
  • 140
  • So basically the difference between the two types of arrays is that the constant array's offset is figured out at compilation time, while the dynamic array's offset is figured out at runtime? – slartibartfast Apr 01 '11 at 03:46
  • The memory address of an automatic or static array is not stored separately - it appears directly or indirectly in the machine code. – Jonathan Leffler Apr 01 '11 at 03:56
1

The dynamic one will use more memory; how much more depends on the size of pointers and the size of the memory allocator's metadata.

On any modern machine, as @Jonathan says, 32-bit machines use 32-bit pointers and there is no significant difference (aside from stack vs. heap) in how pointers are handled vs. data. Older machines (or even a newer one if you build in "real mode" with split instruction and data spaces) will distinguish between function and data pointers, but not in where they themselves are so much as which address space they point within.

Some very old machines and some experimental architectures — none of which you are ever likely to encounter — actually manage pointers and sometimes data differently; Emacs's historical handling of integers (stealing one or more bits for internal use) reflects the use of "tag bits" on some old architectures. You may also occasionally encounter (mostly older) programs which "know" that pointers should be aligned on 4-byte boundaries and borrow the bottom 2 bits of pointers for other purposes (garbage collection state was a common use).

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • A couple of nits: first, even the most modern Intel supports segments, and at the hardware level, pointers can be either 32 or 48 bits. Practically, the major OS's have decided to ignore this, and only use the 32 bit model, but it's a choice of the OS and the compilers under it, not of the hardware. Secondly, there are still some exotic architectures being sold---Unisys mainframes, in particular---which use a tagged architecture. – James Kanze Apr 01 '11 at 08:25
  • @James: nit-pickingly, I think that first part is covered under "real mode". (I don't even want to think about 8086-emulation "real mode", which hasn't really qualified as "real mode" for a while; I don't recall whether it was the 486 or the Pentium when Intel made it a non-native/emulated environment.) As to Unisys, I keep forgetting that they even still exist; they have more lives than the proverbial cat. – geekosaur Apr 01 '11 at 08:31
  • Segments don't just exist in "real mode" on an Intel, although they are handled differently in the various protected modes. (I've used 48 bit pointers in protected mode on an 80386.) It's true, however, that the two most common OS's for the processor don't support multiple segments. – James Kanze Apr 01 '11 at 10:28
  • @James: I'm not sure I'd call the protected mode ones segments as such; aren't they effectively page tables? – geekosaur Apr 01 '11 at 10:31
  • Selectors, or something similar, would be a better word than segments. They select a page table; under Windows and Linux, I think they're always set up to select the same page table, regardless of which one you use, but the hardware certainly supports more. – James Kanze Apr 01 '11 at 16:20
0

My understanding is that the constant-size array name is a pointer to the first item in the array (essentially)

jwwishart
  • 2,865
  • 2
  • 23
  • 26
  • 1
    It is — but it's a pointer that only exists in the compiler/assembler/linker; there is no allocated memory containing that pointer value. – geekosaur Apr 01 '11 at 03:45
  • Your understanding is wrong. The name of an array names the array, and when used in an expression, has the type of the array (int[10], in this case). An array will convert implicitly to a pointer, and since there are very, very few expressions in C++ which allow arrays (sizeof or binding to a reference to an array come to mind), this conversion enters into play very often, but it doesn't change the meaning of the name of the array. – James Kanze Apr 01 '11 at 08:28
0

This depends on what you want to know the size of.

The size of the variable x will certainly be smaller in the case of the int* x = new int[20]. In this case x is just a pointer and hence has pointer size (typically 4 bytes on a 32 bit system and 8 bytes on a 64 bit system). In the other case x is actually a statically allocated array of length 20 so it's size will be sizeof(int) * 20.

If you're talking about the size of the actual array though there really is no difference. The issue is where will it be allocated. In the int x[20] it will be typically on the stack while the pointer version will be on the heap

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454