Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Detecting errors in String operations #186

Open
@BojanJurca

Description

@BojanJurca

Hello,

If a String creation fails it is possible to detect this error in the code, like:

String s ="abc";if (!s) ...// error, out of heap or something ...

However the error detection doesn't work with String expressions:

#definehowLarge5000String *strArray =new String [howLarge];voidwasteAlmostAllMemory () {for (int i =0; i < howLarge; i++) strArray [i] =" wasted memory";while (true)for (int i =0; i < howLarge; i++)if (!strArray [i].concat (" more wasted memory"))return;} StringreturnLongString () {return"This is a long string, much longer than fits into free memory, although not right now.";}voidsetup () {    Serial.begin (115200);    String longString =returnLongString ();    Serial.printf ("Wasting memory, please wait ...");wasteAlmostAllMemory ();    Serial.printf ("memory successfuly wasted\n");    String resultString;// 1.      resultString = longString;if (!resultString)        Serial.printf ("Could not create a long String s.\n");// error detected successfulyelse { Serial.print ("'"); Serial.print (resultString); Serial.println ("'"); }// 2.      resultString ="ABC" + longString +" DEF";if (!resultString)        Serial.printf ("Could not calculate a String s.\n");// error goes by undetectedelse { Serial.print ("'"); Serial.print (resultString); Serial.println ("'"); }// the output is: ' DEF'// 3.      resultString =returnLongString ();// failure to create the return String crashes the controller}voidloop () {}

My proposal is that each string expression that contains string in "error state" result in string also in "error state", so success could be tested only once even after several String operations. Something like this:

    String s ="abc";for (int i =1; i <1000; i++)      s += s.substring (1,1);if (!s) ...// error

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp