# Solved: Memory error: Atttempt to access 32 bytes which is 24 bytes long



## rockballad (May 4, 2007)

Hi there,

I'm trying to debug my code, here is the result


```
Build
  [C++ Warning] main.cpp(435): W8004 'fDummyDebug' is assigned a value that is never used
  [C++ Warning] main.cpp(1422): W8004 'uBin' is assigned a value that is never used
  [C++ Warning] main.cpp(1427): W8004 'uBin' is assigned a value that is never used
  [C++ Warning] main.cpp(2776): W8004 'ThisRad' is assigned a value that is never used
  [C++ Warning] main.cpp(3780): W8004 'nRow' is assigned a value that is never used
CodeGuard
  7/23/2007 2:09:18 PM started a CodeGuard(tm) process: TestXP.exe(5832)
  Method called on illegally casted object in process: TestXP.exe(5832)  - I:\TestXP\Test.cpp#473
    Attempt to access 32 byte(s) at 0x0012FB60. The 'this' pointer points to local block 0x0012FB60(=[ebp-0x40] @TestXP.exe:0x01:01A841) which is only 24 bytes long.
      0x005156E4 - I:\TestXP\TestXP.cpp#473
      0x0041B841 - I:\TestXP\main.cpp#3735
      0x5204CB29
      0x52058A3E
      0x5205CA1E
      0x52071252
```
It occurs at:

```
Product prod;
```
while Product is a class like this:


```
class IMEXPORT Product  : public ParamString
{
    private:
        UInt32 numid;//autoincremental
        eParamStatus status; //available, archived
        char* name;//80
        char* bgcolor; //varchar (10)
        char* created;//datetime
        char* blobini;//text

    public:
        Product();
        ~Product();
}
```
Please tell me why it access 32 bytes while the size of Product is only 24 bytes.

Thanks so much in advance.

Best regards,


----------



## Chicon (Jul 29, 2004)

Hi rockballad,

I should focus on this part of the error message :
*Method called on illegally casted object in process: TestXP.exe(5832) - I:\TestXP\Test.cpp#473*
The error may have occured on statement prior to your specified one :
*Product prod;*


----------



## rockballad (May 4, 2007)

Hi, Chicon!

Thanks for your attention. I ever thought so, then I copy that line to somewhere else. But it's still the same.

Now I notice about ParamString class. This class doesn't have any member var. I've tried to add an *int* variable, and the project runs well, doesn't stop for that error any more. The new size of prod is 28, not 32 any more.

Could you tell me why if you have ever deal with it?

Thanks so much!

Best Regards,


----------



## Chicon (Jul 29, 2004)

You're welcome ! 

To answer your question : yes, I've already met a similar situation but it's far in the past and my C/C++ knowledges are a bit rusted now I've turned on Java 4 years ago (with Java, you don't have to manage memory allocations, pointers and references).
I still remember that the size of C++ class objects is sensitive to the order of their data members.
In your example, adding a *int* member in your class seems to have produced an alignment.


----------



## rockballad (May 4, 2007)

Yes, the data alignment. I'm using Borland Dev Studio, setting 8-Byte data alignment as default. 4 bytes of an int will be treated as 8. Guess so, but I still don't know why it's related to the parent class (ParamString). I'll test in another case.

Thanks for discussing with me. Hope meet you again in Java thread, hehe. I'm coding Java too. 

Have a nice day!


----------

