c - 访问冲突读取位置 0xfdfdfdfd。 IN C

标签 c

我想得到你的帮助。我正在调试我的代码,我发现编译器在这一行将我踢出

 if(strcmp(text1[i],text1[i+1]) > 0 )

我得到的错误消息是

Unhandled exception at 0x542ed442 (msvcr100d.dll) in LB11_12.exe: 0xC0000005: Access violation reading location 0xfdfdfdfd.

然后它问我是想中断还是继续 为什么会这样?

这是我的代码

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define LINES 4
#define length 100

void SortText1(char **text1);
void SortText2(char **text2);
void PrintStr1(char **text1);
void PrintStr2(char **text2);
void main ()
{
    int i;
    char **text1;
    char **text2;
    char **text3;
    text1=(char **)malloc(LINES*sizeof(char *));
        if(!text1) // Terms - if there is not enough memory,print error msg and exit the program.
            {
                printf("alloc failed\n");
                return ;
            }
     for (i=0 ; i<LINES; i++)
     {
        text1[i] =(char*) malloc(length*sizeof(char));
     }

    text2=(char **)malloc(LINES*sizeof(char *));
        if(!text2) // Terms - if there is not enough memory,print error msg and exit the program.
            {
                printf("alloc failed\n");
                return ;
            }
     for (i=0 ; i<LINES; i++)
         {
        text2[i] =(char*) malloc(length*sizeof(char));
         }
     text3=(char **)malloc((LINES+LINES)*sizeof(char *));
        if(!text3) // Terms - if there is not enough memory,print error msg and exit the program.
            {
                printf("alloc failed\n");
                return ;
            }
     for (i=0 ; i<LINES+LINES; i++)
     {
        text3[i] =(char*) malloc(length*sizeof(char));
     }

    printf("First string,Please enter %d names (only capital letters,without spaces):", LINES);
     for ( i=0 ; i<LINES ; i++)
         {
          gets(text1[i]);
         }
    printf("Second string,Please enter %d names (only capital letters,without spaces):", LINES);
    for ( i=0 ; i<LINES ; i++)
      {
          gets(text2[i]);
      }
    PrintStr1(text1);
    PrintStr2(text2);
    SortText1(text1);
    system("pause");
    return ;
}
void SortText1(char **text1)
{

    int i,j,k;
    for (i=LINES-1 ; i>0 ; i--)
    {
        for ( j=0 ; j<i ; j++ )
        {
            if(strcmp(text1[i],text1[i+1]) > 0 )
            {
                char *temp=text1[i+1];
                text1[i+1]=text1[i];
                text1[i]=temp;
            }
        }
    }
    printf("The first string sorted:\n");
    for ( k=0 ; k<LINES ; k++)
        {
            printf("%s ",text1[k]);
        }
    printf("\n");
}
void PrintStr1(char **text1)
{
    int i;
    printf("The first string of NAMES:\n");
    for ( i=0 ; i<LINES ; i++)
        {
            printf("%s ",text1[i]);
        }
    printf("\n");
}
void PrintStr2(char **text2)
{
    int i;
    printf("The second string of NAMES:\n");
    for ( i=0 ; i<LINES ; i++)
        {
            printf("%s ",text2[i]);
        }
    printf("\n");
}

最佳答案

for (i=LINES-1 ; i>0 ; i--)
...
    if(strcmp(text1[i],text1[i+1]) > 0 )

在第一次迭代中,您将访问 text1[LINES-1]text1[LINES]。但是 text1[LINES] 超出范围,因此访问是非法的 - 双重非法,因为您也没有为其分配任何内存。


旁注:有趣的是,神奇的 0xfdfdfdfd 位于可访问空间之外,专门针对差一错误。

关于c - 访问冲突读取位置 0xfdfdfdfd。 IN C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18913710/

相关文章:

c - 我的 C 代码中的错误,用于反转字符串中的单词

循环FOR生成两个数字序列

c - 如何在C中把帕斯卡三角形倒过来,让第一行最后打印,最后一行最先打印?

c - Trie执行效率

c - 如何将一个文件中以 #include 开头的所有行写入到另一个文件中

c - gcc gdb building - "lvalue required as increment operand"on *((void **)__o->next_free)++ = ((void *)datum);

c++ - 拾色轮

c - 为什么 'EINTR' 未声明?

c - C 中包含双 header 的实际用例是什么?

c - 在无符号整数之间进行减法是否安全?