The reason I asked the question is that I'm not sure that every
instance of a class will have the same size. If they don't, then how
would the result of incrementing a pointer (ptr++ ) be calculated?
You can count on it being consistent. *ptr will fetch the instance of
On Tue, 8 Apr 2003 05:47:00 -0400
"Steven T. Hatton" wrote:
the class that ptr points to. *(ptr + 1) will fetch the next instance of
that class, assuming you allocated them as an array. so:
MyClass *ptr = new MyClass[2];
ptr points to the first instance, and ptr + 1 points to the next.
BUT, if you do:
MyClass *ptr = new MyClass;
MyClass *ptr1 = new MyClass;
You cannot safely predict location of the next instance. In most
instances, ptr1 will not be (ptr + 1). The run time library may allocate
the space for ptr1 anywhere. While we talk about the heap, which is the
classic place where dynamic memory is allocated, newer technology today
may use mmap to allocate dynamic objects. Historically, Unix and Linux
used the brk and sbrk system calls which allocate virtual memory
contiguously above the program's data segment. But, even on the heap,
malloc() (which is usually the underlying memory allocator for new), may
reclaim some freed memory for ptr but not for ptr1 so they will not be
contigious. So, you can only count on pointer arithmetic working
properly for object allocated contiguously.
--
Jerry Feldman
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9