comp.lang.ada
 help / color / mirror / Atom feed
* raised CONSTRAINT_ERROR
@ 2000-07-23  0:00 Matthias Teege
  2000-07-23  0:00 ` Gisle S�lensminde
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Matthias Teege @ 2000-07-23  0:00 UTC (permalink / raw)



Moin,

I made my first steps in Ada using GNATS under FreeBSD and
now I have a problem that I didnt understand. The followig
code gives me a raised CONSTRAINT_ERROR (in Line:
StrArg := Ada.Command_Line.Argument(I)) and I didnt know
why.

with Ada.Command_Line; use Ada.Command_Line;
with GNAT.IO; use GNAT.IO;

procedure Cldemo is

Argcount: Natural;
StrArg : String (1 .. 10);
I: Positive := 1;

begin
   Argcount := Ada.Command_Line.Argument_Count;

Schleife:
   while I <= Argcount loop
      StrArg := Ada.Command_Line.Argument(I);
      Put_Line(StrArg);
      I := I + 1;
   end loop Schleife;

end Cldemo;

If I change the Code to

with Ada.Command_Line; use Ada.Command_Line;
with GNAT.IO; use GNAT.IO;

procedure Cldemo is

Argcount: Natural;
I: Positive := 1;

begin
   Argcount := Ada.Command_Line.Argument_Count;

Schleife:
   while I <= Argcount loop
      Put_Line(Ada.Command_Line.Argument(I));
      I := I + 1;
   end loop Schleife;

end Cldemo;

all works perfect. What is the main difference between
both versions?

Thanks for any hints
Matthias





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: raised CONSTRAINT_ERROR
  2000-07-23  0:00 raised CONSTRAINT_ERROR Matthias Teege
@ 2000-07-23  0:00 ` Gisle S�lensminde
  2000-07-24  0:00   ` Gisle S�lensminde
  2000-07-24  0:00 ` Pascal Obry
  2000-07-24  0:00 ` Ted Dennison
  2 siblings, 1 reply; 5+ messages in thread
From: Gisle S�lensminde @ 2000-07-23  0:00 UTC (permalink / raw)


In article <86lmytp5i0.fsf@book.mteege.de>, Matthias Teege wrote:
>
>Moin,
>
>I made my first steps in Ada using GNATS under FreeBSD and
>now I have a problem that I didnt understand. The followig
>code gives me a raised CONSTRAINT_ERROR (in Line:
>StrArg := Ada.Command_Line.Argument(I)) and I didnt know
>why.

In the first version a string of length exactly 10 is used. In
the other version a generic string (allocated on stack) is
used. In Ada it's not allowed to assign strings of different
length, and the 

If you write on command line: 

% cldemo 0123456789 9876543210 abcdefghij 

The program will not give constraint error because all strings
is of the correct size. The string type have fixed size. If you 
want to handle dynamic sized strings use unbounded strings,
which is there to handle such cases. 

Another way is to use a block:

declare 
  Arg : String := Ada.Command_Line.Argument_Count;
begin
  put_line(Arg);
end;

This way a local variable arg (with desired size) is introduced.


>
>with Ada.Command_Line; use Ada.Command_Line;
>with GNAT.IO; use GNAT.IO;
>
>procedure Cldemo is
>
>Argcount: Natural;
>StrArg : String (1 .. 10);
>I: Positive := 1;
>
>begin
>   Argcount := Ada.Command_Line.Argument_Count;
>
>Schleife:
>   while I <= Argcount loop
>      StrArg := Ada.Command_Line.Argument(I);
>      Put_Line(StrArg);
>      I := I + 1;
>   end loop Schleife;
>
>end Cldemo;
>
>If I change the Code to
>
>with Ada.Command_Line; use Ada.Command_Line;
>with GNAT.IO; use GNAT.IO;
>
>procedure Cldemo is
>
>Argcount: Natural;
>I: Positive := 1;
>
>begin
>   Argcount := Ada.Command_Line.Argument_Count;
>
>Schleife:
>   while I <= Argcount loop
>      Put_Line(Ada.Command_Line.Argument(I));
>      I := I + 1;
>   end loop Schleife;
>
>end Cldemo;
>
>all works perfect. What is the main difference between
>both versions?
>
>Thanks for any hints
>Matthias
>


-- 
--
Gisle S�lensminde ( gisle@ii.uib.no )   

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going
to land, and it could be dangerous sitting under them as they fly
overhead. (RFC 1925)




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: raised CONSTRAINT_ERROR
  2000-07-23  0:00 ` Gisle S�lensminde
