View Single Post
Old 09-16-2005, 11:31 AM   #1 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 734
DJMaze is on a distinguished road
Reference/Dereference Operators

I know there are:
& cast-expression
* cast-expression

And you can do things like:
Code:
char **array;
But somehow i have memory allocation problems.
I have a multidimensional array like:
Code:
┌──────┬──────┬──────┐
│string│string│string│ <= headers
├──────┼──────┼──────┤
│string│string│string│ <= cells
├──────┼──────┼──────┤
│string│string│string│ <= cells
└──────┴──────┴──────┘
but instead of strings i use classes and structures
PHP Code:
enum TColTypes ctStringctIntegerctTime };

typedef struct Header {
  
TColTypes Type;
  
short Width;
  
char *Text;
} * 
PHeader;

typedef struct Cell {
  
char *Text;
} * 
PCell;

class 
GridRow
{
  private:
    
Cell **Cells;
}

class 
Grid
{
  private:
    
Header **Headers;
    
GridRow **Rows;

as you can see it has an array of headers like:
PHP Code:
Headers[0] = new Header
and an array of rows that contains cells
PHP Code:
Rows[0] = new GridRow;
Rows[0]->Cells[0] = new Cell
But whenever i try to realloc() the size of Headers and it seems i'm forced to recreate tmpHeaders and copy the data from Headers.
Sadly this is not an option because it could deal with 100MB of data and then it will be a pain and slow process.
So i've tried to use the following which somehow fails to copy the data properly.
PHP Code:
void __fastcall Grid::SetColCount(int Value)
{
  if (
Value 0Value 0;
  if (
Value != fColCount) {
    
// Destroy header collumns if the size gets less
    
if (Value fColCount) {
      for (
int c=Valuec<fColCount; ++c) { delete Headers[c]; }
    }
    
// A pointer has sizeof = 4
    
Headers = (Header **)realloc(HeadersValue 4); // <= issue here due to ** ?
    // Create header collumns if the size gets bigger
    
if (Value fColCount) {
      for (
int c=fColCountc<Value; ++c) { Headers[c] = new Header; }
    }
    
fColCount Value;
  }

I use pointers instead of something like: Header *Headers[10]; because it is very dynamic and column swapping also occures.

Could someone explain me what goes wrong ?

Last edited by DJMaze; 09-17-2005 at 07:59 AM. Reason: fixed TCHAR to char
DJMaze is offline   Reply With Quote