Memory problem
-
Hi, I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?
-
Hi, I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?
I think that these links are helpful to you Dynamic memory allocation in C++[^] and Memory leaks in C++ and how to avoid them[^]_**
**_
whitesky
-
Hi, I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?
Anu_Bala wrote:
I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?
Well some of the things I know I would like to share. :)
-
Use
new
anddelete
. -
Always use
delete[]
withnew[]
anddelete
withnew
-
Do not mix up c, c++. Either you use C, or use C++ for memory allocation.
-
Always set a pointer to
NULL
after deletion. -
Always allocate sufficient amount of memory. Do not allocate memory like this...
char *str = new char[ strlen( someStr ) ];//stingy
strcpy( str, someStr );// well asking for trouble
delete [] str;//error here
str = NULL;//good practiceinstead use
char *str = new char[ strlen(someStr) + 1 ];//bring it on
strcpy( str, someStr );//gooood, very gooood.
delete [] str;//hehe fine
str = NULL;//good practice -
Try using
auto_ptr
; -
Maintain a list of pointers to memory that you have allocated. When you exit from your app you can delete them all at one go if any of them hasn't been deleted.
-
Always check for
NULL
on pointers after anew
. -
Always check for
NULL
on pointers before using them. -
Be really really afraid to dynamically allocate memory.
Nibu thomas A Developer Programming tips[^] My site[^]
-
-
Hi, I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?
I suggest you to read Effective C++ 2nd Edition or the latest 3rd Edition. If u r confused about pointers, read Pointers in C to know more about pointers. as nibu said if u r much concerned about pointers, try using auto_pointers that is the best way to handle memory leaks. but I suggest that not to use auto_pointer because simply u can handle those allocation and deallocation of objects. In MFC, it is not using any special allocation strategy, its all C++. Code Complete 2 is a book which contains some best practices. SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!" -
I suggest you to read Effective C++ 2nd Edition or the latest 3rd Edition. If u r confused about pointers, read Pointers in C to know more about pointers. as nibu said if u r much concerned about pointers, try using auto_pointers that is the best way to handle memory leaks. but I suggest that not to use auto_pointer because simply u can handle those allocation and deallocation of objects. In MFC, it is not using any special allocation strategy, its all C++. Code Complete 2 is a book which contains some best practices. SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"SaRath C wrote:
In MFC, it is not using any special allocation strategy, its all C++.
That is not entirely accurate. See this MSDN Article[^]
"What classes are you using ? You shouldn't call stuff if you have no idea what it does"
Christian Graus in the C# forumled mike
-
SaRath C wrote:
In MFC, it is not using any special allocation strategy, its all C++.
That is not entirely accurate. See this MSDN Article[^]
"What classes are you using ? You shouldn't call stuff if you have no idea what it does"
Christian Graus in the C# forumled mike
Thanks for ur information. It was quite new for me. Anu said that (he/she) is new to MFC and suffering with some exception and all. Everything has an user point of view. We are the users of C++ right, at our point of view, operator new has a standard form and a predefined way which it should be used. I just indicated about that fact. never I meant any regarding internal handling of compiler. I think the language I used was something not proper :( SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!" -- modified at 1:55 Thursday 1st June, 2006 -
Anu_Bala wrote:
I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?
Well some of the things I know I would like to share. :)
-
Use
new
anddelete
. -
Always use
delete[]
withnew[]
anddelete
withnew
-
Do not mix up c, c++. Either you use C, or use C++ for memory allocation.
-
Always set a pointer to
NULL
after deletion. -
Always allocate sufficient amount of memory. Do not allocate memory like this...
char *str = new char[ strlen( someStr ) ];//stingy
strcpy( str, someStr );// well asking for trouble
delete [] str;//error here
str = NULL;//good practiceinstead use
char *str = new char[ strlen(someStr) + 1 ];//bring it on
strcpy( str, someStr );//gooood, very gooood.
delete [] str;//hehe fine
str = NULL;//good practice -
Try using
auto_ptr
; -
Maintain a list of pointers to memory that you have allocated. When you exit from your app you can delete them all at one go if any of them hasn't been deleted.
-
Always check for
NULL
on pointers after anew
. -
Always check for
NULL
on pointers before using them. -
Be really really afraid to dynamically allocate memory.
Nibu thomas A Developer Programming tips[^] My site[^]
Nibu thomas wrote:
strcpy( str, someStr );// well asking for trouble
True, use
strncpy()
instead.Nibu thomas wrote:
delete [] str;//error here
Why do you assert this is in error?
Nibu thomas wrote:
Be really really afraid to dynamically allocate memory.
Why?
"The largest fire starts but with the smallest spark." - David Crow
-
-
Thanks for ur information. It was quite new for me. Anu said that (he/she) is new to MFC and suffering with some exception and all. Everything has an user point of view. We are the users of C++ right, at our point of view, operator new has a standard form and a predefined way which it should be used. I just indicated about that fact. never I meant any regarding internal handling of compiler. I think the language I used was something not proper :( SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!" -- modified at 1:55 Thursday 1st June, 2006 -
Nibu thomas wrote:
strcpy( str, someStr );// well asking for trouble
True, use
strncpy()
instead.Nibu thomas wrote:
delete [] str;//error here
Why do you assert this is in error?
Nibu thomas wrote:
Be really really afraid to dynamically allocate memory.
Why?
"The largest fire starts but with the smallest spark." - David Crow
DavidCrow wrote:
Nibu thomas wrote: delete [] str;//error here
That was in relation to the point I mentioned. i.e. to allocate sufficient amount of memory. Well if you are using
strcpy
for that purposestrcpy
will put aNULL
char just after the end of the memory block. And when you try todelete
such pointers it will result in memory damaged error message.strcpy
was used here just to regenerate that issue.DavidCrow wrote:
Nibu thomas wrote: Be really really afraid to dynamically allocate memory. Why?
Yeah, use it as a last resort. For eg: using standard stl classes can avoid most of the issues. Note: I didn't say not to use it but instead to be afraid. Sometimes being afraid is good. :)
Nibu thomas A Developer Programming tips[^] My site[^]
-
DavidCrow wrote:
Nibu thomas wrote: delete [] str;//error here
That was in relation to the point I mentioned. i.e. to allocate sufficient amount of memory. Well if you are using
strcpy
for that purposestrcpy
will put aNULL
char just after the end of the memory block. And when you try todelete
such pointers it will result in memory damaged error message.strcpy
was used here just to regenerate that issue.DavidCrow wrote:
Nibu thomas wrote: Be really really afraid to dynamically allocate memory. Why?
Yeah, use it as a last resort. For eg: using standard stl classes can avoid most of the issues. Note: I didn't say not to use it but instead to be afraid. Sometimes being afraid is good. :)
Nibu thomas A Developer Programming tips[^] My site[^]
Nibu thomas wrote:
Well if you are using strcpy for that purpose strcpy will put a NULL char just after the end of the memory block.
But the point is that the source could be longer than the destination, which is why
strcpy()
should not be used, regardless of how it deals with the\0
terminator.Nibu thomas wrote:
For eg: using standard stl classes can avoid most of the issues.
Last time I checked, STL used memory from the heap (i.e., dynamic). There's just not any way around not using memory from the heap.
"The largest fire starts but with the smallest spark." - David Crow
-
Nibu thomas wrote:
Well if you are using strcpy for that purpose strcpy will put a NULL char just after the end of the memory block.
But the point is that the source could be longer than the destination, which is why
strcpy()
should not be used, regardless of how it deals with the\0
terminator.Nibu thomas wrote:
For eg: using standard stl classes can avoid most of the issues.
Last time I checked, STL used memory from the heap (i.e., dynamic). There's just not any way around not using memory from the heap.
"The largest fire starts but with the smallest spark." - David Crow
DavidCrow wrote:
But the point is that the source could be longer than the destination, which is why strcpy() should not be used, regardless of how it deals with the \0 terminator.
Exactly, I was just demonstrating how such things could happen.
DavidCrow wrote:
Last time I checked, STL used memory from the heap (i.e., dynamic). There's just not any way around not using memory from the heap.
Exactly, they work by allocating on the heap. But then we don't have to do it. These are proven classes (AFAIK) which work efficiently. Hence using them would be best for us. These classes takes the onus on them to do memory clean up.
Nibu thomas A Developer Programming tips[^] My site[^]