Thread: Memory issues in C++ vectors compared to ordinary arrays

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User FortranLevelC++'s Avatar
    Join Date
    May 2013
    Location
    United States
    Posts
    81

    Memory issues in C++ vectors compared to ordinary arrays

    First of all, many thanks for your C++ book that I have been reading. For two decades I have been using C++ as if were Fortran, only to multiply arrays and matrices for numerical computation. I did accumulate nearly two dozens of C++ books, but I have never used any serious data structures. Thanks to the concrete motivation in your book, I am already using more serious capabilities of C++, and the results are very productive.

    I have already started to use vectors quite successfully, but I have the following two questions about the memory management of vectors.

    1) If I understand what I have been reading, in general even the clear() method applied to a vector does not free the memory that is already occupied by the vector, it merely erases its contents. If I am correct, then this is unfortunate, and this is in contrast with the memory taken by an array that can be totally freed by using "delete." What can I do to make sure that this memory aspect of the vector data structure does not cause trouble? At least what happens when the entire program terminates, after the program terminates can the memory of a vector cause problems?

    2) I have written and tested the following code where I am passing a vector whose elements are structures, as an argument to a function that modifies the vector and returns this modified vector. Below, I have listed the entire program that is self-contained and it is working. The code seems to work, but I am surprised because I did not expect the following signature of the calling function to be adequate (surprisingly it is working):

    void tester(vector<Item >& );

    Instead, I was expecting that I should have written something like this (with * for the type inside the vector) as the signature of the calling function:

    void tester(vector<Item * >& );

    The reason I am puzzled is because the elements of the vector are not simple things like int or double, they are structures that may require an additional pointer when the vector is passed as an argument to the function.

    Do you think that there is a hidden problem in my code even though it is actually compiling and executing without error?

    In any case, I can see that even for numerical computation, vector and map data structures can be very useful in order to control the architecture and logical behavior of the program. For this reason I will appreciate if you write a section in this website dedicated to advanced uses of vectors, how to create a vector of functions (I think I know how to create an array of functions, but creating a vector of functions that will change real time seems more difficult), and how to pass a vector of functions as an argument to a function. Also, the possibility of creating a vector whose elements are classes (not just structures, but with each class having its specialized function), to pass these vectors as arguments to a function, can be extremely useful in practical applications. I hope that you can write a chapter on this subject.

    // MY ENTIRE CODE IS LISTED BELOW:

    Code:
    #include <stdio.h>      
    #include <stdlib.h>  
    #include <iomanip>
    #include <fstream>
    #include <iostream>
     #include <cstring>
    #include <sstream>
    #include <string>
    #include <string.h>
    
    #include <vector>
    #include <map>
    
    using namespace std;
    
    
    struct Item{
            string Word;
            int Number;
        };
    
    
    void tester(vector<Item >& );  // Signature of the calling function below
    
    
    int main() 
    {
       vector<Item> Exper;
       Item junk1, junk2, junk3;
       int temp;
    
    
        junk1.Word = "a"; 
        junk1.Number = 0;
        junk2.Word = "b";
        junk2.Number = 1;
        junk3.Word = "c";
        junk3.Number = 2;
        
        
         Exper.push_back(junk1);
        Exper.push_back(junk2);
        Exper.push_back(junk3);
    
    
            cout << "Before tester function is called we get: " << endl;
         
        int iterationCounter =0;
            
        for(vector<Item>::iterator itr=Exper.begin(), end=Exper.end(); itr != end; itr++)
        { 
          // cout << *itr << endl;
          /* std::cout << *itr << endl; // this causes a compiler error maybe because the type Item 
            in the vector is not an integer to point to, but the rest of the code works
             when we use an artificial iteration counter called "iteractionCounter" below: */
          cout << Exper[iterationCounter].Word << " " << Exper[iterationCounter].Number << endl;
          ++iterationCounter;
        }
    
      
        tester(Exper); // calling the test function modifying the vector Exper
    
            cout << "After tester function is called we get: " << endl;
         iterationCounter =0;
         for(vector<Item>::iterator itr=Exper.begin(), end=Exper.end();  itr != end; ++itr )
        {
          cout << Exper[iterationCounter].Word << " " << Exper[iterationCounter].Number << endl;
          ++iterationCounter;
        }
            
        
        cout << "Enter any integer to stop program: " ;
            cin >> temp;
    
    
    }
    
    
    void tester( vector<Item > & s)
    {
        s[0].Word = "mod0";
        s[0].Number = 10;
    }
    Last edited by FortranLevelC++; 05-09-2013 at 12:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors and memory
    By Matty_Alan in forum C++ Programming
    Replies: 5
    Last Post: 05-22-2010, 08:01 PM
  2. Vectors in Arrays
    By Osiris990 in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2008, 02:48 PM
  3. Arrays vs Vectors
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 02:06 AM
  4. arrays or vectors
    By Geo-Fry in forum C++ Programming
    Replies: 26
    Last Post: 04-17-2003, 07:08 PM
  5. arrays and vectors
    By volk in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2003, 03:45 PM