How do I delete a structure?

Hello,
I'm new to c++ and am now trying to build a neural network that I had initially built in matlab.

I'm somewhat stuck on a for loop that I have, which basically iterates and adds vectors into structures. The structures themselves are stored in a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
for(int index=0; index < max; index++) {
    struct_MAP temp;
    
    //do some stuff to build the structure
    temp.someval = 1;
    for (int i = 0; i < someval; i++) {
        //do stuff that adds to the temp structure using pushback, etc.
    }

    MAP.push_back(temp); //add the structure to the vector of structures (MAP)
    
    //DELETE temp??
}


My problem is that without deleting temp, the inside for loop keeps adding to the structure from previous loops. I need to somehow delete temp. I've looked online and found lots of very confusing "examples", but was hoping to get an answer specific to my case because I simply don't understand how it's done...

Thanks in advance!
You could possibly use new:

1
2
3
temp = new struct_MAP;
...
delete temp;


I think that should work, I don't normally use new.
Though if you showed more code then we can see what the problem is with temp keeping previous information. You might need to change the position of temp so that it is created and destroyed every time the function is executed.
I thought about what you said and found a way of avoiding the problem of the values iterating and growing larger. Here is what I have now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(int index=0; index < max; index++) {
    struct_MAP temp;
    
    //do some stuff to build the structure
    temp.someval = 1;
    for (int i = 0; i < someval; i++) {
        temp.value = myfun(i);
        iterator.pushback(temp.value);
    }
    MAP.push_back(temp); //add the structure to the vector of structures (MAP)
    
    iterator.clear(); //clears the value

    //DELETE temp??
}


So now I've avoided the more serious problems. However, my MAP vector is global (ie. defined outside of the function) and I'd like to avoid returning it with a "temp" entry inside it. If I replace "struct_MAP temp" with "temp = new struct_MAP" I get the following error:

"'temp' undeclared (first use this function)"

So I can't seem to get your suggestion to work. Is there no way of simply finding the memory location of the temp structure and removing it?
temp will be deleted when the function exits because it is allocated on the stack (since it only within the function scope).

You don't have to "delete" it.

Note: This depends however on if MAP.push_back(temp) copies the whole structure somewhere or just keeps a pointer. If it copies then fine, if it doesn't then you will get a runtime error when you try to access this structure at a later stage since you will have deleted it. The solution to that (if that is the case) is to allocate a new structure (create a pointer: struct_MAP *structure = new struct_MAP) and don't delete it.

Topic archived. No new replies allowed.