Segmentation fault and huge SRAM need for Serial.printlnNeed help optimizing snake game code for Arduino Nano

Why is the application of an oracle function not a measurement?

Avoiding cliches when writing gods

Average spam confidence

What's the correct term for a waitress in the Middle Ages?

Russian equivalent of the French expression "broyer du noir"

How to retract the pitched idea from employer?

Why don't B747s start takeoffs with full throttle?

Are go-arounds prohibited at St Barth (TFFJ)?

What are the words for people who cause trouble believing they know better?

How to make thick Asian sauces?

How to make a setting relevant?

Why don’t airliners have temporary liveries?

How did students remember what to practise between lessons without any sheet music?

How to skip replacing first occurrence of a character in each line?

How to translate “Me doing X” like in online posts?

Java guess the number

Does Lightning Network has concept of continuous stream of value?

What happened to all the nuclear material being smuggled after the fall of the USSR?

What do we gain with higher order logics?

Turing patterns

Segmentation fault and huge SRAM need for Serial.println

Why is the relationship between frequency and pitch exponential?

Building a road to escape Earth's gravity by making a pyramid on Antartica

How were concentration and extermination camp guards recruited?



Segmentation fault and huge SRAM need for Serial.println


Need help optimizing snake game code for Arduino Nano













1















I have written my own 'assert' since I want to use it for both Windows and Arduino. The class is called from many files (about 10).



AssertUtils.h:



#pragma once

#define assert(expr) AssertUtils::Assert2((expr), (__func__), (__FILE__), (__LINE__));

class AssertUtils

public:
static void Assert2(bool expression, const char* funcName, const char* fileName, int line);
;


AssertUtils.cpp:



#include "AssertUtils.h"
#include "Arduino.h"

/* static */ void AssertUtils::Assert2(bool expression, const char* funcName, const char* fileName, int line)

if (!expression)

Serial.println(funcName);
Serial.println(fileName);
Serial.println(line, DEC);
Serial.flush();

// Abort program execution.
abort();




When I compile this, the SRAM usage is 1469 bytes (together with the rest of my sketch and classes).



When I comment the line below, I get a segmentation fault in a random assert call (when I comment out that call, I get it in the next, etc.).



 //Serial.println(funcName);


When I also comment out the second print statement, the compiler reports only 1137 bytes SRAM usage (which I expect since this was about equal until before I added the Assert class).



 //Serial.println(funcName);
//Serial.println(fileName);


