Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-03

sizeof returns wrong size?

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

I'm attempting to write a generic writeln type of procedure for win32 console mode programs, but I'm having trouble using sizeof. I'm reasonably certain I've got it written correctly, but perhaps I'm missing something. The procedure looks like this:

Procedure Win32Write (Col,Row : byte; var SourceData);
var
  BuffSize  : TCoord;
  BuffCoord  : TCoord;
  DestRect  : TSmallRect;
begin
  // BuffSize is how big a window to write
  BuffSize.X := sizeof (SourceData);
  BuffSize.Y := 1;
  // BuffCoord is where in the window to start
  BuffCoord.X := 0;
  BuffCoord.Y := 0;
  // DestRec is where on the screen to write, here it's at Col,Row
  DestRect.Top := pred (Row);
  DestRect.Bottom := pred (Row);
  DestRect.Left := pred (Col);
  DestRect.Right := pred (sizeof (SourceData));
  WriteConsoleOutput (getstdhandle (std_output_handle), @SourceData,
BuffSize, BuffCoord, DestRect);
end;

Data being passed as SourceData is an array of tcharinfo.

Where I'm having the problem is that sizeof returns invalid information about the actual size of the SourceData variable. For example, if I pass an array with only [1..10] elements sizeof doesn't return a value of 10. And WriteConsoleOutput needs to have the correct sizes established for BuffSize.X and DestRectRight, or it prints garbage (if the numbers for those variables are larger then the data you're trying to print the remainder of the line is random, colored blocks).

Since SourceData can be any size, how do I know how large it is so I can set those values properly?

TIA...

Jim

Reply by email

Jonas Maebe
to Unknown author
I'm attempting to write a generic writeln type of procedure for win32 console mode programs, but I'm having trouble using sizeof. I'm reasonably certain I've got it written correctly, but perhaps I'm missing something.

The thing you're missing is that sizeof() is always evaluated at comnpile-time, never at run-time.

Show quoted text 12 lines
Procedure Win32Write (Col,Row : byte; var SourceData); var BuffSize : TCoord; BuffCoord : TCoord; DestRect : TSmallRect; begin // BuffSize is how big a window to write BuffSize.X := sizeof (SourceData);

This will always return 4 I think (because a generic "var" parameter is simply a pointer to whatever data you are passing to the routine).

You will have to add an explicit size parameter to your routine to solve this.

Jonas

Reply by email

Unknown author
Show quoted text 6 lines
This will always return 4 I think (because a generic "var" parameter is simply a pointer to whatever data you are passing to the routine). You will have to add an explicit size parameter to your routine to solve this.

Unfortunately, that won't work, primarily because the passed var could be of any size. I'm hoping there's some way to get the actual size of the var itself.

Jim

Reply by email

Ivan Montes
to Unknown author
Show quoted text 8 lines
> This will always return 4 I think (because a generic "var" > parameter is simply a pointer to whatever data you are passing > to the routine). > You will have to add an explicit size parameter to your routine > to solve this. Unfortunately, that won't work, primarily because the passed var could >be of any size. I'm hoping there's some way to get the actual size of >the var itself.

if you finish the array with a null value you can know when is finish and so the length of it. but as jonas said it'll be better to pass an argument to the function with the length of the buffer: Procedure Win32Write (Col,Row : byte; "length :dword;" var SourceData);

bye

Reply by email

Andreas K. Foerster
to Unknown author: Unfortunately, that won't work, primarily because the passed var could be of any size.…
Show quoted text 10 lines
> This will always return 4 I think (because a generic "var" > parameter is simply a pointer to whatever data you are passing > to the routine). > You will have to add an explicit size parameter to your routine > to solve this. Unfortunately, that won't work, primarily because the passed var could be of any size. I'm hoping there's some way to get the actual size of the var itself.

Before you call the procedure, it should be possible to use "sizeof". It's just impossible, because it's untyped IN the procedure.

Try calling it like this:
Win32Write (Col, Row, SourceData, sizeof(SourceData));
Tschuess Andreas

Reply by email

Unknown author
Show quoted text 6 lines
Before you call the procedure, it should be possible to use "sizeof". It's just impossible, because it's untyped IN the procedure. Try calling it like this: Win32Write (Col, Row, SourceData, sizeof(SourceData)); if you finish the array with a null value you can know when is finish and

so the length

of it. but as jonas said it'll be better to pass an argument to the

function with the length

of the buffer: Procedure Win32Write (Col,Row : byte; "length :dword;" var SourceData);

Getting the length before calling the procedure was a PITA for the most part -- and sizeof still didn't return the correct values even, when used on the calling side of the code -- but the logic behind these two suggestions made a lot of sense. After some recoding I was able to get things to function a bit better. Thanks.

Jim

Reply by email

Ivan Montes
to Unknown author
Show quoted text 5 lines
> if you finish the array with a null value you can know when is finish and so the length > of it. but as jonas said it'll be better to pass an argument to the function with the length > of the buffer: > Procedure Win32Write (Col,Row : byte; "length :dword;" var SourceData); Getting the length before calling the procedure was a PITA for the most part -- and sizeof still >didn't return the correct values even, when used on the calling side of the code -- but the logic >behind these two suggestions made a lot of sense. After some recoding I was able to get things >to function a bit better. Thanks.

Maybe if you post the type declaration of the SourceData you use, we can be able to find a solution.

ciao, Ivan

Reply by email