View Issue Details

IDProjectCategoryView StatusLast Update
0032073FPCCompilerpublic2020-02-04 10:25
ReporterGeorg Hieber Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
PlatformavrOSembedded 
Product Version3.1.1 
Summary0032073: [FEATURE REQUEST] avr: maximal string length settable
DescriptionIn each procedure (including main) the compiler allocates space for temporary variables on the stack. If these temps include strings, 256 bytes are allocated for each of them.

On the small controllers that only have ½ or 1k of RAM, this more or less precludes the use of strings. On the other side, in the typical applications for these chips, strings usually are something like: 'ready in ' + xy + 'minutes', and the maximal string length is known to the programmer.

It would therefore desireable to be able to tell the compiler how much space to allocate for these temps, or, what is the default length of the type shortstring. This could be done by a compiler directive like

    {$SHORTSTRINGLENGTH xy}

This would, of course, also affect declared string variables, but with these al least one has the option to declare them as strings with a given maximal length.
TagsAVR
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Activities

Vincent Snijders

2017-06-28 19:50

manager   ~0101378

Can't you declare such a string as:

var s: string[25];

Thaddy de Koning

2017-06-28 22:21

reporter   ~0101380

Last edited: 2017-06-28 22:45

View 5 revisions

Yes. And has always been the case with shortstrings.
You can also do this,of course:
type
  avrstring = type string[25];// or whatever max you want
Note that for maximum efficiency, in this case it should be an UNeven length,because of the length byte at element [0] so that the total allocated length is a multiple of two.

Thaddy de Koning

2017-06-30 21:32

reporter   ~0101393

Note that even on AVR, if mode H+ the strings are on the little heapy,the pointer is on the stack! But anyway, fixed length shorter strings are supported. Plz ask to close.

Georg Hieber

2017-07-06 20:14

reporter   ~0101555

The problem are not your declared variables, of course one can define their length. The problem is temporary, internal variables the compiler uses for example to pass the string result of one function to another, something like:

   lcd_putstring('Voltage: '+ myIntToStr(readadc1) + ' V').

For the concatenation of the 2 constants and function result, the compiler assigns a temporary variable that is then passed to lcd_putstring.

I consider ANSI-strings, like everything else on the heap, a no-go for the AVR.

Thaddy de Koning

2020-01-29 10:29

reporter   ~0120789

concat() should work as long as the total is not longer than specified.

Heiko

2020-01-30 12:48

reporter   ~0120807

If concat behaves differently, the documentation should be adapted.

Current:
Concat concatenates the strings S1,S2 etc. to one long string.
The resulting string is truncated at a length of 255 bytes.
The same operation can be performed with the + operation.

It's otherwise misleading.

Thaddy de Koning

2020-02-03 19:16

reporter   ~0120862

Yes, the docs should be adapted, also on the little matter that ($Q+}{$R+} are ignored.
My suggestion works, btw, so the feature is already there indeed:
program avrtest;
{$mode fpc}{$H-}{$R+}{$Q+}
type
  avrstring = string[25];
var
  a:avrstring = 'test het even goed';
  b:avrstring = 'adbcdefgacdefg';
  c:avrstring = '';
begin
  c:=concat( a,b);
  writeln(length(c):5,c:40);
end.

Thaddy de Koning

2020-02-04 09:02

reporter   ~0120872

Last edited: 2020-02-04 09:05

View 3 revisions

To explain the above:
Note the internal temp used by concat is indeed string[255], but the requested result is silently truncated to the result size: string[25] in my case.
This seems documented. I will look it up, but I seem to remember that. If that is the case, there is no change required imho.

Thaddy de Koning

2020-02-04 10:25

reporter   ~0120877

I filed a report (basically against documentation)

Issue History

Date Modified Username Field Change
2017-06-28 15:31 Georg Hieber New Issue
2017-06-28 19:50 Vincent Snijders Note Added: 0101378
2017-06-28 22:21 Thaddy de Koning Note Added: 0101380
2017-06-28 22:26 Thaddy de Koning Note Edited: 0101380 View Revisions
2017-06-28 22:29 Thaddy de Koning Note Edited: 0101380 View Revisions
2017-06-28 22:30 Thaddy de Koning Note Edited: 0101380 View Revisions
2017-06-28 22:45 Thaddy de Koning Note Edited: 0101380 View Revisions
2017-06-30 21:32 Thaddy de Koning Note Added: 0101393
2017-07-06 20:14 Georg Hieber Note Added: 0101555
2020-01-28 14:45 Dimitrios Chr. Ioannidis Tag Attached: AVR
2020-01-29 10:29 Thaddy de Koning Note Added: 0120789
2020-01-30 12:48 Heiko Note Added: 0120807
2020-02-03 19:16 Thaddy de Koning Note Added: 0120862
2020-02-04 09:02 Thaddy de Koning Note Added: 0120872
2020-02-04 09:03 Thaddy de Koning Note Edited: 0120872 View Revisions
2020-02-04 09:05 Thaddy de Koning Note Edited: 0120872 View Revisions
2020-02-04 10:25 Thaddy de Koning Note Added: 0120877