Calculator Memory Issues
Hello, i tried making a small math expression parsing library, but it seems that i can only do one Parse, and then my Uno freezes, i'm fairly certain there are no leaks which makes me ask why its freezing, and i would appreciate if anyone is able to look into it. I am not yet very familiar with the C allocation functions, so please tell me if i used any of them wrong.
Thank you!
52 Replies
Best way to not have an issue is to not use c allocation especially in arduino
they dont work in arduino and in microcontrollers the way they do with computers
Oh, should i just use fixed size arrays then?
Im pretty sure arduino has an alternative anyways
Wait for someone who knows what to do answer, i would use a fixed array but im sure theres a much better way.
Thanks!
Microcontrollers have a different system of memory than computers, im not sure but i think they use really short paging. Like 100 bytes short
And it pages by the instruction counter
You can check if you have memory leaks by testing the free heap before and after the calculation.
https://docs.arduino.cc/learn/programming/memory-guide/#sram-memory-measurement gives the code for AVR boards, which your Uno is
if we're talking Uno R3.
Can malloc even be used?
yup
In extremely small cases, yes.
Heap fragmentation can quickly become an issue.
Same with excessive
String() object creation.alright im quickly gonna check some things with this and then ill come back with results!
Whats the way to use it because i remember it worked way differently that on a computer
well thats weird, i actually gained SRAM after doing '=', it prints out once more when doing '=' (but the result doesnt for some reason), then becomes unresponsive, maybe it isnt a memory issue?
Can you send the part of your code where you do the memory allocation?
What do you mean exactly? All malloc's are in calclib.hpp
I mean send it like this im on phone
I cant look at the file
Alright
the entire file?
No just the part where your using malloc
theres 18 malloc's
Nvm
I dont even you can allocate 32 spaces, what pointer does it return when you do?
wdym?
Whats the pointer that it returns?
what returns?
Malloc(32)
let me check
this is from the result or what do you mean?
Does it return that again when you repeat it?
Rerun it
it does not return the same result after i run it again, after running '=' again it doesnt print and after one more it becomes entirely unresponsive.
Malloc(32
Malloc(32
These two return different result?
In a program alone with nothing else
uhh alright let me check
How much ram does the ide say the board has?
And how much are you allocating? (Assuming there isnt a memory leak)
2048 bytes
nope this seems to work in a single program just fine.
in total from every malloc summed up or?
Alright lets test something, allocate 2000 bytes. Does it freeze the board up like your program your trying to fix does?
Summed up yeh
The uno might not be freeing the memory
Btw srry for making you do all of this im away from a computer rn and cant test anything myself
If you want to wait thats okay
uhh somehow doesnt freeze? Unless i did something wrong
its fine
Try to do more than 2048
Also check if its returning null
sure
void setup()
{
Serial.begin(115200);
Serial.println("Memory");
char* e = malloc(2000);
if (e == NULL)
{
Serial.println("NOT memory 2");
}
Serial.println(("Memory 2"));
}
void loop()
{
}
2000 isnt null, 2048 and above is null.
there are alot of malloc's which are writing to the same pointer (freed before, basically im saying theres alot more mallocs then frees), do you want it anyway?
im just gonna try the same calculator code on my mega, see if theres any difference, even tho i doubt it
seems to not be memory related at all, my mega has the exact same result
First prints out the valid result, then prints out nothing, then becomes unresponsive AFTER the third '='
which is
interesting
Huh
Yeah i have no idea
me neither
this is, extremely weird
Ill try your code on a esp sometime today
its 8:44 pm for me
2 here
2 pm?
Yeh
alright, i might be asleep by the time you test, which is fine, so incase i will be asleep by then, goodnight.
also its crazy how i managed to make it CONSISTENTLY unresponsive after 2 parsing's
i mean, its also possible its not truly unresponsive, i still see the Serial led light up when i input something
just no output
so if you have an lcd that could help debug
Serial issue maybe?
Maybes its not the board freezing up?
Yeah thats a possibility
If you have anything to test that thatd be great
But then again, what would cause serial to freeze up?
No idea
@Dimolade btw sorry for making you wait im a bit busy
I have to get my workspace setup before i can do anything
Its fine👍
Just a quick look, maybe this has been cought alreadey? This is really rough to follow with all its combining and double negitives. Try..
Also, why structs? If you want to make classes, why not make classes?
You also mix nullptr with NULL all over. Is there a reason for this? You sure that's a good idea?
Oh one other thing. declaring variables and classes inside case statements is a very bad idea! I'm seen the compiler go completely batty doing that. Took forever to track it down.
I thought the only difference was the privacy of variables?
Oh dang
Alr ill try that
For allocating and re-allocating of memory. I use resizeBuff(). Saves a lot of typing.
Bascally you declare a pointer, say.. for a c string, like you use.
Oh thats pretty cool
well thank you!
It's actually in the LC_baseTools library. You can instal it from the library manager.
:arduinoheart: