wait, accepted solution is … memory full because of goto?
See reply #3 regarding the F() macro, a huge amount of your memory is being used for storing text for printing.
Yup that's true, at the time I didn't understand what automatic means but I know if you have global scope variables they're initialized by system but if you have it in any local scope then it's a garbage value;
Nope, solution is using F() macro as suggested. It saved me a lot of effort.
Hey thank you so much guys for helping me out with that issue, but now I was working on similar project to this one except it's much larger and I was learning what're pointers, I came across a way to print addresses of variables. I have code below where I am trying to practice. Does any know what does datatype "int" within parenthesis means in function -
Serial.println((int}&A, HEX);
int A = 0;
int B = 4;
int *pointer;
void setup(){
Serial.begin(115200);
}
void loop(){
pointer = &A;
Serial.println( (int)&A, HEX); //IDK what does "(int)" means inside that. I know "int" as
//datatype and how to use it but this is very new to me.
}
It performs type conversion. From current type to int. The current type is a memory address. It wil be represented to the monitor as an int.
Oh I see, I updated my code but idk why the variable C has same memory location as B, when I try to print it on monitor. First I replaced ptr3 = C; and then ptr3 = B; and it showed same memory location address as for both of them, I changed them one by one and uploaded one by one. I didn't compare them by storing those addresses in two different variables. Also When I printed location of array A[0], A[1], A[2], A[3] the result was 200, 202, 204, 206. Then I replaced ptr3 = C; it seemed that as if memory allocation shifted every time. the result was 202, 204, 206 and 200 for "C". Can you please explain and help me why is this happening. Should I understand and learn properly and completely how does memory work before learning pointers?
here's my code:
int A[] = {'h', 'e', 'l', 'l', 'o'};
int B = 4;
int C = 6;
int *ptr;
int *ptr1;
int *ptr2;
int *ptr3;
void setup(){
Serial.begin(115200);
}
void loop(){
ptr = &A[0];
ptr1 = &A[1];
ptr2 = &A[2];
ptr3 = &C;
Serial.print((int) ptr, HEX);
Serial.print(" ");
Serial.print((int)ptr1, HEX);
Serial.print(" ");
Serial.print((int)ptr2, HEX);
Serial.print(" ");
Serial.println((unsigned int)ptr3, HEX);
}
You are facing compiler optimization. In the first run you never used 'C'. The compiler recognizes that, and ignores C, reserving no space for this variable.
This is often the problem if you try such simple programs to see how it works. To switch off optimizing you could add a line at the top of your sketch:
#pragma GCC optimize ("O0")
Then the compiler doesn't optimize and the result is nearer to what you expect. But you should never switch off optimizing in a 'productive' sketch.
Normally you should use:
char A = {'h','e','l','l', 'o'};
Then addresses of A[0], A[1], A[3] should have consecutive numbers.
Apparently the compiler is free to change the order of putting things on the stack...
Groet, Koen.
I doubt that that will compile without warnings or errors. In you example, the variable A is not an array.
You are right...
char A[] =
I guess then
char A[ ] = {'h','e','l','l', 'o','\0'};
If you want it to be a C-string... then yes...
I don't understand what do you mean by I didn't use C variable because I initialized it as 6 but do you mean I didn't use that in any of the functions? if I do ptr3 = &C is that considered using C or not, and if not then why? pls explain.
Actually you're right it should be char instead of int but even if I do int then it shouldn't affect it right because it's just larger storage? It's not efficient but it shouldn't affect my result if use it that way for small program.
If you make it char instead of int you may print the chars as text. Otherwise you will get the binary representation of the characters.
An array of char will take half the memory of an array of int.
If you make the last character '\0 you have a C string that you can print as a whole:print(A);`
A C-string can be manipulated with string functions, I did not try, but I guess that cannot be done with your array of int.
Yes you initialized the variable, but you never used that value anywhere in the program. That's why the compiler dropped it.
Of course it's a C/C++ assignment. Because you use the address of the variable C, the compiler cannot drop it anymore.
After ptr3=&C;
the compiler can still decide to discard implementation, since now we have a pointer pointing to a value. The pointer is not used yet.
After Serial.println((unsigned int)ptr3, HEX);
Implementation cannot be discarded anymore.
So indeed:
#pragma GCC optimize ("O0")
So lets say If I have a String variable like String word = "Hello"; Program will reserve some bytes for that string, Do you know how the string "Hello" is stored in those bytes? Can we access individual bytes where the program has reserved some bytes for string "Hello". Because I was thinking If the program saves each character as string in each individual bytes then maybe I can access the individual byte and manipulate the string. Is it possible? Also can you pass a string store somewhere in a function and somehow assign that string to an array inside the function such that each index has individual characters of the string? Please help me with I need it
I already answered this question in You Other Thread.