Having problems with dynamic arrays

This is a discussion on Having problems with dynamic arrays within the Windows Programming forums, part of the Platform Specific Boards category; I'm trying to dynamically allocate a 2D array over 2 functions. I can allocate, but cleanup becomes messy. CString **ppStrArray; ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    106

    Having problems with dynamic arrays

    I'm trying to dynamically allocate a 2D array over 2 functions. I can allocate, but cleanup becomes messy.

    CString **ppStrArray;
    // Create a pointer
    ppStrArray = new CString *[];

    int rows, cols;
    rows = cols = 0;

    CreateArray( rows, cols, ppStrArray );

    // Work with array
    :
    :

    // Free memory
    if( ppStrArray )
    {
    for( int i = 0; i < rows; i++ )
    {
    delete [] ppStrArray[i];
    }
    // It has a memory violation here
    delete [] ppStrArray;
    }

    void CreateArray( int &row_count, int &col_count, CString **ppArray )
    {
    // For simplicity I set these, in reality these are determined
    // elsewhere
    row_count = 5;
    col_count = 5;

    if( ppArray )
    {
    for( int row = 0; row < ( int )numRows; row++ )
    {
    ppArray [row] = new CString[numCols];
    }
    }
    return;
    }


    This code works if I change
    ppStrArray = new CString *[];
    to
    ppStrArray = new CString *[5]; // 5 in this example

    But I don't want to hard code the column count, the whole point of a dynamic array is because I don't know the number of columns. (I'm accessing a SQL server2000 database through stored procedures)
    Can any one help?
    Thanks,
    Drewpee

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    instead of hardcoding a size use a variable that you can set elsewhere in the code possibly even by user input.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    Yes, before I call the function to create the array, I run a SQL stored procedure to return rowsets from the database (in yet another function) So I don't know the number of columns untill after. The CreateArray function has access to this number but the calling function doesn't know until after the array is created.
    Is there no way for a function to clean up this array without explicity setting the dimensions itself?
    Seems kinda fishy to me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault AND 2d dynamic arrays
    By lord in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2008, 01:07 PM
  2. Dynamic allocation of 2 dim. arrays in classes
    By circuitbreaker in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2008, 11:13 AM
  3. help with dynamic arrays
    By Chaplin27 in forum C++ Programming
    Replies: 5
    Last Post: 09-03-2004, 08:22 PM
  4. Dynamic Arrays?
    By leeor_net in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:24 PM
  5. dynamic memory and arrays
    By jomns in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2004, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21