How to clear a variable?

Lets say I have a variable called c

char c = "h";

When i'm done with that variable, how do I empty that variable without putting something else into it?

Lets say I have a variable called c

OK

char c = "h";

Well, that's wrong. The thing on the right of the '=' is a string literal and its data type is "pointer to char." The thing on the left is a char variable. The compiler won't let you assign a pointer value to a char variable. Period. Full stop.

Maybe you mean

char c = 'h';

OK, now the the memory location associated with the variable c contains an eight-bit number that has the numerical value of the (ASCII) character 'h'

how do I empty that variable without putting something else into it?

You don't. I mean you can't. Not in C or C++

Every memory location contains "something." If you store a value there (with an assignment statement, for example) that's what it has. If you declare a variable and don't store anything there, its value is undefined. I mean, it contains "something," but as far as the C program is concerned it could be anything.

Sometimes people store a number with a value of zero in a memory location and say they have "cleared" the memory," but they haven't "cleared" anything. They have stored a number with a value of zero. Maybe zero sounds like "nothing," to some people but actually it's "something."

It is the program's responsibility (the programmer's, actually) to put stuff where it wants to and to use it when it wants to. The memory associated with a char variable can contain any eight-bit number. As far as C and C++ is concerned, there is no such thing as "clearing" memory.

Regards,

Dave

Thanks. Just getting started in this. So used to PHP.

When i'm done with that variable, how do I empty that variable without putting something else into it?

If what you want to achieve is freeing up the memory that variable used so it can be reused for other data, then the way to "clear" it is to make sure it falls out of scope. That can be done by splitting your program into many small functions - as soon as a function returns, the local variables declared within it are forgotten and the space they occupied can be reused by variables in the next function call.

The reason I ask is because I have a sketch that's acting up and I thought maybe it had something to do with this. I'll post the code later in a new thread I guess. Thanks.

I've never tried it, but if you have a pointer to the memory location of the variable, couldn't you use the avr-libc free() statement to free up the memory?

:-?

couldn't you use the avr-libc free() statement to free up the memory?

That only works if the value of the pointer was obtained from one of the alloc family functions. That's kind of irrelevant (to me at least) for programs that are not running under an operating system with lots of resources to give to applications, since I wouldn't be using malloc() (or C++ new or whatever...) in my Arduino sketch.

When a variable goes out of scope, the compiler is free to use that memory for anything that it pleases, including storage for other variables at other times in the program, (but it isn't "cleared" in any meaningful sense of the word).

Regards,

Dave

Does it have to be cleared, or just changed to an unused variable? As in does it have to go away, or just change to a varialbe that won't cause unwanted things to run? If you just want to change it to somethign unused, just do something like:

variable = 0;

just do something like

variable = 0;

Well, if I have a variable that I know will never (legally) take on a value of zero, then setting its value to zero can indicate (somewhere further along in the program) that the programmer hasn't stored anything meaningful there, and that might be useful in some special (and limited) circumstances. But that has nothing to do with the original question posted by the Original Poster:

how do I empty that variable without putting something else into it?

I stand by my previous answer: You don't. You can't. Not in C or C++.

Period. Full stop.

Regards,

Dave

That only works if the value of the pointer was obtained from one of the alloc family functions.

Ah - that makes sense; in the back of my mind I was wondering how it would know how much memory to clear with just the pointer without knowing the type (int, long, char, byte, etc).

:)

