This question already has answers here:
Crash or “segmentation fault” when data is copied/scanned/read to an uninitialized pointer
                                
                                    (5个答案)
                                
                        
                                2年前关闭。
            
                    
我想将字符串数组的每个元素复制到另一个字符串数组,但是当我复制一个字符串时,整个数组都将更改为该字符串。以下是代码集。

这是有问题的代码行:

strcpy(MsgList[i].ga_data, a_Database[i].ga_data);


当我检查内容时
a_Database [i] .ga_data,如下所示

"1240,message 7:War of the worlds"

"1238,message 5:Life of this world"

"1236,message 3:world is not enough"

"1235,message 2:What a world!"


因此,结果不是在MsgList[i].ga_data中填充相同内容,而是变成了“ 1235,消息2:真是个世界!” 4次,这是最后复制的元素。

typedef enum
{
  ACK,
  NACK,
  DELETE
}eMsgStatus_t;

typedef struct _Message
{
  eMsgStatus_t status;
  char *ga_data;
  uint16_t time;
}Message_t;
Message_t MessageList[8]={
  {ACK,"1234,message 1:Hello world",1000},
  {NACK,"1235,message 2:What a world!",1011},
  {NACK,"1236,message 3:world is not enough",1022},
  {ACK,"1237,messsge 4:Cruel world",1033},
  {NACK,"1238,message 5:Life of this world",1044},
  {ACK,"1239,message 6:Around the world in 80 days",1055},
  {NACK,"1240,message 7:War of the worlds",1066},
  {ACK,"1241,message 8:End of World",1077}
};

Message_t a_Database[20];
Message_t MsgList[20]= {0};

int main()
{
  for (i = 0; i < idx /* total unread message */; ++i)
  {
      strcpy(MsgList[i].ga_data, a_Database[i].ga_data);
      MsgList[i].ga_data[14] = '\0';
  }
}

uint8_t  GetMessages (Message_t *pg_Message)
{
  char i;uint8_t idx = 0;
   for (i = 0; i < 8 /* total message */; ++i)
  {
    if(NACK == MessageList[i].status)
    {
      pg_Message[idx].status = MessageList[i].status;
      pg_Message[idx].ga_data = MessageList[i].ga_data;
      pg_Message[idx].time = MessageList[i].time;
      idx++;
    }
  }
  return idx;
}

最佳答案

首先,您正在破坏此处的内存:

strcpy(MsgList[i].ga_data, a_Database[i].ga_data);


由于我看不到MsgList [i] .ga_data的分配位置,
我以为不是。

然后在此处用魔术数字'14'制成以null结尾的字符串:

 MsgList[i].ga_data[14] = '\0';


也会导致一些内存损坏。

甚至假设您在某处进行分配,由于您在14处执行空终止,因此您可以在MsgList [i] .ga_data中观察到完整的字符串,这很奇怪:

1235,message 2
1236,message 3
1238,message 5
1240,message 7

07-26 05:23