Pointers or arrays ?

Hi,
I'm about to start a new project about processing data stored in text file (all data are of type double) and I'm wondering which appraoch is the best (in terms of execution time) to deal with this huge amount of data:
-Arrays?
-Pointers associated with dynamic memory allocation?
Also, what is the difference between the two approaches and why?

thanks
I would say the best way (in terms of execution time) is to use a std::multiset<double>

multiset because multyset allows you to store numbers which may be equal, and because we (probably) don't know what in the text file is we use multiset.
If you're sure that all the numbers are different then std::set is prefered because it allows you to store only unique numbers.

Second thig why multiyset/set is prefered is the fact that set sort's it members during inserting and because you deal with huge amount of data, searching trough the set is extremly fast.

and of course set like any other STL container has a methods that enables you to perform various tasks.
also you can use STL algorhtms etc etc...

one more thing about multiset is that you don't have to provide how many data you have, multiset will resize on it own as needed so you don't have to wory about memeory allocation.

You can read more about this here:
http://www.cplusplus.com/reference/stl/multiset/

You asked what is better: Arrays or poiners to the heap?

definitely arrays (assuming on the stack right?) because heap alocation takes more time then stack alocation but also referencing takes longer.


Kind regards.
You don't say what the data is or how it's going to be used. If the order of the data is important then std::set/std::multiset is not very good.

Using a static array could be a problem because you probably don't know how much data the files will contain at compile time, and you can't change the size at runtime. If the amount of data is very large you easily run out of stack memory.

If you dynamically allocate the array with new you can decide the array size at runtime. If you don't know the amount of data when you start reading you will have to implement a mechanism to allocate more memory for your data in case you run out of allocated memory. If you use std::vector you don't need to worry about that. You can just use the push_back member function and it will automatically allocate more memory when needed.
The best between arrays and pointers is std::vector!:)
I was supposing that order of values is not important to the OP, if it is then yes std::vector is the best option :)

but if not then std::set is more powerfull then vector.

It depends what OP want's to do with his values.
I think a set is a bit too specific for the OP's question. If you're asking "static or dynamic array?", the logical answer is std::vector.
closed account (zb0S216C)
nadir CoCo wrote:
"-Arrays?
-Pointers associated with dynamic memory allocation?"

Pointers are the underlying type for all containers & arrays. Manually handling pointers increases the possibility of mistakes, such as memory leaks, segmentation faults, and access violations. The standard containers, such as std::vector, encapsulate the underlying pointers, which separates you from the pointers.

The choice depends entirely on the intent. If your program simply stores a group of something, with no specific requirements, use a standard container. If your program requires specific placement of objects, or requires careful alignment, use manual allocations.

Wazzak
hello again everybody,,,,,I 'm very grateful for you and your discussion it has ben so instructive,,,,I think I have been mixing things, but now it's clearer to me, and I like too much the idea of using std::vector, I think they'll stisfy my need to speed of execution;

if I had well understood, the STL containers are the product of pointers use, isn't it?
If it's the case, then I think I'm getting better :)

thank you very much :)
closed account (zb0S216C)
nadir CoCo wrote:
"if I had well understood, the STL containers are the product of pointers use, isn't it?"

Yes. As I've said, the std::vector isn't always the best solution. Sometimes, things have to be custom made to tailor your needs. I do this all too often, and I enjoy it, but it's experience at the end of the day.

Wazzak
Thank you
Last edited on
Topic archived. No new replies allowed.