Re: [DUG] Sizeof record gives error

2011-08-25 Thread Rohit Gupta

David,

It looks like a compiler bug to me.  Its treating the array as size 0.

My suggestion is to use a class/Tobject instead of record.

Rohit

On 25/08/2011 11:40 a.m., David Moorhouse (DUG) wrote:

I have the following code snippet

code
type
   PConstArray = ^TConstArray;
   TConstArray = array of TVarRec;

function CreateConstArray(const Elements: array of const): TConstArray;

type
   TLogType = (ltError, ltWarn, ltInfo);
   PLogData = ^TLogData;
   TLogData = record
 LogType: TLogType;
 LogArgs: TConstArray;
   end;



procedure TUserClass.Log(const LogType: TLogType; const Args: array of
const );
var
   LogData: PLogData;
begin
 // size of record TLogData does not work
 GetMem(LogData, sizeof(TLogData));
 LogData.LogType := LogType;
// blows up on next line
 LogData.LogArgs := CreateConstArray(Args);
//  ... do some other stuff with the LogData item finally calling FreeMem
end;

function CreateConstArray(const Elements: array of const): TConstArray;
var
   I: Integer;
begin
   SetLength(Result, Length(Elements));
   for I := Low(Elements) to High(Elements) do
 Result[I] :=  // assign a TVarRec here
end;
/code

The code that assigns the memory only assigns 8 bytes - and an access
violation ensues.  If I replace the call to sizeof with the number 16,
the code works fine.

My understanding of dynamic arrays was that the compiler created a 4 byte
field before the first element that contained the length of the array.

So why does the sizeof  function not reflect this ?  And why do I need 16
bytes not 12  (4 for LogType + 4 for length of array + 4 for array
pointer)?
Also regardless of the number of items in the open array parameter, 16
bytes works, so it does not relate the length of the TConstArray.

Your thoughts ?

David



___
NZ Borland Developers Group - Delphi mailing list
Post: delphi@delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: 
unsubscribe

__ Information from ESET NOD32 Antivirus, version of virus signature 
database 6407 (20110824) __

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com


__ Information from ESET NOD32 Antivirus, version of virus signature 
database 6407 (20110824) __

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com







--
Regards

*Rohit Gupta*
B.E. Elec., M.E., Mem IEEE, Member IET
Technical Manager
Computer Fanatics Ltd

*Tel *4892280
*Fax *4892290
*Web *www.cfl.co.nz

This email and any attachments contain information, which is 
confidential and may be subject to legal privilege and copyright. If you 
are not the intended recipient, you must not use, distribute or copy 
this email or attachments. If you have received this in error, please 
notify us immediately by return email and then delete this email and any 
attachments.
___
NZ Borland Developers Group - Delphi mailing list
Post: delphi@delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: 
unsubscribe

Re: [DUG] Sizeof record gives error

2011-08-24 Thread Colin Johnsun
Haven't got into the details of your problem, but here is an article
which goes into the details
of array of const.

http://rvelthuis.de/articles/articles-openarr.html

Hope it helps,

Cheers,
Colin

On 25 August 2011 09:40, David Moorhouse (DUG) del...@moorhouse.net.nz wrote:
 I have the following code snippet

 code
 type
  PConstArray = ^TConstArray;
  TConstArray = array of TVarRec;

 function CreateConstArray(const Elements: array of const): TConstArray;

 type
  TLogType = (ltError, ltWarn, ltInfo);
  PLogData = ^TLogData;
  TLogData = record
    LogType: TLogType;
    LogArgs: TConstArray;
  end;

 

 procedure TUserClass.Log(const LogType: TLogType; const Args: array of
 const );
 var
  LogData: PLogData;
 begin
    // size of record TLogData does not work
    GetMem(LogData, sizeof(TLogData));
    LogData.LogType := LogType;
 // blows up on next line
    LogData.LogArgs := CreateConstArray(Args);
 //  ... do some other stuff with the LogData item finally calling FreeMem
 end;

 function CreateConstArray(const Elements: array of const): TConstArray;
 var
  I: Integer;
 begin
  SetLength(Result, Length(Elements));
  for I := Low(Elements) to High(Elements) do
    Result[I] :=  // assign a TVarRec here
 end;
 /code

 The code that assigns the memory only assigns 8 bytes - and an access
 violation ensues.  If I replace the call to sizeof with the number 16,
 the code works fine.

 My understanding of dynamic arrays was that the compiler created a 4 byte
 field before the first element that contained the length of the array.

 So why does the sizeof  function not reflect this ?  And why do I need 16
 bytes not 12  (4 for LogType + 4 for length of array + 4 for array
 pointer)?
 Also regardless of the number of items in the open array parameter, 16
 bytes works, so it does not relate the length of the TConstArray.

 Your thoughts ?

 David



 ___
 NZ Borland Developers Group - Delphi mailing list
 Post: delphi@delphi.org.nz
 Admin: http://delphi.org.nz/mailman/listinfo/delphi
 Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: 
 unsubscribe


