Home:ALL Converter>Memory allocation with operator new and initialization with data

Memory allocation with operator new and initialization with data

Ask Time:2014-10-24T20:05:27         Author:ninbit

Json Formatter

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];

Author:ninbit,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/26547533/memory-allocation-with-operator-new-and-initialization-with-data
Tony Delroy :

As a beginner, it's best to avoid using your own containers. You can just use the Standard-provided ones:\n\n...\n\n#include <vector>\n#include <cstdlib> // for EXIT_FAILURE, EXIT_SUCCESS\n\ndouble temp1, temp2, temp3; //Each line has three numbers\nstd::vector<Person> people;\nfor(int i=0; i<N; i++)\n if (file >> temp1 >> temp2 >> temp3)\n people.emplace_back(temp1, temp2, temp3);\n else\n {\n std::cerr << \"error reading 3 numbers from file, terminating\\n\";\n exit(EXIT_FAILURE);\n }\n\n\nIt'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.\n\n\n\nJust to explain what was happening with your current code:\n\n\nyou 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).\n\n\n\n Secondly, is there any difference between these two code;\n\n\nPerson* list[N]; // first\nfor(i=0; i<N; i++){\n list[i] = new Person();\n}\n\nPerson *list = new Person[N]; // second - corrected from \"new list[N}\"\n\n\nThe 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.\n\nThe 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).",
2014-10-24T12:16:42
yy