memory leak in the code?
-
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do not delete a.
try { a = new int [N]; b = new int [M]; } catch (bad_alloc) { // if a success, but b fail, should we try to delete[] a here to avoid memory leak? }
thanks in advance, George -
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do not delete a.
try { a = new int [N]; b = new int [M]; } catch (bad_alloc) { // if a success, but b fail, should we try to delete[] a here to avoid memory leak? }
thanks in advance, GeorgeYou have always to do your cleanup stuff! :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
You have always to do your cleanup stuff! :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles]Thanks CPallini! I think you mean we have to delete A, right? regards, George
-
Thanks CPallini! I think you mean we have to delete A, right? regards, George
Yes. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do not delete a.
try { a = new int [N]; b = new int [M]; } catch (bad_alloc) { // if a success, but b fail, should we try to delete[] a here to avoid memory leak? }
thanks in advance, GeorgeHello George, nice to see you again. But this time I've got a question for you. Straight to you. Why do are you acting 'quick'-judgemental before someone could actually reply you fully?:~ See here[^] And I've seen many times , anybody who replies to your thread gets a 3 vote. Immediately. Is it a token of acknowledgment to say that you've read the message? :-). I'm really not hurt by that but I'm finding it funny! Why do you do that?
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]
-
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do not delete a.
try { a = new int [N]; b = new int [M]; } catch (bad_alloc) { // if a success, but b fail, should we try to delete[] a here to avoid memory leak? }
thanks in advance, GeorgeIn a case like this you should initialize both a and b to 0 before the try clause. You cannot delete a or b at this stage, I assume you will need to use them later, or what was the purpose of allocating them? int* a = NULL; int* b = NULL; try { a = new int [N]; b = new int [M]; } catch (bad_alloc) { // Tell the user if a or b failed... } // Do some stuff on a or b // Now delete if they are allocated if(a) delete a[]; if (b) delete b[]; Thanks!
-
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do not delete a.
try { a = new int [N]; b = new int [M]; } catch (bad_alloc) { // if a success, but b fail, should we try to delete[] a here to avoid memory leak? }
thanks in advance, GeorgeWhy you didnt use like this code try { int * a= new int[N]; int * b= new int[M]; } catch (bad_alloc&) { cout <<"Error allocating memory!"; }
-
Why you didnt use like this code try { int * a= new int[N]; int * b= new int[M]; } catch (bad_alloc&) { cout <<"Error allocating memory!"; }
huh Hamid! :| it's simply because he should have put it like:
int* a;
int* b;try { a= new int[N]; b= new int[M]; } catch (bad_alloc&) { cout <<"Error allocating memory!"; } wake up! :)
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]
-
You have always to do your cleanup stuff! :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
huh Hamid! :| it's simply because he should have put it like:
int* a;
int* b;try { a= new int[N]; b= new int[M]; } catch (bad_alloc&) { cout <<"Error allocating memory!"; } wake up! :)
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]
Yeah it was a quick sample. ;)
-
huh Hamid! :| it's simply because he should have put it like:
int* a;
int* b;try { a= new int[N]; b= new int[M]; } catch (bad_alloc&) { cout <<"Error allocating memory!"; } wake up! :)
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]
Not like that, see my earlier post. Always initialize pointers!! In this case set them to NULL(0). int* a = NULL; int* b = NULL;
-
Not like that, see my earlier post. Always initialize pointers!! In this case set them to NULL(0). int* a = NULL; int* b = NULL;
Who said I didn't do it? I'm a c++ programmer. class myclass { int* a; int* b; myclass() { a= NULL; b= NULL; } } ;P
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]
-
But I suggest he should do something like this :
if(pMyStuff!=NULL) { delete pMyStuff; }
:cool:
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]
And you are right! :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
Who said I didn't do it? I'm a c++ programmer. class myclass { int* a; int* b; myclass() { a= NULL; b= NULL; } } ;P
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]
Your code said it. Anyway, I think it is pretty important to show it in the code as well.
-
In a case like this you should initialize both a and b to 0 before the try clause. You cannot delete a or b at this stage, I assume you will need to use them later, or what was the purpose of allocating them? int* a = NULL; int* b = NULL; try { a = new int [N]; b = new int [M]; } catch (bad_alloc) { // Tell the user if a or b failed... } // Do some stuff on a or b // Now delete if they are allocated if(a) delete a[]; if (b) delete b[]; Thanks!
Thanks pierre_ribery, I want to confirm with you that your point is we need to delete a or b if they are successful allocated, even if bad_alloc happens (may be caused by other statements), right? regards, George
-
Why you didnt use like this code try { int * a= new int[N]; int * b= new int[M]; } catch (bad_alloc&) { cout <<"Error allocating memory!"; }
Hi Hamid, I am confused. My question is about whether we need to delete a or b if bad_alloc happens, does your reply has anything related to my question? :-) regards, George
-
Yes. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles]Thanks for your confirmation, CPallini! regards, George
-
Hi Hamid, I am confused. My question is about whether we need to delete a or b if bad_alloc happens, does your reply has anything related to my question? :-) regards, George
My reply was for check does it with success or no (and my suggestion is when you want to allocate or convert use of try/catch block) and when you got error means that it doesnt allocate any thing to variable.
-
Thanks pierre_ribery, I want to confirm with you that your point is we need to delete a or b if they are successful allocated, even if bad_alloc happens (may be caused by other statements), right? regards, George
Yes that was exactly my point! If you have allocated memory, then you have to delete it. Therefore it is vital to initialize your pointers to 0 before using them. Cheers, Pierre
-
My reply was for check does it with success or no (and my suggestion is when you want to allocate or convert use of try/catch block) and when you got error means that it doesnt allocate any thing to variable.
Thanks Hamid, I have developed a couple of samples, which specific case do you think I need to check? regards, George