c-结构存储器的动态阵列



我正在尝试创建客户端的动态数组,但没有成功。这是我的密码。当我运行这个代码时,输出是


3
� H
3
4332
3
8939

我想这是打印记忆的东西,但我不知道为什么。我把我的代码放在这里

int client_counter = 0;
typedef struct client
{
char *pid;
char *message;
}Client;

void store (Client * client_array, char *buf)
{  
Client c;
c.pid = strdup (strtok (buf, ":"));
c.message = strdup (strtok (NULL, ""));
client_array[client_counter++] = c;
}
int main () {

Client* client_array = malloc (sizeof (struct client));
char buf1[50] = { "1245:message" };
store (client_array, buf1);

char buf2[50] = { "4332:message" };

store (client_array, buf2);
char buf3[50] = { "8939:message" };
store (client_array, buf3);

for (int i = 0; i < client_counter; i++)
{
printf ("%dn", client_counter);
printf ("%sn", client_array[i].pid);
}
return 0;
}

我已经尝试过使用这个:

client_array = realloc(client_array, sizeof(struct client) * (client_counter + 1));

就在这一行之后的店内功能。

client_array[client_counter++] = c;

但它也不起作用。

如果没有足够的空间,则需要分配额外的内存。现在,你为一个分配了足够的资源,但你试图访问三个。

不要忘记将新内存块的指针返回到main!在下文中,这是通过将指针传递给调用者的指针来完成的。store通过传递的指针修改调用者的指针。

// Sets errno and returns 0 on error.
int store(Client ** client_array_ptr, char *buf) {  
Client* new_client_array = realloc(*client_array_ptr, sizeof(Client) * (client_counter + 1));
if (!new_client_array)
return 0;
*client_array_ptr = new_client_array;

// ...
new_client_array[client_counter++] = c;
return 1;
}

int main() {
Client* client_array = NULL;
// ...
if (!store(&client_array, buf1)) {
perror("malloc");
exit(1);
}
// ...
if (!store(&client_array, buf2)) {
perror("malloc");
exit(1);
}
// ...
free(client_array);
return 0;
}

原始代码执行范围外访问,因为它试图将多个数据存储在只为一个元素分配的缓冲区中。

要使用realloc(),您必须注意,C中函数的参数是所传递内容的副本。修改被调用者函数内部的参数不会影响调用者中传递的内容。您应该将指针传递到应该修改的内容,以使函数修改调用者的本地内容。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int client_counter = 0;
typedef struct client
{
char *pid;
char *message;
}Client;

void store (Client ** client_array, char *buf)
{  
Client c;
c.pid = strdup (strtok (buf, ":"));
c.message = strdup (strtok (NULL, ""));
*client_array = realloc(*client_array, sizeof(struct client) * (client_counter + 1));
(*client_array)[client_counter++] = c;
}
int main () {

Client* client_array = malloc (sizeof (struct client));
char buf1[50] = { "1245:message" };
store (&client_array, buf1);

char buf2[50] = { "4332:message" };

store (&client_array, buf2);
char buf3[50] = { "8939:message" };
store (&client_array, buf3);

for (int i = 0; i < client_counter; i++)
{
printf ("%dn", client_counter);
printf ("%sn", client_array[i].pid);
}
return 0;
}

我会用不同的方式来做。

typedef struct
{
char *pid;
char *message;
}client_TypeDef;
typedef struct
{
size_t size;
client_TypeDef clients[];
}clients_TypeDef;
clients_TypeDef *add(clients_TypeDef *clients, const char *pid, const char *message)
{
size_t newsize = clients ? clients -> size + 1 : 1;
client_TypeDef client = {.pid = strdup(pid), .message = strdup(message)};
if(client.pid && client.message)
{
clients = realloc(clients, sizeof(*clients) + newsize * sizeof(clients -> clients[0]));
if(clients)
{
clients -> size = newsize;
clients -> clients[newsize - 1] = client;
}
}
else
{
free(client.pid);
free(client.message);
clients = NULL;
}
return clients;
}

最新更新