@ 2000-07-24  0:00   ` Gisle S�lensminde
  0 siblings, 0 replies; 5+ messages in thread
From: Gisle S�lensminde @ 2000-07-24  0:00 UTC (permalink / raw)


In article <slrn8nlpu2.6dc.gisle@apal.ii.uib.no>, Gisle S�lensminde wrote:
>In article <86lmytp5i0.fsf@book.mteege.de>, Matthias Teege wrote:
>>
>>Moin,
>>
>
>declare 
>  Arg : String := Ada.Command_Line.Argument_Count;
>begin
>  put_line(Arg);
>end;
>

This is of cause wrong, it should have been:

declare
   Arg : String := Ada.Command_Line.Argument(I);
begin
   put_line(Arg);
end;              

--
Gisle S�lensminde ( gisle@ii.uib.no )   

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going
to land, and it could be dangerous sitting under them as they fly
overhead. (RFC 1925)




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: raised CONSTRAINT_ERROR
  2000-07-23  0:00 raised CONSTRAINT_ERROR Matthias Teege
  2000-07-23  0:00 ` Gisle S�lensminde
  2000-07-24  0:00 ` Pascal Obry
@ 2000-07-24  0:00 ` Ted Dennison
  2 siblings, 0 replies; 5+ messages in thread
From: Ted Dennison @ 2000-07-24  0:00 UTC (permalink / raw)


In article <86lmytp5i0.fsf@book.mteege.de>,
  Matthias Teege <matthias@mteege.de> wrote:
> now I have a problem that I didnt understand. The followig
> code gives me a raised CONSTRAINT_ERROR (in Line:
> StrArg := Ada.Command_Line.Argument(I)) and I didnt know
> why.
...
> StrArg : String (1 .. 10);
...
>       StrArg := Ada.Command_Line.Argument(I);

That's why. Unless your command line argument happens to be *exactly* 10
characters long, you will get a constraint error here.

Check out the following entry in our FAQ:
http://www.adapower.com/lab/adafaq/24.html


Ada's string handling is very powerful. But its also very different from
most other languages. The important thing to remember is that its length
is defined by the variable's *declaration*, not by its contents.
Generally you either want to set the string's size with an
initialization:

   loop
      declare
         StrArg : constant String := Ada.Command_Line.Argument(I);
      begin
         ...
      end;
   end loop;

...or you want to declare one string big enough to hold the entire
argument, no matter how big it could reasonably get, then assign into a
correctly-sized slice of it.

      Str_Arg_Length := Ada.Command_Line.Argument(I)'length;
      StrArg (1..Str_Arg_Length) := Ada.Command_Line.Argument(I);

      Put_Line (StrArg(1..Str_Arg_Length));

Since Ada strings don't rely on a terminator to define their length, the
'length operation does *not* iterate through the string. It might not
even generate any code at all if you have optimizations on.

Another alternative is to use a dynamic string type, like
Ada.Strings.Unbounded.Unbounded_String. Or you can use the source string
in-situ (like you did in your second example)

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: raised CONSTRAINT_ERROR
  2000-07-23  0:00 raised CONSTRAINT_ERROR Matthias Teege
  2000-07-23  0:00 ` Gisle S�lensminde
@ 2000-07-24  0:00 ` Pascal Obry
  2000-07-24  0:00 ` Ted Dennison
  2 siblings, 0 replies; 5+ messages in thread
From: Pascal Obry @ 2000-07-24  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 992 bytes --]


Matthias Teege <matthias@mteege.de> a �crit dans le message :
86lmytp5i0.fsf@book.mteege.de...
>
> Moin,
>
> I made my first steps in Ada using GNATS under FreeBSD and

You meant GNAT as GNATS is another beast.

Pascal.

--

--|------------------------------------------------------------
--| Pascal Obry                               Team-Ada Member |
--|                                                           |
--| EDF-DER-IPN-SID- T T I                                    |
--|                       Intranet: http://cln46gb            |
--| Bureau N-023            e-mail: p.obry@der.edf.fr         |
--| 1 Av G�n�ral de Gaulle  voice : +33-1-47.65.50.91         |
--| 92141 Clamart CEDEX     fax   : +33-1-47.65.50.07         |
--| FRANCE                                                    |
--|------------------------------------------------------------
--|
--|         http://perso.wanadoo.fr/pascal.obry
--|
--|   "The best way to travel is by means of imagination"







^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2000-07-24  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-07-23  0:00 raised CONSTRAINT_ERROR Matthias Teege
2000-07-23  0:00 ` Gisle S�lensminde
2000-07-24  0:00   ` Gisle S�lensminde
2000-07-24  0:00 ` Pascal Obry
2000-07-24  0:00 ` Ted Dennison

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox