Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Pascal Misc > Re: size proble...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 7 Topic 714 of 811
Post > Topic >>

Re: size problems when casting integer onto set in gpc

by Waldek Hebisch <hebisch@[EMAIL PROTECTED] > Jan 30, 2007 at 06:16 PM

southwesthustla <fdkelly@[EMAIL PROTECTED]
> wrote:
> 
> >
> > I think you should write what you really want to do.  If you want
> > copy content of a set to an array of 16-bit integers (or back) you
> > can use Move builtin routine.
> 
> That's exactly what I want to do. The move command seems to work and 
> so I have managed to create an array of 16-bit integers sized at 14-
> bytes total. The problem still arises however, when I try to cast/
> write this array onto a 112-element set. Firstly due to the set being 
> worth 16 bytes in 32-bit gpc. Secondly as the two items are not the 
> same 'shape' in memory I get a crash on execution.
> 
> I have tried re-sizing the array of 16 bit integers to have an extra 
> 'blank' row, to bring its size up to 16 bytes. This removes the 'cast 
> to type of different size' warning. However still crashes with 
> segmentation errors on running, perhaps due to memory "beyond 
> allocated size" as you say.
>

If you have variables of the same size cast should work OK:

{ To compile: gpc -fautomake cass.pas }
program cass;
uses gpc;
type at = array [0..7] of ShortInt;
     st = set of 0..111;
var a : at;
    b : st;
    i : integer;
begin
  b := [13, 17];
  a := at(b);
  for i := 0 to 7 do
    write(a[i], ', ');
  writeln;
  a[2] := 15;
  b := st(a);
  write('[');
  for i := 0 to 111 do
    if i in b then write(i, ', ');
  writeln(']');
end
..

If your program crashes, post a _complete_ compilable program that
crashes, command line used and the output of gpc -v.  Try to
simplify cra****ng part: it is quite likely that in the process
you will notice error in your program.
 
> Generally, what determines the size/shape of memory allocated for a 
> set or an array?
> 

ATM all gpc variables have simple shape: they are allocated in a
sequence of consequitive memory locations. There may be padding
between components, but this padding is considered part of the 
variable. 

For sets gpc allocate a sequence of machine words big enough to
hold the set.  If lower bound is an integer not divisible by
the word size gpc adds extra padding at the begining.  Note
that gpc idea of word size may be different than yours (IIRC
on m68k gpc uses 32-bit words).  

In the future gpc probably will optimize space used by small
sets and allocate them in half-word or byte if possible.

Plain (non-schema) arrays just are a sequence of elements.

> Concerning crash: accessing memory
> > beyond allocated size is likely to cause crash (in newer gpc versions
> > most such cases should be caught by range checking).
> 
> Do you happen to know which 'newer' gpc version will work smoothly in 
> conjunction with gcc?
> 

All versions should work well with matching gcc version.  The newest is
gpc-20060325.  Full range checking appeared in gpc-20050217, but the
first version had some problems, so you probably want later one.

-- 
                              Waldek Hebisch
hebisch@[EMAIL PROTECTED]

 




 7 Posts in Topic:
size problems when casting integer onto set in gpc
"southwesthustla&quo  2007-01-26 05:39:02 
Re: size problems when casting integer onto set in gpc
"southwesthustla&quo  2007-01-26 05:43:12 
Re: size problems when casting integer onto set in gpc
Jonas Maebe <Jonas.Mae  2007-01-26 14:44:02 
Re: size problems when casting integer onto set in gpc
Waldek Hebisch <hebisc  2007-01-26 14:57:43 
Re: size problems when casting integer onto set in gpc
"southwesthustla&quo  2007-01-30 07:46:34 
Re: size problems when casting integer onto set in gpc
Waldek Hebisch <hebisc  2007-01-30 18:16:00 
Re: size problems when casting integer onto set in gpc
"southwesthustla&quo  2007-01-31 06:13:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 6:14:56 CDT 2008.