June 25, 2024, 05:44:32 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Clearing strings / buffers

Started by Andy, October 12, 2015, 01:32:17 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi,

I've been scratching my head for a while and wondering why anything I have "sent" to a buffer simply echoes back.

Then I realised that the line to "empty" the buffer doesn't work for me.

If I have this:
1. istring buf[8] = ""
2. Then populate "buf" i.e. buf[1] = "1", buf[2] = "2" etc...
3. Use buf[8] = "" to "empty buf"
4. Read and print the contents of "buf" back

I find "buf" is not empty.

If I comment out
buf[8] = ""

and use a FOR / NEXT loop to empty the contents of "buf", it works.

Why??

See attached

Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

King64

To empty the string you must use the following statement:

buf = SPACE$(8)

instead of

buf[8] = ""

:)

Andy

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Andy

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Brian

I sometimes use buf[0]=NULL

That seems to do the trick, and looks more elegant!

Brian

Egil

Quote from: Brian Pugh on October 12, 2015, 07:21:35 AM
I sometimes use buf[0]=NULL

That seems to do the trick, and looks more elegant!

Brian


Maybe it looks more elegant, but you'll only empty the first element of the array that way, so you still have to use a loop.  ;D


Egil
Support Amateur Radio  -  Have a ham  for dinner!

Brian

Egil,

I know what you are saying, but the way Andy defined his ISTRING would have made it
a single-dimension array, anyway

ISTRING buf[8] is an array of 8 characters. If you NULL the first element, the array is unusable

I think I am right, but I have been shot down in flames before!

Brian

Egil

Brian.
Please do not talk of shoot downs... It has happen more often to me that I like to think of.

But you made me so uncertain, that I just had to test.
Instead of Larry's suggestion buf = SPACE$(8) I put in buf[0] = NULL instead.
Below is a screendump of the result.


Support Amateur Radio  -  Have a ham  for dinner!

Brian

OK, I consider myself shot down! Although I seem to remember that it came from a reliable source, Fletchie

Further testing required, methinks...

Brian

Here's some array info from a file that's been sitting for ages on my HD:

define one-dimensional array - 100 IStrings of ten characters max length (including a null character)
Def SingleArray[10,100]:IString

When referencing elements from the array, always use 0 as the first parameter:
assign text to sixth string
SingleArray[0,5]="Hello"

define two-dimensional array - 25 indexes of 10 IStrings of eight characters max length (including a null character)
Def MultiArray[8,25,10]:IString

assign text to eighth string of thirteenth index
MultiArray[0,12,7]="Goodbye"

Egil

Interesting examples you got there Brian.
I have almost no experience with multi-dimensional arrays in PC programs, only used them for microcontroller applications back in the stone age. But have forgotten all about it.


Egil
Support Amateur Radio  -  Have a ham  for dinner!

LarryMc

Quote from: andy1966 on October 12, 2015, 01:32:17 AM
If I have this:
1. istring buf[8] = ""
2. Then populate "buf" i.e. buf[1] = "1", buf[2] = "2" etc...
3. Use buf[8] = "" to "empty buf"
4. Read and print the contents of "buf" back

I find "buf" is not empty.

If I comment out
buf[8] = ""

and use a FOR / NEXT loop to empty the contents of "buf", it works.

Why??
Andy
There are several things going on with your stated facts.
Lets start with
Quote1. istring buf[8] = ""
if you had used string buf="" you would have used up 255 memory locations and the first memory location would be automatically be set to CHR$(0) NULL and the remaining memory can be all trash because IWBasi stop reading when it hits the NULL.
You used ISTRING buf[8]="" which sets every element to NULL.  IWB does that because it has no way of telling if you are going to use the character array as an array of characters or if you are going to use it like you normally use strings.
Then you populate elements of the characters array with this:
Quote2. Then populate "buf" i.e. buf[1] = "1", buf[2] = "2" etc...
then you tried to clear the string with this
Quote3. Use buf[8] = "" to "empty buf"
WHAT?? did I say TRIED??
If I set memory location buf[8] to a NULL what does that have to do with my original character array.
Let's see.
ISTRING buf[8] defines our array as having 8 characters. I know, you already see what you missed.
Our array elements are buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]
Setting buf[8] to NULL could be corrupting some other part of the computer.

In ya'lls post while I was type this there was mention of loops and putting a NULL in the first element.


1st, I'm not talking about referencing multi-dimension arrays where you have to use a 0 in the 1st position.

If you define  an ISTRING then how you clear it (and where you can put the NULL) depends on how you are going to use the character array.

If you are going to read/write it like a STRING then you can just use buf[0]="" or buf=""

And this is a problem I have run into before when reading and printing.  What if I have an embedded NULL before the end of the array , i.e. multiple NULLS. Not everything will be read.
The array will only be cleared up until it encounters the first NULL.

If you use the windowsdk.inc file in your programs there is a function that I use a ton of times to clear my variables especially my nested structures.

rtlzeromemory(gpSngl,len(gpSngl))
pointer to a memory location, and the length of that memory

ISTRING buf[8]
rtlzeromemory(buf,len(buf))

and you can call it as many times as needed in a program to rezero an array and it is faster than doing it with IWBasic

And that's my story and I'm sticking to it.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Brian

Good explanation, Larry - and I like the RtlZeroMemory call. Gonna use that now
instead of NULLing

Brian