我的结构是从以前的一个重叠的一些数据(编辑),我在与指针和内存出现问题

问题描述:

嗨,大家好,这是我的数据结构:我的结构是从以前的一个重叠的一些数据(编辑),我在与指针和内存出现问题

struct user_node { 
    char *name; 
    struct user_node *next; 
    struct msg_node *msgnext; 
}; 

struct msg_node { 
    char *sender; 
    char *receiver; 
    char *title; 
    char *content; 
    struct msg_node *msgnext; 
}; 
  • 每个用户都拥有一个名字和一个链接列表存储消息。

我已经在用户创建/删除实现,但我无法执行新的味精,我使用这个功能:

struct msg_node* sendForm(char** res) { // Format msg before sending 

    struct msg_node *temp = (struct msg_node*) malloc(sizeof(struct msg_node)); 

    char *aux = malloc(sizeof(char) * 400); 
    int i; 

    temp->sender = res[1]; 
    temp->receiver = res[2]; 
    temp->title = res[3]; 

    for (i = 4; i < (n_spaces + 1); i++) { 
     if (res[i] != NULL) { 
      strcat(aux, res[i]); 
      strcat(aux, " "); 
     } 
    } 

    temp->content = aux; 
    return temp; 
} 

int sendMsg(struct msg_node* msg) 
{ 
    struct user_node* user = user_database; 

    if (user == NULL) { 
     printf("There aren't users on the system.\n"); 
     return -1; 
    } 

    while (user != NULL) { 
     if (strncmp(user->name, msg->receiver, strlen(msg->receiver)) == 0) { 
      msg->msgnext = user->msgnext; 
      user->msgnext = msg; 
      readmsglist(); 
      return 0; 
     } 

     user = user->next; 
    } 

    printf("User '%s' not found on the system.\n", msg->receiver); 

    return -1; 
} 

这实际上是工作,如果我只输入一个味精,但当我尝试实现第二个,这是发生了什么事:

执行:

Welcome to the Program> Enter command (h for help):register user 
User created 

There are 1 users right now 

> Enter command (h for help):send RandomUser1 user content1 a b c 
Receiver -> user 
Sender -> RandomUser1 
Subject -> content1 
Content: 
a b c 

> Enter command (h for help):send RandomUser2 user content2 d e f 
Receiver -> user 
Sender -> RandomUser2 
Subject -> content2 
Content: 
d e f 

Receiver -> user 
Sender -> RandomUser2 
Subject -> content2 
Content: 
a b c 

and so more... 

最奇怪的部分在这里内容不会重叠,但其他部分实际上是重叠的。

Btw对不起,我以前的帖子,不得不大量代码。

编辑:(数据输出功能)

void readmsglist() { // Show the msg of the first user Test Purposes 
    struct msg_node** ptr = &user_database->msgnext; 

    while (*ptr) { 
     printf("Receiver -> %s\n", (*ptr)->receiver); 
       printf("Sender -> %s\n", (*ptr)->sender); 
       printf("Subject -> %s\n", (*ptr)->title); 
       printf("Content:\n %s\n", (*ptr)->content); 
       ptr = &(*ptr)->msgnext; 
    } 

} 

EDIT2(更多信息):

一,功能在这里称为:

case 24: //Send messages 
      if (countSpaces(input) > 2) { 
       sendMsg(sendForm(cut(input))); 

      } else { 
       E_Type(); 
      } 
      break; 

这是切换功能:

char** cut(char str[]) { 

    char ** res = NULL; 
    char * p = strtok(str, " "); 
    n_spaces = 0; // Global variable (int) 

    /* split string and append tokens to 'res' */ 

    while (p) { 
     res = realloc(res, sizeof(char*) * ++n_spaces); 

     if (res == NULL) 
      exit(-1); /* memory allocation failed */ 

     res[n_spaces - 1] = p; 

     p = strtok(NULL, " "); 
    } 

    /* realloc one extra element for the last NULL */ 

    res = realloc(res, sizeof(char*) * (n_spaces + 1)); 
    res[n_spaces] = 0; 

    return res; 
} 

inp ut就像 - >发送:

+0

究竟是什么与哪个数据重叠在哪里? – jwdonahue

+0

@jwdonahue在执行过程中你可以看到msg2如何超过ms1的一部分 – Edw4rd

+0

@ Edw4rd,你的意思是输出不正确?您不显示任何实际输出数据的代码。我们也看不到你是如何调用sendForm或sendMsg的。 – jwdonahue

Edw4d和我有一个long discussion and debugging session

消息数据被用户输入覆盖,因为每个消息的输入缓冲区中的数据位置都存储在每个消息节点中,而不是实际上将字符串复制到新的缓冲区中。这就解决了这个问题:

char** cut(char str[]) 
{ 
    char *p = strtok(str, " "); // We don't need the first token. 
    p = strtok(NULL, " "); 

    char **res = calloc(sizeof(char*), 4); /* allocate a buffer full of NULL's*/ 
    if (res == NULL) exit(-1); /* memory allocation failed */ 

    /* split string and append tokens to 'res' */ 
    for (int idx = 0; idx < 3; idx++) 
    { 
     res[idx] = malloc(strlen(p) + 1); 
     strcpy(res[idx], p); 
     p = strtok(NULL, " "); 
    } 

    p[strlen(p)] = ' '; // Put the space back where strtok wrote the null character. 
    res[3] = malloc(strlen(p) + 1); 
    strcpy(res[3], p); 
    return res; 
} 

MsgNode* sendForm(char** res) { // Format msg before sending 

    struct msg_node *temp = (struct msg_node*) malloc(sizeof(struct msg_node)); 

    temp->receiver = res[0]; 
    temp->sender = res[1]; 
    temp->title = res[2]; 
    temp->content = res[3]; 

    return temp; 
} 
+0

这个人帮了我3-4个小时非常感谢您的先生。我真的很喜欢。 – Edw4rd

+0

@ Edw4rd,如果我的C编程技能不那么生锈,我们会在不到一个小时的时间内完成。尽管我需要一些非常需要的练习。顺便说一句:在原始的sendForm函数中也有一个错误。 C数组从零开始,而不是一个。 – jwdonahue