[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I deallocate a two-dimensional vector (STL) of wchar_t* type

Posted on 2005-05-03
8
Medium Priority
?
627 Views
Last Modified: 2013-12-14
I'm reading in a file, line by line.  Each line contains values delimited by a colon.  I won't know how many lines are going to be in the file, but I DO know how many values are going to be delimited for each line.  After tokenizing each line, I'm trying to store the values in a two-dimensional array using STL's <vector>.  Each entry in the array contains another vector array of wide char pointers that will each point to a newly allocated wide char string.  I'm having trouble deallocating the vector.  Currently, I have something like the following.

// Channels is defined like this...
vector< vector<wchar_t*> >  Channels;

// Reading in the data goes something like this...

    // Read data in, line by line.
    while ( ReadLine(hFile, szwLine) > 0 ) {
       
        if ( wcsstr(szwLine, L"CHANNEL=") != NULL ) {

            // Create a new index for Channel array.
            g_stAI.Channels.push_back( vector<wchar_t*>() );

            // Determine if line read in is a channel place holder.
            if ( wcsstr(szwLine, L"PLACEHOLDER") != NULL ) {
                pTemp = new wchar_t[wcslen(L"PLACEHOLDER")*sizeof(wchar_t)+1];
                wcscpy( pTemp, L"PLACEHOLDER" );
                g_stAI.Channels[iCount].push_back( pTemp );
               
            } else {
                // Parse data
                pDummy       = wcstok( szwLine, L"=" );
                pChanName    = wcstok( NULL,    L":" );
                pChanID      = wcstok( NULL,    L":" );
                pChanUseHigh = wcstok( NULL,    L":" );
                pChanHighVal = wcstok( NULL,    L":" );
                pChanUseLow  = wcstok( NULL,    L":" );
                pChanLowVal  = wcstok( NULL,    L":" );

                pTemp = new wchar_t[wcslen(pChanName)*sizeof(wchar_t)+1];
                wcscpy( pTemp, pChanName );
                g_stAI.Channels[iCount].push_back( pTemp );

                pTemp = new wchar_t[wcslen(pChanID)*sizeof(wchar_t)+1];
                wcscpy( pTemp, pChanID );
                g_stAI.Channels[iCount].push_back( pTemp );

                pTemp = new wchar_t[wcslen(pChanUseHigh)*sizeof(wchar_t)+1];
                wcscpy( pTemp, pChanUseHigh );
                g_stAI.Channels[iCount].push_back( pTemp );

                pTemp = new wchar_t[wcslen(pChanHighVal)*sizeof(wchar_t)+1];
                wcscpy( pTemp, pChanHighVal );
                g_stAI.Channels[iCount].push_back( pTemp );

                pTemp = new wchar_t[wcslen(pChanUseLow)*sizeof(wchar_t)+1];
                wcscpy( pTemp, pChanUseLow );
                g_stAI.Channels[iCount].push_back( pTemp );

                pTemp = new wchar_t[wcslen(pChanLowVal)*sizeof(wchar_t)+1];
                wcscpy( pTemp, pChanLowVal );
                g_stAI.Channels[iCount].push_back( pTemp );
            }
            iCount++;
        }
    }


When I'm ready to deallocate all values in the vector, how would I go about it?  If someone could help me out, I would surely appreciate it.  If possible, please post code for the deallocation.  Or if anyone knows of a simpler way to do this, please let me know.

Any help would be much appreciated.
Thanks
0
Comment
Question by:DataField
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13919769
You'd use

vector< vector<wchar_t*> >  Channels;

vector< vector<wchar_t*> >::iterator iv;

for ( iv = Channels.begin(); iv != Channels.end(); ++iv) {

    vector<wchar_t*>::iterator i;
    for ( i = iv->begin(); i != iv->end(); ++i) delete [] (*i);
}

to depete all the memory you have allocated.
0
 
LVL 86

Expert Comment

by:jkr
ID: 13919787
To elaborate: Thze outer loop goes through all the 'vector<wchar_t*>' vectors stored in 'Channels', then the inner loop iterates each entry and calls 'delete[]' for each allocated element.
0
 

Author Comment

by:DataField
ID: 13919910
Thanks so much.  That'll work!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:Axter
ID: 13920050
IMHO, a better approach would be to use a vector<vector<wstring> >
or vector<vector<vector<wchar_t> > >

Either of these two methods would be safer, and would not require you to delete the string for cleanup.
0
 

Author Comment

by:DataField
ID: 13921674
If I used...
vector<vector<vector<wchar_t> > >

I'd have to push each individual character though, right?
0
 

Author Comment

by:DataField
ID: 13921807
Oh, I see...
vector< vector <wstring> > would seem to work, but how would I be able to asign any of the strings, which are pointers?

I don't think that the following would work.
g_stAI.Channels[iCount].push_back( *pChanName );
0
 
LVL 86

Expert Comment

by:jkr
ID: 13921839
If you're using a 'wstring', it should work. However, the 'usefulness' of switching to 'wstring' depends on what your code is doing with the data .
0
 
LVL 30

Expert Comment

by:Axter
ID: 13923460
>>I'd have to push each individual character though, right?

No.  You can add a string by calling assign method.
vector<wchar_t> arr;
arr.assign(my_str, my_str+len);
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question