| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi everyone, I'm having some problems with deallocating memory on the heap. Here is a chunk of my code:
Code:
void DynArray::resize(int new_size){
assert(new_size>=0);
EType *new_array;
new_array=new EType(new_size);
int i=0;
if (new_size<=sizeM){
while (i<new_size){
new_array[i]=storageM[i];
i++;
}
delete [] storageM;
storageM=new EType[new_size];
for(i=0; i<new_size; i++){
storageM[i]=new_array[i];
}
delete [] new_array;
}
else if (new_size>sizeM){
while (i<sizeM){
new_array[i]=storageM[i];
i++;}
delete[] storageM; //This line causes problems
cout<<"1";
storageM=new EType[new_size];
for(i=0; i<new_size; i++){
storageM[i]=new_array[i];
}
cout<<"2";
sizeM=new_size;
cout<<"3";
delete[] new_array;
}
}
Thanks in advance! |
|
#2
|
|||
|
|||
|
Re: Problem with "delete[]"
Code:
new_array=new EType(new_size); By the way, is there a reason you aren't just using a std::vector<EType>? |
|
#3
|
|||
|
|||
|
Re: Problem with "delete[]"
Oh my goodness, I can't believe I did that. And overlooked it many times...
I'm not using a vector because the lab focuses just on dynamic arrays and we're restricted to using those. Thanks a lot! |
|
#4
|
|||
|
|||
|
Re: Problem with "delete[]"
Quote:
Code:
new_array=new EType[new_size];
int i=0;
if (new_size<=sizeM){
while (i<new_size){
new_array[i]=storageM[i];
i++;
}
delete [] storageM;
storageM = new_array;
// None of this stuff below is necessary
/* storageM=new EType[new_size];
for(i=0; i<new_size; i++){
storageM[i]=new_array[i];
}
delete [] new_array;*/
Paul McKenzie |
|
#5
|
||||
|
||||
|
Re: Problem with "delete[]"
Quote:
__________________
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong." Richard P. Feynman |
|
#6
|
|||
|
|||
|
Re: Problem with "delete[]"
these teachers are very surprising... I mean, it's reasonable to teach how arrays work but why do they insist in asking to code std::vector like classes/templates ? maybe they think that the proper design of such a class/template is a newbie-affordable task ?
|
|
#7
|
|||
|
|||
|
Re: Problem with "delete[]"
Quote:
Anyway, the curriculum should be focusing on proper usage of the standard library, algorithms, etc. following somewhat in the way books such as Accelerated C++ approach learning the language. C++ has grown beyond the bounds of what the usual C++ curriculum looked like 12 or 15 years ago. You didn't have sort(), std::vector, reverse(), iterators, etc. back in those days, so back then, you had the traditional "write a string class..." and "write a function to reverse...", and "write a dynamic array..." assignments. Unfortunately, that very same curriculum has not changed in many teaching environments. The teachers and/or the ones responsible for the curriculum still believe that it's 1990. My belief is that the curriculum needs to be changed to reflect what the C++ language means today. You can still have a good C++ beginner class that focuses on the fundamentals, and give good, meaningful assignments that stress usage of the containers and algorithms. Regards, Paul McKenzie |
|
#8
|
||||
|
||||
|
Re: Problem with "delete[]"
Quote:
The only advantage I can see to introducing home made containers early on is that when you get to use the STL you think, "Thank God I don't have to do all that new/delete/pointer code any more!".
__________________
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong." Richard P. Feynman |
|
#9
|
|||
|
|||
|
Re: Problem with "delete[]"
Quote:
Regards, Paul McKenzie |
|
#10
|
||||
|
||||
|
Re: Problem with "delete[]"
Quote:
(To any Java programmers reading this who are learning C++; Once you start coding in modern C++, garbage collection & manual memory management largely become non-issues)
__________________
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong." Richard P. Feynman |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|