___
NZ Borland Developers Group - Delphi mailing list
Post: delphi@delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: 
unsubscribe


Re: [DUG] Sizeof record gives error

2011-08-24 Thread Robo
I think your TLogData contains storage for an integer (TLogType, 4 bytes),
and a pointer to an array (4 bytes).

However, what you need is enough memory to store the entire array, not just
the pointer, so you should get the size of the array to get the correct
size.

On Thu, Aug 25, 2011 at 11:40 AM, David Moorhouse (DUG) 
del...@moorhouse.net.nz wrote:

 I have the following code snippet

 code
 type
  PConstArray = ^TConstArray;
  TConstArray = array of TVarRec;

 function CreateConstArray(const Elements: array of const): TConstArray;

 type
  TLogType = (ltError, ltWarn, ltInfo);
  PLogData = ^TLogData;
  TLogData = record
LogType: TLogType;
LogArgs: TConstArray;
  end;

 

 procedure TUserClass.Log(const LogType: TLogType; const Args: array of
 const );
 var
  LogData: PLogData;
 begin
// size of record TLogData does not work
GetMem(LogData, sizeof(TLogData));
LogData.LogType := LogType;
 // blows up on next line
LogData.LogArgs := CreateConstArray(Args);
 //  ... do some other stuff with the LogData item finally calling FreeMem
 end;

 function CreateConstArray(const Elements: array of const): TConstArray;
 var
  I: Integer;
 begin
  SetLength(Result, Length(Elements));
  for I := Low(Elements) to High(Elements) do
Result[I] :=  // assign a TVarRec here
 end;
 /code

 The code that assigns the memory only assigns 8 bytes - and an access
 violation ensues.  If I replace the call to sizeof with the number 16,
 the code works fine.

 My understanding of dynamic arrays was that the compiler created a 4 byte
 field before the first element that contained the length of the array.

 So why does the sizeof  function not reflect this ?  And why do I need 16
 bytes not 12  (4 for LogType + 4 for length of array + 4 for array
 pointer)?
 Also regardless of the number of items in the open array parameter, 16
 bytes works, so it does not relate the length of the TConstArray.

 Your thoughts ?

 David



 ___
 NZ Borland Developers Group - Delphi mailing list
 Post: delphi@delphi.org.nz
 Admin: http://delphi.org.nz/mailman/listinfo/delphi
 Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject:
 unsubscribe

___
NZ Borland Developers Group - Delphi mailing list
Post: delphi@delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: 
unsubscribe

Re: [DUG] Sizeof record gives error

2011-08-24 Thread David Moorhouse (DUG)
Thanks Colin

The TVarRec and open array stuff come from Rudy's page - very helpful too :)

D


 Haven't got into the details of your problem, but here is an article
 which goes into the details
 of array of const.

 http://rvelthuis.de/articles/articles-openarr.html

 Hope it helps,

 Cheers,
 Colin

 On 25 August 2011 09:40, David Moorhouse (DUG) del...@moorhouse.net.nz
 wrote:
 I have the following code snippet

 code
 type
  PConstArray = ^TConstArray;
  TConstArray = array of TVarRec;

 function CreateConstArray(const Elements: array of const): TConstArray;

 type
  TLogType = (ltError, ltWarn, ltInfo);
  PLogData = ^TLogData;
  TLogData = record
    LogType: TLogType;
    LogArgs: TConstArray;
  end;

 

 procedure TUserClass.Log(const LogType: TLogType; const Args: array of
 const );
 var
  LogData: PLogData;
 begin
    // size of record TLogData does not work
    GetMem(LogData, sizeof(TLogData));
    LogData.LogType := LogType;
 // blows up on next line
    LogData.LogArgs := CreateConstArray(Args);
 //  ... do some other stuff with the LogData item finally calling
 FreeMem
 end;

 function CreateConstArray(const Elements: array of const): TConstArray;
 var
  I: Integer;
 begin
  SetLength(Result, Length(Elements));
  for I := Low(Elements) to High(Elements) do
    Result[I] :=  // assign a TVarRec here
 end;
 /code

 The code that assigns the memory only assigns 8 bytes - and an access
 violation ensues.  If I replace the call to sizeof with the number 16,
 the code works fine.

 My understanding of dynamic arrays was that the compiler created a 4
 byte
 field before the first element that contained the length of the array.

 So why does the sizeof  function not reflect this ?  And why do I need
 16
 bytes not 12  (4 for LogType + 4 for length of array + 4 for array
 pointer)?
 Also regardless of the number of items in the open array parameter, 16
 bytes works, so it does not relate the length of the TConstArray.

 Your thoughts ?

 David



 ___
 NZ Borland Developers Group - Delphi mailing list
 Post: delphi@delphi.org.nz
 Admin: http://delphi.org.nz/mailman/listinfo/delphi
 Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject:
 unsubscribe


 ___
 NZ Borland Developers Group - Delphi mailing list
 Post: delphi@delphi.org.nz
 Admin: http://delphi.org.nz/mailman/listinfo/delphi
 Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject:
 unsubscribe




