如何分配适量的内存 (c)

How to allocate the right amount of memory (c)

我目前正在试验 C、内存分配和共享内存。我需要帮助,代码是这样的:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <sys/stat.h>
#include <sys/sem.h>
#include <sys/shm.h>

#include "shared_memory.h"
#include "semaphore.h"
#include "errExit.h"

struct Node {
    int ID;
    char password[10];
    struct Node *next;
};

key_t shmKeyServer = 131;
size_t size = (sizeof(struct Node)) * 100;

int main (int argc, char *argv[]) {
    int shmidServer = alloc_shared_memory(shmKeyServer, size);
    struct Node *node = (struct Node *)get_shared_memory(shmidServer, 0);

    //fill all the structs

    for(int i=0;i<100;i++){
       node->ID = i;
       sprintf(node->password, "%s%i", "campo num:", i);
       node->next = node + sizeof(struct Node);
       printf("you are on %i cicle \n", i);
       node = node->next;
    }

    return 0;
}

函数alloc_shared_memory在这里:

int alloc_shared_memory(key_t shmKey, size_t size) {
   // get, or create, a shared memory segment
   int shmid = shmget(shmKey, size, IPC_CREAT | S_IRUSR | S_IWUSR);
   if (shmid == -1)
       errExit("shmget failed");

   return shmid;
}

get_shared_memory:

void *get_shared_memory(int shmid, int shmflg) {
    // attach the shared memory
    void *ptr_sh = shmat(shmid, NULL, shmflg);
    if (ptr_sh == (void *)-1)
        errExit("shmat failed");

    return ptr_sh;
}

问题是在第8个cicle之后。我收到分段错误。 我认为问题出在内存分配或大小声明上。

问题是线路:

node->next = node + sizeof(struct Node);

因为 typeof(node)struct Node * 此语句将 node 指针递增 sizeof(struct Node) * sizeof(struct Node) 字节(参见 C 中的指针算法)。您希望将 node 指针增加 sizeof(struct Node) 个字节,而不是 sizeof(struct Node) 个节点。

你想要:

node->next = (char*)node + sizeof(struct Node);
// or better:
node->next = (void*)((uintptr_t)(void*)node + sizeof(struct Node));
node->next = (void*)((char*)(void*)node + sizeof(struct Node));
// or 
node->next = node + 1;
node->next = &node[1];

修复了段错误。

行中:

sprintf(node->password, "%s%i", "campo num:", i);

发生未定义的行为。 "%s%i", "campo num:", i 将 12 个字节打印到 node->password 指针中,它只有 10 个字节的内存:

campo num:1

是 11 个字符 + 1 个字节的字符串终止零字节。同样对于大于 10 sprintf 的数字将写入 13 个字节。最好像 snprintf(node->password, sizeof(node->password) 一样使用 snprintf 来防止缓冲区溢出。你也可以 sprintf return 值 int ret = sprintf(..); if (ret > sizeof(node->password)) { err(1, "Overflowed"); }