Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-16-2005, 10:31 AM   #1 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 672
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 06:59 AM. Reason: fixed TCHAR to char
DJMaze is offline   Reply With Quote
Old 09-16-2005, 12:43 PM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 672
DJMaze is on a distinguished road
nvm i'm a real ediot, i've overlooked one stupid line that crashes it completely :p

The above code works flawless there was an issue in my file reader that shifted to the next column (fColCount) to early.

Let me explain:

I read an file where i have each row on a line split with '\n' and each cell data is split with '\0'
The format of a header cell is "Title,150.0Ⓝ" and a function splits on each '\0' however where it needs to shift to the next column ('\0') it uses "++col" but ++col wasn't called on '\0' but right after title.
DJMaze is offline   Reply With Quote
Old 09-17-2005, 06:28 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Good you solved it. However, _fastcall and TCHAR are not part of the standard. Next time I'll move your posts if you do it again... .
__________________
Valmont is offline   Reply With Quote
Old 09-17-2005, 06:59 AM   #4 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 672
DJMaze is on a distinguished road
There fixed TCHAR to char
DJMaze is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ test I made, need one to look over... slashdot Standard C, C++ 3 05-03-2005 11:18 AM
Overloading Operators. MadMAxJr Standard C, C++ 3 02-18-2004 05:22 PM


All times are GMT -8. The time now is 04:18 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting