- Nov 18, 2001
- 13,234
- 2
- 81
I've never dealt with arrays in C++. Can anyone tell me how to find the last element of an array (assuming it doesn't fill the entire space alotted)?
Arrays are by definition fixed boundary data types. The last element would be the length of the array - 1.
For this simple example, you're probably right.Originally posted by: Mucman
Just use vectors... they are safe, and just as fast as arrays (according to Bjarne Stroustrup).
Originally posted by: kylef
For this simple example, you're probably right.Originally posted by: Mucman
Just use vectors... they are safe, and just as fast as arrays (according to Bjarne Stroustrup).
But in reality, vectors (and the STL in general) are not as fast as using straight C library calls and intrinsic types, primarily because of all the memory allocation the STL performs in the background.
This is especially true with a multi-threaded application: use of the STL can be a serious bottleneck because you'll find that most of your threads are blocked on the C runtime's global heap allocation functions, which serialize threads because they are locked for thread safety.
The only way around this is to overload the STL's allocators to use a separate heap for each thread, and this is non-trivial.
Originally posted by: kylef
For this simple example, you're probably right.Originally posted by: Mucman
Just use vectors... they are safe, and just as fast as arrays (according to Bjarne Stroustrup).
But in reality, vectors (and the STL in general) are not as fast as using straight C library calls and intrinsic types, primarily because of all the memory allocation the STL performs in the background.
This is especially true with a multi-threaded application: use of the STL can be a serious bottleneck because you'll find that most of your threads are blocked on the C runtime's global heap allocation functions, which serialize threads because they are locked for thread safety.
The only way around this is to overload the STL's allocators to use a separate heap for each thread, and this is non-trivial.
Correct: I never said that STL itself is thread safe, and it certainly isn't. However, the STL does LOTS of memory allocations in the C runtime in the background that you don't see (unless you run in a debugger and watch what's happening). These allocations do use the C runtime's memory allocator, which is locked for thread safety. This absolutely serializes multi-threaded applications. I'm speaking specifically here of the MSVC implementation of the STL. Other implementations may vary.STL is not thread safe unless you use separate synchronization objects to keep from using the same STL object in 2 threads at once. The global heap allocation functions are NOT called every time you add an object to the container. The container grabs space for a bunch of objects into its pool and only asks the OS for more when the space it has already requested fills up.
You basically have to do it yourself, either with a pointer, index or by writing a special value at the end such as a null '\0'.I've never dealt with arrays in C++. Can anyone tell me how to find the last element of an array (assuming it doesn't fill the entire space alotted)?