Questions:



  1. How can I get rid of the segmentation fault?

  2. How can it be that about 332 bytes are used for one two additional print statements? (maybe related to the const char* buffering (?)









share|improve this question

















  • 1





    the segmentation fault is a bug in compiler in 1.6.22 and 1.6.23. more here forum.arduino.cc/index.php?topic=619213.msg4195749#msg4195749

    – Juraj
    6 hours ago






  • 1





    try F(__FILE__) and F(__func__). (change the parameter type to __FlashStringHelper)

    – Juraj
    6 hours ago







  • 1





    @Juraj Somehow it only works for __FILE__ but not for __func__. Anyway using F( __FILE__) was about -90B difference (It's probably full path and it was only used in one file).

    – KIIV
    6 hours ago







  • 1





    so the compiler optimizes the parameters away if hey are not used

    – Juraj
    5 hours ago







  • 1





    Why are you wasting a class on this? Just use the prints directly in the macro.

    – Majenko
    4 hours ago















1















I have written my own 'assert' since I want to use it for both Windows and Arduino. The class is called from many files (about 10).



AssertUtils.h:



#pragma once

#define assert(expr) AssertUtils::Assert2((expr), (__func__), (__FILE__), (__LINE__));

class AssertUtils

public:
static void Assert2(bool expression, const char* funcName, const char* fileName, int line);
;


AssertUtils.cpp:



#include "AssertUtils.h"
#include "Arduino.h"

/* static */ void AssertUtils::Assert2(bool expression, const char* funcName, const char* fileName, int line)

if (!expression)

Serial.println(funcName);
Serial.println(fileName);
Serial.println(line, DEC);
Serial.flush();

// Abort program execution.
abort();




When I compile this, the SRAM usage is 1469 bytes (together with the rest of my sketch and classes).



When I comment the line below, I get a segmentation fault in a random assert call (when I comment out that call, I get it in the next, etc.).



 //Serial.println(funcName);


When I also comment out the second print statement, the compiler reports only 1137 bytes SRAM usage (which I expect since this was about equal until before I added the Assert class).



 //Serial.println(funcName);
//Serial.println(fileName);


Questions:



  1. How can I get rid of the segmentation fault?

  2. How can it be that about 332 bytes are used for one two additional print statements? (maybe related to the const char* buffering (?)









share|improve this question

















  • 1





    the segmentation fault is a bug in compiler in 1.6.22 and 1.6.23. more here forum.arduino.cc/index.php?topic=619213.msg4195749#msg4195749

    – Juraj
    6 hours ago






  • 1





    try F(__FILE__) and F(__func__). (change the parameter type to __FlashStringHelper)

    – Juraj
    6 hours ago







  • 1





    @Juraj Somehow it only works for __FILE__ but not for __func__. Anyway using F( __FILE__) was about -90B difference (It's probably full path and it was only used in one file).

    – KIIV
    6 hours ago







  • 1





    so the compiler optimizes the parameters away if hey are not used

    – Juraj
    5 hours ago







  • 1





    Why are you wasting a class on this? Just use the prints directly in the macro.

    – Majenko
    4 hours ago













1












1








1








I have written my own 'assert' since I want to use it for both Windows and Arduino. The class is called from many files (about 10).



AssertUtils.h:



#pragma once

#define assert(expr) AssertUtils::Assert2((expr), (__func__), (__FILE__), (__LINE__));

class AssertUtils

public:
static void Assert2(bool expression, const char* funcName, const char* fileName, int line);
;


AssertUtils.cpp:



#include "AssertUtils.h"
#include "Arduino.h"

/* static */ void AssertUtils::Assert2(bool expression, const char* funcName, const char* fileName, int line)

if (!expression)

Serial.println(funcName);
Serial.println(fileName);
Serial.println(line, DEC);
Serial.flush();

// Abort program execution.
abort();




When I compile this, the SRAM usage is 1469 bytes (together with the rest of my sketch and classes).



When I comment the line below, I get a segmentation fault in a random assert call (when I comment out that call, I get it in the next, etc.).



 //Serial.println(funcName);


When I also comment out the second print statement, the compiler reports only 1137 bytes SRAM usage (which I expect since this was about equal until before I added the Assert class).



 //Serial.println(funcName);
//Serial.println(fileName);


Questions:



  1. How can I get rid of the segmentation fault?

  2. How can it be that about 332 bytes are used for one two additional print statements? (maybe related to the const char* buffering (?)









share|improve this question














I have written my own 'assert' since I want to use it for both Windows and Arduino. The class is called from many files (about 10).



AssertUtils.h:



#pragma once

#define assert(expr) AssertUtils::Assert2((expr), (__func__), (__FILE__), (__LINE__));

class AssertUtils

public:
static void Assert2(bool expression, const char* funcName, const char* fileName, int line);
;


AssertUtils.cpp:



#include "AssertUtils.h"
#include "Arduino.h"

/* static */ void AssertUtils::Assert2(bool expression, const char* funcName, const char* fileName, int line)

if (!expression)

Serial.println(funcName);
Serial.println(fileName);
Serial.println(line, DEC);
Serial.flush();

// Abort program execution.
abort();




When I compile this, the SRAM usage is 1469 bytes (together with the rest of my sketch and classes).



When I comment the line below, I get a segmentation fault in a random assert call (when I comment out that call, I get it in the next, etc.).



 //Serial.println(funcName);


When I also comment out the second print statement, the compiler reports only 1137 bytes SRAM usage (which I expect since this was about equal until before I added the Assert class).



 //Serial.println(funcName);
//Serial.println(fileName);


Questions:



  1. How can I get rid of the segmentation fault?

  2. How can it be that about 332 bytes are used for one two additional print statements? (maybe related to the const char* buffering (?)






serial programming arduino-nano class c-preprocessor






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









Michel KeijzersMichel Keijzers

7,24462041




7,24462041







  • 1





    the segmentation fault is a bug in compiler in 1.6.22 and 1.6.23. more here forum.arduino.cc/index.php?topic=619213.msg4195749#msg4195749

    – Juraj
    6 hours ago






  • 1





    try F(__FILE__) and F(__func__). (change the parameter type to __FlashStringHelper)

    – Juraj
    6 hours ago







  • 1





    @Juraj Somehow it only works for __FILE__ but not for __func__. Anyway using F( __FILE__) was about -90B difference (It's probably full path and it was only used in one file).

    – KIIV
    6 hours ago







  • 1





    so the compiler optimizes the parameters away if hey are not used

    – Juraj
    5 hours ago







  • 1





    Why are you wasting a class on this? Just use the prints directly in the macro.

    – Majenko
    4 hours ago












  • 1





    the segmentation fault is a bug in compiler in 1.6.22 and 1.6.23. more here forum.arduino.cc/index.php?topic=619213.msg4195749#msg4195749

    – Juraj
    6 hours ago






  • 1





    try F(__FILE__) and F(__func__). (change the parameter type to __FlashStringHelper)

    – Juraj
    6 hours ago







  • 1





    @Juraj Somehow it only works for __FILE__ but not for __func__. Anyway using F( __FILE__) was about -90B difference (It's probably full path and it was only used in one file).

    – KIIV
    6 hours ago







  • 1





    so the compiler optimizes the parameters away if hey are not used

    – Juraj
    5 hours ago







  • 1





    Why are you wasting a class on this? Just use the prints directly in the macro.

    – Majenko
    4 hours ago







1




1





the segmentation fault is a bug in compiler in 1.6.22 and 1.6.23. more here forum.arduino.cc/index.php?topic=619213.msg4195749#msg4195749

– Juraj
6 hours ago





the segmentation fault is a bug in compiler in 1.6.22 and 1.6.23. more here forum.arduino.cc/index.php?topic=619213.msg4195749#msg4195749

– Juraj
6 hours ago




1




1





try F(__FILE__) and F(__func__). (change the parameter type to __FlashStringHelper)

– Juraj
6 hours ago






try F(__FILE__) and F(__func__). (change the parameter type to __FlashStringHelper)

– Juraj
6 hours ago





1




1





@Juraj Somehow it only works for __FILE__ but not for __func__. Anyway using F( __FILE__) was about -90B difference (It's probably full path and it was only used in one file).

– KIIV
6 hours ago






@Juraj Somehow it only works for __FILE__ but not for __func__. Anyway using F( __FILE__) was about -90B difference (It's probably full path and it was only used in one file).

– KIIV
6 hours ago





1




1





so the compiler optimizes the parameters away if hey are not used

– Juraj
5 hours ago






so the compiler optimizes the parameters away if hey are not used

– Juraj
5 hours ago





1




1





Why are you wasting a class on this? Just use the prints directly in the macro.

– Majenko
4 hours ago





Why are you wasting a class on this? Just use the prints directly in the macro.

– Majenko
4 hours ago










1 Answer
1






active

oldest

votes


















2














Note, this answer is not MY answer, but approximately the combination of the first 5 comments from my original question.



What I did was:



  • (Juraj) Changed the compiler version to 6.21

  • (juraj) Added an F in F(__FILE__)

  • (KIIV) Removed func; this did not work with the F flash helper, but I can find back the code anyway unambiguously because of the file name + line number.

  • (Majenko) I removed the class and use a macro. This costs some more Flash memory (depending on the number of asserts I use, but I'm still around 40% (was 35%).

The SRAM usage is now 1125 bytes, even less than where I'm started with.



The resulting code is below:



#define assert(expression) 
if (!(expression))

Serial.println(F(__FILE__));
Serial.println(__LINE__, DEC);
Serial.flush();
abort();






share|improve this answer


















  • 1





    About class - in this case it was used as old form of namespaces (as namespaces weren't present in c++ until 1990). However struct was used more likely as it doesn't need public: specifier.

    – KIIV
    2 hours ago











Your Answer






StackExchange.ifUsing("editor", function ()
return StackExchange.using("schematics", function ()
StackExchange.schematics.init();
);
, "cicuitlab");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "540"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f65933%2fsegmentation-fault-and-huge-sram-need-for-serial-println%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Note, this answer is not MY answer, but approximately the combination of the first 5 comments from my original question.



What I did was:



  • (Juraj) Changed the compiler version to 6.21

  • (juraj) Added an F in F(__FILE__)

  • (KIIV) Removed func; this did not work with the F flash helper, but I can find back the code anyway unambiguously because of the file name + line number.

  • (Majenko) I removed the class and use a macro. This costs some more Flash memory (depending on the number of asserts I use, but I'm still around 40% (was 35%).

The SRAM usage is now 1125 bytes, even less than where I'm started with.



The resulting code is below:



#define assert(expression) 
if (!(expression))

Serial.println(F(__FILE__));
Serial.println(__LINE__, DEC);
Serial.flush();
abort();






share|improve this answer


















  • 1





    About class - in this case it was used as old form of namespaces (as namespaces weren't present in c++ until 1990). However struct was used more likely as it doesn't need public: specifier.

    – KIIV
    2 hours ago















2














Note, this answer is not MY answer, but approximately the combination of the first 5 comments from my original question.



What I did was:



  • (Juraj) Changed the compiler version to 6.21

  • (juraj) Added an F in F(__FILE__)

  • (KIIV) Removed func; this did not work with the F flash helper, but I can find back the code anyway unambiguously because of the file name + line number.

  • (Majenko) I removed the class and use a macro. This costs some more Flash memory (depending on the number of asserts I use, but I'm still around 40% (was 35%).

The SRAM usage is now 1125 bytes, even less than where I'm started with.



The resulting code is below:



#define assert(expression) 
if (!(expression))

Serial.println(F(__FILE__));
Serial.println(__LINE__, DEC);
Serial.flush();
abort();






share|improve this answer


















  • 1





    About class - in this case it was used as old form of namespaces (as namespaces weren't present in c++ until 1990). However struct was used more likely as it doesn't need public: specifier.

    – KIIV
    2 hours ago













2












2








2







Note, this answer is not MY answer, but approximately the combination of the first 5 comments from my original question.



What I did was:



  • (Juraj) Changed the compiler version to 6.21

  • (juraj) Added an F in F(__FILE__)

  • (KIIV) Removed func; this did not work with the F flash helper, but I can find back the code anyway unambiguously because of the file name + line number.

  • (Majenko) I removed the class and use a macro. This costs some more Flash memory (depending on the number of asserts I use, but I'm still around 40% (was 35%).

The SRAM usage is now 1125 bytes, even less than where I'm started with.



The resulting code is below:



#define assert(expression) 
if (!(expression))

Serial.println(F(__FILE__));
Serial.println(__LINE__, DEC);
Serial.flush();
abort();






share|improve this answer













Note, this answer is not MY answer, but approximately the combination of the first 5 comments from my original question.



What I did was:



  • (Juraj) Changed the compiler version to 6.21

  • (juraj) Added an F in F(__FILE__)

  • (KIIV) Removed func; this did not work with the F flash helper, but I can find back the code anyway unambiguously because of the file name + line number.

  • (Majenko) I removed the class and use a macro. This costs some more Flash memory (depending on the number of asserts I use, but I'm still around 40% (was 35%).

The SRAM usage is now 1125 bytes, even less than where I'm started with.



The resulting code is below:



#define assert(expression) 
if (!(expression))

Serial.println(F(__FILE__));
Serial.println(__LINE__, DEC);
Serial.flush();
abort();







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 hours ago









Michel KeijzersMichel Keijzers

7,24462041




7,24462041







  • 1





    About class - in this case it was used as old form of namespaces (as namespaces weren't present in c++ until 1990). However struct was used more likely as it doesn't need public: specifier.

    – KIIV
    2 hours ago












  • 1





    About class - in this case it was used as old form of namespaces (as namespaces weren't present in c++ until 1990). However struct was used more likely as it doesn't need public: specifier.

    – KIIV
    2 hours ago







1




1





About class - in this case it was used as old form of namespaces (as namespaces weren't present in c++ until 1990). However struct was used more likely as it doesn't need public: specifier.

– KIIV
2 hours ago





About class - in this case it was used as old form of namespaces (as namespaces weren't present in c++ until 1990). However struct was used more likely as it doesn't need public: specifier.

– KIIV
2 hours ago

















draft saved

draft discarded
















































Thanks for contributing an answer to Arduino Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f65933%2fsegmentation-fault-and-huge-sram-need-for-serial-println%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480