Codevision/Studio Codefragment Question

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a 50 element array that I periodically need to reinitialize back to zero.

The code fragment follows:
char asciibuf[50];// Declared Global
loopcountval = strlen(asciibuf);// Figure out # of times to loop
for (i = 1;i<=loopcountval;i++)
{
asciibuf[i] = 0;// Clear out ASCII buffer
}

I just use the array as a scratch pad for concantenating strings so when I need a new string I need to clear it out first or end up with an undesired string.

The problem I'm having is when simulating in Studio loopcountval is always a zero, which is confusing to me.

strlen is a Codevision function that requires a char *str as an input.
I tried strlen(&asciibuf) - this generated an error
I tried strlen(&asciibuf[0]) - this returned a 0

Any thoughts on what I am doing wrong?

Thanks,

Erick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, just a thought--

strlen() returns the length of the string in asciibuf, not the width of asciibuf.

If the first character of asciibuf is already a 0x00, then strlen will return 0.

If you want to clear the entire array, you may need a different approach.

HTH. Take care. Lee

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It might be faster to simply zero out the entire buffer every time rather than determine how much you need to zero out. A 50 char buffer should take only ~100 cycles to erase. I'm pretty sure that the strlen function will take much longer to execute (saving variables to the stack, jumping to the code, searching for the null char, pulling variable off the stack) so you'll probably end up wasting more cycles just figuring out how to shorten the erasing part of the code.

Nick

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, Guys - I guess that sleep thing isn't really overrated. I've been programming so much Matlab over the past two weeks that I was thinking srtlen() would return my array size like "size(var)" does in Matlab.

Erick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Erik,

all you have to do, is clear the first byte of the buffer;
strcat () will then take care of all the dirty details.

bis die Tage...
  Mathias

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mathias,

Does srtcat() wipe out the rest of the buffer when it concantenates things? I'm concerned because I use the buffer to generate a varying length string to pump out a serial port and I don't want to be sending left over (at the end of the buffer) characters if my real message is shorter.

Erick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One little note is that the string functions all require that the string be NULL terminated. As such, if you have a 0x00 within your data ANYWHERE (that you actually want to be there), the string functions will treat the first 0x00 as the string terminator. If you are using a string array as a data buffer to send data other than ASCII characters, this could cause you some problems.

Paul

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you absolutely need to clear the entire buffer every time, this would be one way to do it:

char asciibuf[50];// Declared Global
for (i = 0; i < sizeof (asciibuf);i++)
{
    asciibuf [i] = 0;// Clear out ASCII buffer
}

While this is one way of doing it, however, it's not horribly efficient. memset (asciibuf, 0, sizeof (asciibuf)); would be more efficient, since presumably memset() is written in assembly.

Be wary of spending more CPU time trying to figure out how much to clear than just clearing it all. For instance, the strlen() function has to evaluate over the characters starting at the beginning of the array up until it finds a 0. Supposing the array is near full, why waste CPU cycles figuring out where the end is, when you need to go there anyway?

If you really want to do it efficiently, you could write a small assembly routine that looks at the current character and returns if 0. If not 0, it stores a 0, checks that it's not at the limit of the array (a passed parameter), and repeats. However, in the case of a nearly full array, it's still more efficient to just clear it, since the load and check for 0 takes a couple of instructions.

--John ("The art of optimization is looking in the right area." --unknown)

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Erick,

> Does srtcat() wipe out the rest of the buffer when it concantenates things?

no, of course it doesn't.
Why should it?
All strings in C are valid just up to the first null-character (0x00); it doesn't matter, if there's more memory after the strings end.

And (of course) all str...-functions recognize the terminating null-character (0x00);
all str...-functions as well terminate any "new" string by a null-character (by "new" I don't mean the C++ new-operator, which allocates memory).

So, when you "terminate" the string by writing 0x00 to the first position in the array, all str..-functions will see an "empty string" there (and act accordingly).

All you'll have to do after strcat-ting your text is to transmit it one byte at a time, until you'll find the terminating null-character.
You must as well take care, that your catenated string isn't longer than the buffer; the str...-functions won't do this for you!!

bis die Tage...
  Mathias

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On the other hand, Erick;
if you ARE sending only ASCII characters, you could utilize the null characters when writing your ascii_buf. You just write in the chars wanted over the previous chars in the same array, and put a null at the end.
This way you dont have to clear the entire array first; and you dont have to worry about leftovers from the previous string, either.

Eivind

*** Eivind, webmaster ***