0

In my project, there are one million inputs and I am supposed to compare search/sort algorithms with different numbers of inputs untill one million inputs. I want to do memory allocation and initialization with data together but I reailized it is not possible. So I decided to do like this;

double temp1, temp2, temp3;  //Each line has three numbers
    int i;
    Person *list[N];  //Here, stackoverflow occurs, for example N=500000
    for(i=0; i<N; i++){
        file >> temp1 >> temp2 >> temp3;
        list[i] = new Person(temp1, temp2, temp3);  //I wanted to initialize with data
    }                                          //but if I wrote "new Person[N]" 
                                               //stackoverflow doesn't occur
    
But there is an overflow with huge numbers, for example N = 500000. So, is there any method which combine these two?(Without overflow and with data initialization)

Secondly, is there any difference between these two code;
Person *list[N];
for(i=0; i<N; i++){
    list[i] = new Person();
    }

Person *list = new list[N];

11
  • Array of one million of data structures containing only three double values is not too big. It's size is only 1000000*3*8 = 24 Mb. Why can't you allocated 24 Mb? Commented Oct 24, 2014 at 12:10
  • 2
    Yea, for example Person *list[N]; and Person *list = new list[N]; list will have different types. Commented Oct 24, 2014 at 12:11
  • 1
    Use std::vector<Person> perhaps Commented Oct 24, 2014 at 12:12
  • @Ilya VS2013 uses 1MB as default max stack size... So yeah, this would be too big. @OP Is there a reason why you're using a simple array and not a container like vector? Commented Oct 24, 2014 at 12:13
  • @Sambuca, yes, but isn't it easy to increase this default value? Commented Oct 24, 2014 at 12:14

2 Answers 2

2

As a beginner, it's best to avoid using your own containers. You can just use the Standard-provided ones:

...

#include <vector>
#include <cstdlib>  // for EXIT_FAILURE, EXIT_SUCCESS

double temp1, temp2, temp3;  //Each line has three numbers
std::vector<Person> people;
for(int i=0; i<N; i++)
    if (file >> temp1 >> temp2 >> temp3)
        people.emplace_back(temp1, temp2, temp3);
    else
    {
        std::cerr << "error reading 3 numbers from file, terminating\n";
        exit(EXIT_FAILURE);
    }

It's especially useful to use vector (or new Person[n], and in contrast to new Person*[n]) to keep the data together (contiguous) in memory, so your CPU gets the maximum possible benefit from its caches during the searching and sorting that you want to compare... if your data's harder to access it'll hide the extent of performance difference between the algorithms under test. With new Person*[n] and every Person object being allocated on the heap, the data gets scattered and can be much slower to access.


Just to explain what was happening with your current code:

  • you were trying to put too much data on the stack; you can work around that by having a single stack-hosted pointer to the required amount of dynamically allocated memory (it's normal for an application to have massively more dynamic memory available than stack space).

Secondly, is there any difference between these two code;

Person* list[N];                  // first
for(i=0; i<N; i++){
    list[i] = new Person();
}

Person *list = new Person[N];       // second - corrected from "new list[N}"

The first asks for an array of Person*s on the stack, then assigns each of those pointers to a distinct dynamically-allocated memory address. At best, that will use almost as much stack memory - and at worst around double - as trying to put Person list[N]; directly on the stack and is likely to fail the same way. It also scatters the Person data around in dynamic memory, and operations on the data will be unnecessarily slow.

The second creates one dynamically-allocated memory region big enough for N Persons, and keeps a single pointer to it on the stack. That's not unreasonable (but std::vector's still a better idea).

Sign up to request clarification or add additional context in comments.

5 Comments

I guess people.reserve(N); would not hurt.
Thanks for your reply but in my university, there is only 2 or 3 lectures for standard library and this is just for introducing them. We mostly cover the background of this language. But thanks for your help, it might be good if I try to learn standard library.
@FaithBilginer: learning to make basic use of std::string, std::vector and std::map only takes an hour and can save days of frustration and errors in implementing your own equivalents. I heartily recommend making the investment.
Okey I understand it's the easiest way but think like this; what if there is a "Standard Library" course instead of a "Data Structure" course. All the backgrounds are not understood mostly. Now I should learn the harder one in order to implement some methods with my own to the standard libraries.
@FatihBilginer: different trajectories to the same goal - but keep in mind that seeing best practice (which those class designs illustrate) is a great way to learn quickly, and gives you a feel for what your own from-scratch designs in the same problem space should aspire to.
0

In your example,

Person *list[N];

is created as a local variable on the stack. 500,000 pointers would take up about 2 MB - which is likely to exceed the stack size on some machines. http://msdn.microsoft.com/en-us/library/windows/desktop/ms686774(v=vs.85).aspx

However,

    //Person *list = new list[N];
    Person **list = new Person* [N];

will create your array on the heap, and you should be able to allocate that without running out of memory. However, each Person object will have a size and require allocation in addition to the array of pointers.

2 Comments

but if N is small enough for not to overflow, is there still any difference?
yes, - I don't think you must use "new" if new to dynamically allocate memory (you only know how big at runtime). Also, the memory gets allocated in different places in memory, but in most cases that doesn't really matter. I'm not a C++ expert and am just grateful nobody has downvoted my c++ answer.. yet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.