0

I have some question about vector release. Releasing vector storage, they said that swap is best way to release vector

vector<tempObject>().swap(tempVector);

however, if vector has struct which has variety array, how to release this vector?

struct st{
    int *arr;
    st(int _size){
        arr = (int *)malloc(_size * sizeof(int));
    }
}

vector<st> vec_st;
// pushback data

1.

vec_st.clear();

2.

for(int i =0; i < vec_st.size(); i++){
  free(vec_st(i).arr);
}

is first way enough? or have to use second way? or other...?

My project running on the android using JNI. That's why I wander how to release efficiently to avoid memory problem. please help me....

ps. I don't wanna destructor. Cuz' it makes problem when used as parameter without pointer.

stenliis
  • 337
  • 8
  • 22
Ji-Seong
  • 25
  • 5
  • 7
    Have a destructor that handles it? – Some programmer dude Sep 19 '16 at 08:03
  • 1
    Also, if programming in C++, don't use `malloc` and `free`. First of all use a [standard container](http://en.cppreference.com/w/cpp/container) (such as [`std::vector`](http://en.cppreference.com/w/cpp/container/vector)) or if you really must allocate directly of the heap, use `new[]` and `delete[]`. If the structure `st`used a `std::vector`, then you would not have to do anything, it would be automatic. – Some programmer dude Sep 19 '16 at 08:05
  • Use `std::vector arr;` as your member for `st` and stop messing around with manually managing raw pointers. Large chunks of tedious and error-prone code (like your blatant memory leak in (1)) go the wayside. – WhozCraig Sep 19 '16 at 08:06
  • Lastly, read about [the rules of three, five and zero](http://en.cppreference.com/w/cpp/language/rule_of_three). Again, if the structure used a vector you could use the rule of zero and it would all work out fine, even passing the structure by value to functions. – Some programmer dude Sep 19 '16 at 08:07
  • @JoachimPileborg thx for advice. I never have seen link that you posted. It is useful to me. i also use `new[]` and `delete[]` :) When I wrote it , I didn't think that. but nothing changed, except to 'delete []' from 'free()'. and this struct is sample. In fact, my struct has 4 array which is changed size dynamically , 3 integer, 3 float :( so i think that it isn't adjust 'rule zero' – Ji-Seong Sep 19 '16 at 08:39
  • @WhozCraig I heard Pointer is faster than vector... and it is contained image... that's why i used array :( – Ji-Seong Sep 19 '16 at 08:44
  • @Ji-Seong: Where did you hear that? And why did you not _verify_ this claim before settling on a decision? – Lightness Races in Orbit Sep 19 '16 at 10:06
  • @JoachimPileborg you missed a step : `std::unique_ptr` ;) – Quentin Sep 19 '16 at 10:08
  • @JoachimPileborg you should probably advise people to use unique/shared ptr rather than new/delete. Modern C++ should use new/delete very infrequently. – xaxxon Sep 19 '16 at 19:36

2 Answers2

0

First of all why are you still using malloc() and free() with modern C++? Instead of int* use a vector with reserve(), resize() or just use one of its initializing ctors. You can get to the contiguous memory inside the vector using the data() method. Also, if you must use a struct, consider giving it a ctor and dtor (a struct is the same as a class, but by default all members are public).

  • sorry... in my source, I used `new[]` and `delete[]`. It could be better :) 1. my project is lib and run on the android which has small memory storage. that's why I can't reserve memory :( 2. In fact, designing project, I never thought about vector. Because I heard array is faster and smaller than vector to Image processing. but i don't know it's true or not... – Ji-Seong Sep 19 '16 at 09:01
  • [A type declared with `struct` **is** a class](http://stackoverflow.com/a/34108140/560648). – Lightness Races in Orbit Sep 19 '16 at 10:02
  • @Ji-Seong "has small memory storage. that's why I can't reserve memory " I don't know what you think "reserve" does, but what you said doesn't make any sense. It doesn't use any more memory, it just makes it work faster. – xaxxon Sep 19 '16 at 19:53
  • @LightnessRacesinOrbit C++ Class is running on Android. However, mac? ios? I don't know what problem is, the guy who is programming on mac said the class which has 'Variable in class, not in function' doesn't work because of version problem.... I just heard... so I couldn't use class :( – Ji-Seong Sep 20 '16 at 01:45
  • @xaxxon I thought 'reserve' is function to catch memory area to realloc fast, isn't it? if i reserved memory for my program, Android couldn't use enough memory to calculate because of my reserve area.... Honestly, I didn't perfectly understand how reserve function works :( – Ji-Seong Sep 20 '16 at 01:51
  • reserve doesn't use any more memory than you would anyhow, it just allocates it all at once instead of a bit at a time as you add more stuff to the vector. – xaxxon Sep 20 '16 at 04:07
0

invoking vector vec_st; You are creating vector of st objects and you use here default constructor for st, so I think defining constructor st::st(int _size) is pointless ( you are invoking st::st() ).

Anyway,You can use destructor to release memory. Using malloc/free, new/delete is quite obsolete now. Instead, use shared_ptr or unique_ptr that will manage acquiring/releasing resources and will prevent from memory leakage.

Lukasz
  • 248
  • 1
  • 7