CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    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++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 4th, 2009, 08:54 PM
    melawrghk melawrghk is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    melawrghk is an unknown quantity at this point (<10)
    Question Problem with "delete[]"

    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;
        }
    
    }
    The function is supposed to resize the array to the specified length and copy known elements into it. I don't really get why it's not working, it compiles fine, so I guess it's not my syntax, but I don't know.

    Thanks in advance!
    Reply With Quote
      #2    
    Old November 4th, 2009, 09:21 PM
    Lindley Lindley is offline
    Elite Member
    Power Poster
     
    Join Date: Oct 2007
    Location: Fairfax, VA
    Posts: 6,522
    Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)
    Re: Problem with "delete[]"

    Code:
    new_array=new EType(new_size);
    This does not construct an array of EType, it constructs a single EType using the constructor which takes an int argument.

    By the way, is there a reason you aren't just using a std::vector<EType>?
    Reply With Quote
      #3    
    Old November 4th, 2009, 09:37 PM
    melawrghk melawrghk is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    melawrghk is an unknown quantity at this point (<10)
    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!
    Reply With Quote
      #4    
    Old November 5th, 2009, 12:39 AM
    Paul McKenzie Paul McKenzie is online now
    Elite Member
    Power Poster
     
    Join Date: Apr 1999
    Posts: 20,406
    Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)
    Re: Problem with "delete[]"

    Quote:
    Originally Posted by melawrghk View Post
    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.
    Why are you allocating twice? All you need is one allocation.
    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;*/
    Regards,

    Paul McKenzie
    Reply With Quote
      #5    
    Old November 5th, 2009, 05:33 AM
    JohnW@Wessex's Avatar
    JohnW@Wessex JohnW@Wessex is offline
    Senior Member
     
    Join Date: Jul 2002
    Location: Southampton. United Kingdom
    Posts: 1,954
    JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)
    Re: Problem with "delete[]"

    Quote:
    Originally Posted by melawrghk View Post
    I'm not using a vector because the lab focuses just on dynamic arrays and we're restricted to using those.
    But std::vector is a dynamic array
    __________________
    "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
    Reply With Quote
      #6    
    Old November 5th, 2009, 06:16 AM
    superbonzo superbonzo is offline
    Member
     
    Join Date: Oct 2008
    Posts: 149
    superbonzo is on a distinguished road (30+)
    Re: Problem with "delete[]"

    Quote:
    Originally Posted by JohnW@Wessex View Post
    But std::vector is a dynamic array
    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 ?
    Reply With Quote
      #7    
    Old November 5th, 2009, 06:39 AM
    Paul McKenzie Paul McKenzie is online now
    Elite Member
    Power Poster
     
    Join Date: Apr 1999
    Posts: 20,406
    Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)
    Re: Problem with "delete[]"

    Quote:
    Originally Posted by superbonzo View Post
    you're maybe they think that the proper design of such a class/template is a newbie-affordable task ?
    The only reason that I can see is that the teachers are trying to weed out the potential good programmers from the weak ones, so that the weak ones drop the course. So they introduce (which IMO, doesn't need to be introduced) coding all sorts of things, asking the students to reinvent the wheel.

    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
    Reply With Quote
      #8    
    Old November 5th, 2009, 07:09 AM
    JohnW@Wessex's Avatar
    JohnW@Wessex JohnW@Wessex is offline
    Senior Member
     
    Join Date: Jul 2002
    Location: Southampton. United Kingdom
    Posts: 1,954
    JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)
    Re: Problem with "delete[]"

    Quote:
    Originally Posted by Paul McKenzie View Post
    Anyway, the curriculum should be focusing on proper usage of the standard library, algorithms, etc.
    I agree, I've always thought that the better way would be to get the student used to many different types of standard containers & algorithms, and then later on, for the more advanced students, posing the question "If you were to implement a vector/string/map style class, how would you go about it?"

    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
    Reply With Quote
      #9    
    Old November 5th, 2009, 11:04 AM
    Paul McKenzie Paul McKenzie is online now
    Elite Member
    Power Poster
     
    Join Date: Apr 1999
    Posts: 20,406
    Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)
    Re: Problem with "delete[]"

    Quote:
    Originally Posted by JohnW@Wessex View Post
    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!".
    Maybe, but the problem with this is that the "let's make them do this hard stuff first" type of teaching discourages many students, maybe turning them off from C++, and possibly programming in general.

    Regards,

    Paul McKenzie
    Reply With Quote
      #10    
    Old November 5th, 2009, 11:36 AM
    JohnW@Wessex's Avatar
    JohnW@Wessex JohnW@Wessex is offline
    Senior Member
     
    Join Date: Jul 2002
    Location: Southampton. United Kingdom
    Posts: 1,954
    JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)JohnW@Wessex is a glorious beacon of light (400+)
    Re: Problem with "delete[]"

    Quote:
    Originally Posted by Paul McKenzie View Post
    ... type of teaching discourages many students, maybe turning them off from C++,...
    That's probably why all the C++ critics constantly go on about the lack of garbage collection. They bail out of C++ when it's being taught like C and think that's all there is to it.

    (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
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:01 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009