String Parameter

General discussion on mikroPascal PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: String Parameter

#7 Post by Dany » 04 Jun 2015 14:57

Hi, apparently the following happens:

For the statement "DebugStr('Zero Counts: ' + s1 + ' (11.7mA resolution)' + #13 + #10);" the compiler has to reserve memory to hold the total string that will be sent to the uart.

In case you e.g. define "procedure DebugStr(var debugstring: string[50]);" it will reserve (on the stack) 50 bytes for the total string sent to DebugStr.
This means also, that, when you take a number that is too low (e.g. "procedure DebugStr(var debugstring: string[5]);" the software will block due to memory corruption. (tested: it happens).

In case of using "procedure DebugStr(var debugstring: string);" the compiler has no clue about the number of bytes to reserve.
The P24 mP compiler version 6.0.1 takes here a size based on the size of S1, which is 129 bytes, plus the constants in the DebugStr statement, comes in total 164 bytes reserved for the total string.

So, as far as I can see using "procedure DebugStr(var debugstring: string);" should be safer than using "procedure DebugStr(var debugstring: string[50]);" because in the latter case one can reserve a too low amount.

p.s. I did not know this could happen. :shock: :shock:

Anyway: I see the opposite happening of what You detected Jim, but only if the size of the string parameter is too low to hold the complete string parameter for DebugStr. Do you use the same mP compiler version as I do?
I did the test with the P24FJ64GA002, what type did you use?

p.s. to prevent these type of problems it is perhaps better not to use the '+' operator when handling strings. This means creating your own temp string variable and using "strcat".

My Test code:

Code: Select all

program Jim_Keuneman;

{ Declarations section }

var
    ADCZeroCounts: Integer;
    Res: word;

procedure DebugStr(var debugstring: string);
begin
  UART1_Write_Text(debugstring);
end;

begin
  { Main program }

  ADPCFG := 0xFFFF;       // Configure AN pins as digital I/O

  Res := PPS_Mapping (7, _INPUT,  _U1RX);  // RP7 is uart1 input
  Res := PPS_Mapping (6, _OUTPUT, _U1TX);  // RP6 is uart1 output

  Uart1_init(115200);
  delay_ms(250);
  
  ADCZeroCounts := 1250;

  while true do
  begin
    WordToStr(ADCZeroCounts, s1);
    DebugStr('Zero Counts: ' + s1 + ' (11.7mA resolution)' + #13 + #10); 
  end;
end.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

JimKueneman
Posts: 417
Joined: 10 Jan 2009 22:03

Re: String Parameter

#8 Post by JimKueneman » 04 Jun 2015 16:57

Can't thank you enough for digging into this. I am using the latest compiler 6.0.2 on the big dsPIC33EP512GP710? There is a warning about the compiler only supporting one "+" but I recalled a different problem in the past that I thought was solved in the later compiler. I assumed the help file was just out of date. It appears there is a much more subtle issue with "+"...

It is SO hard not to use "+" since I am in parallel writing Delphi/Lazarus code on the other end of the TCP stream for this project, especially when all this is used for is debug. I just want to concat some info and spit it out to debug something else.

Jim

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: String Parameter

#9 Post by Dany » 04 Jun 2015 20:43

After more testing:
- The issue occurs also when S1 is defined globally in the main unit,
- The issue is there in the mP for PIC, dsPIC/PIC24 and PIC32 compilers. The latter 2 reserve too less stack space and the PIC one makes a global variable that is too small when using "string[xx]" as var parameter for the DebugStr function.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

JimKueneman
Posts: 417
Joined: 10 Jan 2009 22:03

Re: String Parameter

#10 Post by JimKueneman » 04 Jun 2015 23:29

mE, is this something we should put a ticket in on or is as expected? I hope it is not the latter because I did not expect it!

Jim

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: String Parameter

#11 Post by marina.petrovic » 05 Jun 2015 06:58

Hi,

There is no need to create a ticket, everything is explained properly on this forum topic.
I will consult our software developers about this behavior and notify you what they told me as soon as I receive some information.

Thank you both, Jim and Dany for pointing on this behavior.

Best regards,
Marina

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: String Parameter

#12 Post by janni » 06 Jun 2015 03:04

JimKueneman wrote:mE, is this something we should put a ticket in on or is as expected? I hope it is not the latter because I did not expect it!
I think that this kind of trouble should be expected (sorry Jim :) ). After all, you use string literal in parameter passed by reference (I know mP allows it, but it's more a shortcut for newbies to pass the famous "Hello World" string than a proper Pascal use). Putting too much trust in small processor compiler by employing multiple '+' operators is also a bit risky. That it may work at all is due more to mE developers effort to satisfy user's wishes than to Pascal language requirements. If mP and mB users had to pay the same high RAM use penalty for passing string literals as programmers using mC, it certainly wouldn't be a popular solution.

As for the difference in behaviour when one uses 'string[n]' rather than 'string' when specifying parameter type, it's due to the assumption that compiler should not outguess the programmer (unless one likes unexpected effects :wink: ). When one declares size of a parameter, it should be for a reason, so mP limits the memory reserved to user-specified size and it becomes user responsibility to keep within his own limits. (Initially, when one forced string literal to be passed by reference, compiler assumed size 0 when one didn't specify the parameter size or exactly the size specified - whether the space was needed or not. Present solution seems much better and I don't see any other reasonable way it could work with the limited resources of small processors.)

This issue should certainly go into Dany's "Ram Corruption source" collection (which could some day be incorporated to Help file :idea: ), as a warning, but there doesn't seem to be a need for changes in compiler. Certainly, passing string literals by reference should not be so much encouraged in examples and the way it works (against Pascal definition) should be well described in Help.

JimKueneman
Posts: 417
Joined: 10 Jan 2009 22:03

Re: String Parameter

#13 Post by JimKueneman » 06 Jun 2015 04:12

Hi Janni,


I am not going to argue at all that what I am doing is not recommended and dangerous that is really why I did not spend much time understanding it. I was just hoping for a warning or something.

Jim

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: String Parameter

#14 Post by janni » 06 Jun 2015 13:33

Hi Jim,

And I'm not going to argue that you shouldn't take risks in your own code :) . A warning should undoubtedly be issued when compiler finds the string parameter longer than declared and mP PRO for PIC does issue one. If there's no warning in dsPIC compiler than this should be fixed, but there will be no warning if string parameter size wasn't declared (there should be no need to).

The issue you experienced was most probably due to use of the '+' operator in strings, which just happened to show up in passing string literal by reference. String concatenation with '+' operator was never perfect and, though it works now in most cases (at least in PIC compiler), one cannot assume it safe. For example, if one appends a string constant then string terminator will not be added :( .

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: String Parameter

#15 Post by Dany » 07 Jun 2015 19:57

janni wrote:For example, if one appends a string constant then string terminator will not be added :(
Indeed, this problem has been reported on August the 14th 2014, and is still there (at least in mP for PIC). See http://www.mikroe.com/forum/viewtopic.php?f=86&t=61214.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

Post Reply

Return to “mikroPascal PRO for dsPIC30/33 and PIC24 General”