0

我有一些小烦恼而不是错误,但我无法弄清楚它为什么会发生。我有一个数组索引和 2D gchar 数组,声明如下:

gchar FilterArray[10][51];
static uint8_t ArrayIndex = 0;
static gchar ScanLine[9640];

现在,我第一次使用数组索引时,它的值不知何故为 115。在使用它之前,我没有在代码中的任何地方设置它。如果我在 strcpy 命令中使用它之后才打印出来,它的值为 115。如果我在 strcpy 命令之前打印出来,它的值在程序运行期间是正确的。

while(FilterAmount != 0)
{
    g_io_channel_read_chars (source,(gchar *) ScanLine,1,&BytesRead,&GlibError);
    if(ScanLine[0] == FilterTerminator[0]) { 
        printf("Array Index: %i\n", ArrayIndex);
        if(strlen(FilterName) > 0){
            printf("Array Index: %i\n", ArrayIndex); //if I only print before, value is correct
            strcpy(FilterArray[ArrayIndex],FilterName); 
            printf("Array Index: %i\n", ArrayIndex); //if I only print after, value is incorrect
            ArrayIndex++; 
            FilterAmount--; 
            FilterName[0] = '\0'; 
        }
    }
    else {
        strcat(FilterName, ScanLine);
    }
}
4

1 回答 1

0

大概 ,FilterName过长并且超出了 end和 overstrcpy(FilterArray[ArrayIndex],FilterName)的写入。如果在可能之前打印,这似乎是正确的,因为该值保存在寄存器中并且不会从被覆盖的内存位置再次加载。FilterArrayArrayIndexArrayIndexstrcpy

于 2015-05-06T09:30:45.457 回答