07-28-2005 12:30 PM
07-28-2005 12:39 PM
Tiwi,
From the Labwindows function panel for strcpy:
"This function copies a source string (including the terminating ASCII NUL byte) into a target string. If copying takes place between objects that overlap, the behavior is undefined."
From what this says you are exposing yourself to bugs with the code posted above.
07-28-2005 12:50 PM
07-28-2005 12:59 PM - edited 07-28-2005 12:59 PM
Tiwi,
Give the following a try:
char readBuffer[READBUFFERSIZE], my_Temp_Buffer;
if (strlen(readBuffer) >= (READBUFFERSIZE -1) )
{
for (index = 0; index < READBUFFERSIZE - 1; index ++)
{
my_Temp_Buffer = readBuffer[index +1];
readBuffer[index] = my_Temp_Buffer;
}
readBuffer[READBUFFERSIZE-1] = 0;
}
strcat(readBuffer, tempReadBuffer);
Message Edited by DaveC on 07-28-2005 01:00 PM
07-28-2005 02:09 PM
07-29-2005 03:24 AM
Alternatively you could use the CVI function CopyString(). This does allow source and target strings to overlap.
JR
08-02-2005 08:26 AM
The behaviour is not undefined, it is perfectly understandable. Copying characters backwards works OK, copying them forwards does not work OK, as you're modifying characters that have yet to be copied. Consider the following:
char Test1[30] = "Hello, World";
char Test2[30] = "Hello, World";
char Test3[30] = "Hello, World";
strcpy (Test1, &Test1[2]);
strncpy(&Test2[2], Test2, 28);
strcpy (&Test3[2], Test3);
Test1 will be "llo, World"
Test2 will be "HeHeHeHeHeHeHeHeHeHeHeHeHeHeHe"
Test3 will cause a GPF, because an endless string is copied.
The way you (the OP) considered making your code would work perfectly, since it copies the characters backwards.
You could also consider a sliding index pointer which points at the first character. Something like the following:
char readBuffer[1024] = "";
int BufPtr = 0;
void AddCharsToCircularBuf(char *Chars, int NrOfChars)
{
int i;
for (i=0; i < NrOfChars; i++) {
readBuffer[BufPtr++] = Chars[i];
if (BufPtr == 1024) BufPtr = 0;
}
// after this loop BufPtr points BEHIND the last char
// which is also the BEGINNING of the circular buffer.
}
When you use some kind of start character in the buffer, you could start reading at index BufPtr and scan the buffer for the start character, and copy all characters until BufPtr-1 in a same kind of loop (jumping back from 1023 to 0). Or, you could maintain 2 pointers, one for the starting position (which would be modified by the reading routine) and one for the ending position (modified by the writing routine).
08-02-2005 09:03 AM
08-02-2005 10:03 AM