Re: Constraint Error

tmoran_at_acm.org
Date: 09/30/04


Date: Thu, 30 Sep 2004 01:11:17 GMT


> m2:=Messages.Create("This is a second test");
will raise Constraint_Error because you attempt to store its 21 characters
into M.Text, which is a 20 character string. Ada is preventing a buffer
overflow error.

> m1:=Messages.Create("This is a test");
raises Constraint_Error because you attempt to store a 14 character
string into a 20 character M.Text If you had said, for instance,
  M.Text(1 .. Str'length) := Str;
then it would have stored into the first 14 characters, leaving junk
in the next 6 and no way for a later user of M.Text to know which,
if any, of its characters are non-junk.
Or
  M.Text := Str & (Str'length+1 .. M.Text'length => ' ');
would fill in any unused characters with ' ', which might be what
you would like.

To assign an array (or String, which is just an array of characters)
to another array, their sizes must match. If they don't match, you
will need to truncate, pad, or just partially fill, as needed. In
that case you probably want to use a counter or special terminating
character, to remember the "actual" size of the string in M.Text

In the particular case of varying length strings, of course, it would
be easiest to just use Ada.Strings.Bounded or Ada.Strings.Unbounded



Relevant Pages

  • Re: Help a beginner - simple lowercase to uppercase and so on function
    ... And then one to loop across the string calling that function ... copying at to a new array. ... are any characters other than lowercase letters */ ...
    (comp.lang.c)
  • Re: fgets question
    ... documentation didn't say if fgets put \0 after a string literal. ... fgets() has nothing at all to do with string literals. ... within the bounds of the array, then it is not a string. ... strlenon an array of characters that is not a string, ...
    (comp.lang.c)
  • Re: Help creating a random string in Perl
    ... can pick one of the 7 characters in my source string) by using "print ... third and fourth characters to and check that the random number hasn't ... shuffling the array ... The idea here is to swap the current cell with some cell ...
    (perl.beginners)
  • Re: newbie: mapping CHARACTER*2 to INTEGER*2
    ... that destroys the Chinese characters in my input. ... effect the conversion required. ... If MOLD is an array and SIZE is omitted, ... represent the values 4.0 and 1082130432 as the string of binary digits ...
    (comp.lang.fortran)
  • Re: Parsing binary values in the registry
    ... how to take a string variable that could concievably be up to 300 ... and split that into either an array or multiple variables for ... every 32 characters within the original varable. ... I am trying to build a file path from a binary value in the ...
    (microsoft.public.scripting.vbscript)