Any guarentee that C memory addresses will be ascending?

Armitage

Banned
Feb 23, 2001
8,086
0
0
So I have this algorithm that needs to find the intersection of three vectors of pointers to objects. The objects are held in another vector.

Right now I sort the vectors by an ID field i the objects, and then run binary searchs, again, 0n this ID field.

I was thinking that I could just as well sort & search the vectors of pointers by the address of the original object rather then dereferencing the object and calling the ID function. Then I thought about it some more, and wondered if the addresses of the memory allocated (using new) for the object vector is guarenteed to be in ascending order already? Because of the way the vectors of pointers are formed, they would also be sorted if this is true, and I could skip the sort step!

I wrote a small chunk of code below to test it, and it seems to be true, but I don't know if this is a general result. I highly doubt it - and doubt I would rely on it, but thought I'd ask.

 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
When you malloc in C, you basically allocate a big, contiguous chunk of memory. I don't know how your vector functions handle stuff internally so I'm not too sure, but it should work.

x = (int**) malloc(SIZE*sizeof(int*));

That line of code basically allocates an array int pointers of size 1000. You also can iterate through using x[0], x[1], etc. and each element will be an integer pointer.
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
Originally posted by: igowerf
When you malloc in C, you basically allocate a big, contiguous chunk of memory. I don't know how your vector functions handle stuff internally so I'm not too sure, but it should work.

x = (int**) malloc(SIZE*sizeof(int*));

That line of code basically allocates an array int pointers of size 1000. You also can iterate through using x[0], x[1], etc. and each element will be an integer pointer.

as long as the objects are in a single array i think that'll be ok. if they were in multiple arrays of different sizes, theres no way of knowing.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: igowerf
When you malloc in C, you basically allocate a big, contiguous chunk of memory. I don't know how your vector functions handle stuff internally so I'm not too sure, but it should work.

x = (int**) malloc(SIZE*sizeof(int*));

That line of code basically allocates an array int pointers of size 1000. You also can iterate through using x[0], x[1], etc. and each element will be an integer pointer.


Yea, I guess for pointer arithmatic to work, it has to be true, unless the compiler does some tricky stuff internally for pointer arithmatic.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
When you malloc in C, you basically allocate a big, contiguous chunk of memory
^ true.

Note though that only addresses within each malloc are contiguous and ascending. Any additional mallocs could have addresses higher or lower than the first malloc.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Armitage
Originally posted by: igowerf
When you malloc in C, you basically allocate a big, contiguous chunk of memory. I don't know how your vector functions handle stuff internally so I'm not too sure, but it should work.

x = (int**) malloc(SIZE*sizeof(int*));

That line of code basically allocates an array int pointers of size 1000. You also can iterate through using x[0], x[1], etc. and each element will be an integer pointer.
Yea, I guess for pointer arithmatic to work, it has to be true, unless the compiler does some tricky stuff internally for pointer arithmatic.
Even if the compiler did do tricky stuff you're still only doing pointer arithmetic so the numbers you use will still be ordered :p