Dynamic Memory

Hi

I am having issues populating a vector using "new" where nodeVec is a vector where each entry consists of another vector with 3 elements. zMap is the vector I am trying to populate with the sum of the squares of the first two elements for each entry in nodeVec.

Any help will be greatly appreciated!

The code so far:

1
2
3
4
5
6
7
8
9
vector <double> mapNodalData(vector <Vec*> nodeVec, vector <double>& zMap){

    for (unsigned int i = 0; i < nodeVec.size(); i++){
        double vector *pz = new double vector(pow(((nodeVec[i])[0]),2) + pow(((nodeVec[i])[1]),2));
        zMap.push_back(pz);
        pz = NULL;
    }
    return zMap;
}

Now hold on a minute. Don't return zMap by value when it is already passes as a reference parameter. That is a waste of time.

The new statement makes no sense at all. What do you want a double or a vector<double>? YOu have allocated a pointer to a single double and I have no idea what that new statement means. What's a Vec type by the way?

The push_back is incorrect. zMap is a vector<double> not a vector<double*>. Even if the new worked you have a memory leak as you are trying to dynamically allocate without using delete. I'm not sure that you need to dynamically allocate using new at all based on your problem description. I recommend that you try again without using operator new. Finally remember that a*a is equal to a2. Good luck!
Topic archived. No new replies allowed.