Geesh! That was harsh! :-[ :’(

It's true that you can't put nothing in a variable, but if you are only using the char as a holder for charactors, you can use a system which uses a non-zero entry as an empty variable

char i = '0';

Which is actually not logged as 0x00 in memory.

Or you may want to enter

char i = '\0';

Which is the EOT charactor for strings.

... Or you could use pointers and enter the position as NULL

char *i = NULL;

One last thing that you can do is use an "if" statement to check and see if the variable is 0, if it is, don't do the things that would upset it.

eg

char i = 0;
if(i!=0) y=100/i;

I hope I gave you some ideas, because as everyone said, you can't put "nothing" in a variable, but you can work around it. :)

That was harsh!

As a new-with-Arduino guy and a new-to-this-forum guy, I was trying to stay on topic with regards the original question instead of trying to to guess why the Original Poster would want to do what it is that he asked about. The answer to the original question.is simple and unequivocal: You don't. You can't.

I guess that it's obvious that I haven't acclimated myself to the local culture.

I meant no disrespect.

Regards,

Dave

Well, considering that I am a 12 year old arduino programmer, I think I am pretty smart (not bragging at all), but this is not really my good section... But as said before, I don't think you can blank it out... Maybe there is some sort of clear feature? If not, just change it to some character that will never matter... Like a $ or something?

Dave said, "That only works if the value of the pointer was obtained from one of the alloc family functions. That's kind of irrelevant (to me at least) for programs that are not running under an operating system with lots of resources to give to applications, since I wouldn't be using malloc() (or C++ new or whatever...) in my Arduino sketch."

It would be nice to have "new", but it's not supported in the Arduino environment.

Regards,

-Mike

If what you want to achieve is freeing up the memory that variable used so it can be reused for other data, then the way to "clear" it is to make sure it falls out of scope. That can be done by splitting your program into many small functions - as soon as a function returns, the local variables declared within it are forgotten and the space they occupied can be reused by variables in the next function call.

You don't actually have to use functions, you can use local scopes:

int i = 42;
Serial.print(i);
// open a new scope
{
  char h='a';
  Serial.print(h);
}
// h doesn't exist here anymore

You can even do crazy things like this:

int i = 42;
Serial.print(i); // prints 42
// open a new scope
{
  int i = 23; //New variable i, uses a different memory location as the other i
  char h='a';
  Serial.print(h);
  Serial.print(i); // prints 23
}
// h and the inner i don't exist here anymore
Serial.print(i); // prints 42 again

But since the arduino doesn't do multitasking and not much dynamic memory allocation, it doesn't really make sense to do this to save memory. I would only start to do this if i had problems runnihng my sketch otherwise

A good use case for scopes without functions, is if you want to absolutely make sure, that one section of code doesn't influence the other. For example if you copy code from some other sketch and they both use the same variable names.

Bye, NsN

You don't actually have to use functions, you can use local scopes:

Variables declared with a local scope within a function cannot be accessed outside of that scope, but at the beginning of the function, the stack pointer will be moved enough to cover all the variables in all the function's scopes combined (I just checked the generated assembler). So local scopes cannot help you save memory.

Variables declared with a local scope within a function cannot be accessed outside of that scope, but at the beginning of the function, the stack pointer will be moved enough to cover all the variables in all the function’s scopes combined (I just checked the generated assembler).

I stand corrected, thanks for checking.

So the required memory for something like:

int i = 0;
{
  char x='a'
}
{
  char y = 'b'
}

would be the same as

int i = 0;
char x='a'
char y = 'b'

and more than

int i = 0;
char x='a'
x = 'b'

?

That seems like an opportunity to optimize the compiler, but i assume that the algorithm might be not as trivial as it seems.

That seems like an opportunity to optimize the compiler

In the examples above, the compiler is smart enough to store the variables in cpu registers. It takes quite a number of instructions to move the stack pointer (copy over to two 8-bit registers, do 16-bit subtract and move back). Doing that twice for every local scope is perhaps not optimal either.

@tehjrow - Really the important thing to note is that when you create a function, all the variables created in that function are freed from memory when the function ends. So you really don't have to worry about "memory leaks" from that perspective (for base variables for sure). So a lack of "freeing" memory inside your functions is most likely not the reason your sketch is/was acting up.

Did you solve your problem? Running out of memory will make a function end without completing all the instructions and act funny. If you are using lots of integers or defining multiple arrays, you could be hitting limits. I realize without related hardware we can't run your code and recreate the problem but any hint on what is going on may help .. if your still having issues.

Maybe check out this link as well: http://www.arduino.cc/playground/Code/AvailableMemory