___
NZ Borland Developers Group - Delphi mailing list
Post: delphi@delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: 
unsubscribe


Re: [DUG] Sizeof record gives error

2011-08-24 Thread David Moorhouse (DUG)
Thanks Robo

In my mind the memory layout looks like this (attempt at ascii art follows)

TLogData
 - LogType = 4 bytes
 - LogArgs -   TConstArray - length = 4 bytes
- data pointer = 4 bytes

Regardless of the Length of the array, if I allocate 16 bytes the code
works fine.  If I allocate 12 (as above) it crashes.


Cheers

D


 I think your TLogData contains storage for an integer (TLogType, 4 bytes),
 and a pointer to an array (4 bytes).

 However, what you need is enough memory to store the entire array, not
 just
 the pointer, so you should get the size of the array to get the correct
 size.

 On Thu, Aug 25, 2011 at 11:40 AM, David Moorhouse (DUG) 
 del...@moorhouse.net.nz wrote:

 I have the following code snippet

 code
 type
  PConstArray = ^TConstArray;
  TConstArray = array of TVarRec;

 function CreateConstArray(const Elements: array of const): TConstArray;

 type
  TLogType = (ltError, ltWarn, ltInfo);
  PLogData = ^TLogData;
  TLogData = record
LogType: TLogType;
LogArgs: TConstArray;
  end;

 

 procedure TUserClass.Log(const LogType: TLogType; const Args: array of
 const );
 var
  LogData: PLogData;
 begin
// size of record TLogData does not work
GetMem(LogData, sizeof(TLogData));
LogData.LogType := LogType;
 // blows up on next line
LogData.LogArgs := CreateConstArray(Args);
 //  ... do some other stuff with the LogData item finally calling
 FreeMem
 end;

 function CreateConstArray(const Elements: array of const): TConstArray;
 var
  I: Integer;
 begin
  SetLength(Result, Length(Elements));
  for I := Low(Elements) to High(Elements) do
Result[I] :=  // assign a TVarRec here
 end;
 /code

 The code that assigns the memory only assigns 8 bytes - and an access
 violation ensues.  If I replace the call to sizeof with the number 16,
 the code works fine.

 My understanding of dynamic arrays was that the compiler created a 4
 byte
 field before the first element that contained the length of the array.

 So why does the sizeof  function not reflect this ?  And why do I need
 16
 bytes not 12  (4 for LogType + 4 for length of array + 4 for array
 pointer)?
 Also regardless of the number of items in the open array parameter, 16
 bytes works, so it does not relate the length of the TConstArray.

 Your thoughts ?

 David



 ___
 NZ Borland Developers Group - Delphi mailing list
 Post: delphi@delphi.org.nz
 Admin: http://delphi.org.nz/mailman/listinfo/delphi
 Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject:
 unsubscribe

 ___
 NZ Borland Developers Group - Delphi mailing list
 Post: delphi@delphi.org.nz
 Admin: http://delphi.org.nz/mailman/listinfo/delphi
 Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject:
 unsubscribe



___
NZ Borland Developers Group - Delphi mailing list
Post: delphi@delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: 
unsubscribe


Re: [DUG] Sizeof record gives error

2011-08-24 Thread kaller


sure ok. 

On Thu, 25 Aug 2011 11:57:26 +1200, Robo  wrote:  

I think
your TLogData contains storage for an integer (TLogType, 4 bytes), and a
pointer to an array (4 bytes).

However, what you need is enough memory to
store the entire array, not just the pointer, so you should get the size of
the array to get the correct size. On Thu, Aug 25, 2011 at 11:40 AM, David
Moorhouse (DUG)  wrote:
 I have the following code snippet

type

PConstArray = ^TConstArray;
 TConstArray = array of TVarRec;

function
CreateConstArray(const Elements: array of const): TConstArray;

type

TLogType = (ltError, ltWarn, ltInfo);
 PLogData = ^TLogData;
 TLogData =
record
 LogType: TLogType;
 LogArgs: TConstArray;
 end;



procedure
TUserClass.Log(const LogType: TLogType; const Args: array of
const );
var

LogData: PLogData;
begin
 // size of record TLogData does not work

GetMem(LogData, sizeof(TLogData));
 LogData.LogType := LogType;
// blows up
on next line
 LogData.LogArgs := CreateConstArray(Args);
// ... do some
other stuff with the LogData item finally calling FreeMem
end;

function
CreateConstArray(const Elements: array of const): TConstArray;
var
 I:
Integer;
begin
 SetLength(Result, Length(Elements));
 for I :=
Low(Elements) to High(Elements) do
 Result[I] := // assign a TVarRec
here
end;

The code that assigns the memory only assigns 8 bytes - and an
access
violation ensues. If I replace the call to sizeof with the number
16,
the code works fine.

My understanding of dynamic arrays was that the
compiler created a 4 byte
field before the first element that contained the
length of the array.

So why does the sizeof function not reflect this ?
And why do I need 16
bytes not 12 (4 for LogType + 4 for length of array +
4 for array
pointer)?
Also regardless of the number of items in the open
array parameter, 16
bytes works, so it does not relate the length of the
TConstArray.

Your thoughts
?

David

___
NZ Borland
Developers Group - Delphi mailing list
Post: delphi@delphi.org.nz
[2]
Admin: http://delphi.org.nz/mailman/listinfo/delphi [3]
Unsubscribe:
send an email to delphi-requ...@delphi.org.nz [4] with Subject:
unsubscribe

 

Links:
--
[1] mailto:del...@moorhouse.net.nz
[2]
mailto:delphi@delphi.org.nz
[3]
http://delphi.org.nz/mailman/listinfo/delphi
[4]
mailto:delphi-requ...@delphi.org.nz
___
NZ Borland Developers Group - Delphi mailing list
Post: delphi@delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: 
unsubscribe

Re: [DUG] Sizeof record gives error

2011-08-24 Thread Jeremy Coulter
Just hard code 16 in David and we can discuss it we we do a code review
:-)

On Thu, Aug 25, 2011 at 12:36 PM, kal...@ihug.co.nz wrote:

 sure ok.







 On Thu, 25 Aug 2011 11:57:26 +1200, Robo robo...@gmail.com wrote:

 I think your TLogData contains storage for an integer (TLogType, 4 bytes),
 and a pointer to an array (4 bytes).

 However, what you need is enough memory to store the entire array, not just
 the pointer, so you should get the size of the array to get the correct
 size.
 On Thu, Aug 25, 2011 at 11:40 AM, David Moorhouse (DUG) 
 del...@moorhouse.net.nz wrote:

 I have the following code snippet


 type
  PConstArray = ^TConstArray;
  TConstArray = array of TVarRec;

 function CreateConstArray(const Elements: array of const): TConstArray;

 type
  TLogType = (ltError, ltWarn, ltInfo);
  PLogData = ^TLogData;
  TLogData = record
LogType: TLogType;
LogArgs: TConstArray;
  end;

 

 procedure TUserClass.Log(const LogType: TLogType; const Args: array of
 const );
 var
  LogData: PLogData;
 begin
// size of record TLogData does not work
GetMem(LogData, sizeof(TLogData));
LogData.LogType := LogType;
 // blows up on next line
LogData.LogArgs := CreateConstArray(Args);
 //  ... do some other stuff with the LogData item finally calling FreeMem
 end;

 function CreateConstArray(const Elements: array of const): TConstArray;
 var
  I: Integer;
 begin
  SetLength(Result, Length(Elements));
  for I := Low(Elements) to High(Elements) do
Result[I] :=  // assign a TVarRec here
 end;


 The code that assigns the memory only assigns 8 bytes - and an access
 violation ensues.  If I replace the call to sizeof with the number 16,
 the code works fine.

 My understanding of dynamic arrays was that the compiler created a 4 byte
 field before the first element that contained the length of the array.

 So why does the sizeof  function not reflect this ?  And why do I need 16
 bytes not 12  (4 for LogType + 4 for length of array + 4 for array
 pointer)?
 Also regardless of the number of items in the open array parameter, 16
 bytes works, so it does not relate the length of the TConstArray.

 Your thoughts ?

 David



 ___
 NZ Borland Developers Group - Delphi mailing list
 Post: delphi@delphi.org.nz
 Admin: http://delphi.org.nz/mailman/listinfo/delphi
 Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject:
 unsubscribe






 ___
 NZ Borland Developers Group - Delphi mailing list
 Post: delphi@delphi.org.nz
 Admin: http://delphi.org.nz/mailman/listinfo/delphi
 Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject:
 unsubscribe

___
NZ Borland Developers Group - Delphi mailing list
Post: delphi@delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: 
unsubscribe