使用memcpy()将记录添加到共享内存表中

问题描述 投票:0回答:1

我正在尝试编写一个将记录写入共享内存对象的函数,但是我一直在遇到段错误。当我用valgrind调试时,它说“使用大小为8的未初始化值”。这是下面的代码。

这些是我的头文件中的结构(我知道我与驼峰大小写不一致,下划线对不起,对不起。)table_t中的最后两个指针指向共享内存中保存数据块/记录的部分(无效* db)和保存每个记录信息的区域(db_info * dbInfo)。

typedef struct{
  int dbInfoId;
  int deleted;
}db_info;

typedef struct{
  int shm_fd;
  int numP;
  int control_id;
  char *name;
  int record_size;
  int max_records;
  db_info* dbInfo;
  void* db;
}table_t;


typedef struct{
  int id;
  void* record;
}data_block;

这是add_record函数。添加了一些printf语句,所以我知道错误开始之前代码能走多远,并可以打印某些信息。

int add_record(table_t *tbl, void *record, int record_size){
  printf("code gets this far\n");
  size_t size = sizeof(record_size);
  data_block* rec;
  db_info* recInfo;

  tbl->control_id += 1;
  printf("cid %d\n", tbl->control_id);
  printf("code reaches this point\n");

  /*according to valgrind issuse start here*/

  rec->id = tbl->control_id;
  printf("rec id %d", rec->id);

  rec->record = record;
  printf("code reaches this point\n");

  memcpy(tbl->db, rec, size);

  recInfo->dbInfoId = rec->id;
  recInfo->deleted = 1;

  tbl->db += sizeof(rec);
  tbl->dbInfo += sizeof(recInfo);
  return rec->id;

这是我在add函数中使用的主要内容

#include <sys/stat.h>
#include "data_table.h"
#define MAX_RECORDS 2000000
#define RECORD_SIZE 1024
#define SHM_NAME "abe"

int main(){
  table_t* tbl;
  table_t controlBlock;
  db_info info;
  int com;
  int loop = 0;
  int recId;
  char* rec = "hello world";

  tbl = open_table(SHM_NAME, RECORD_SIZE, MAX_RECORDS);
  printf("this is the number of processes %d\n", tbl->numP);

  tbl->name = SHM_NAME;
 /*this is where i set the pointers to their required locations*/
  tbl->dbInfo = tbl + sizeof(controlBlock);
  tbl->db = tbl + sizeof(tbl->dbInfo);

 /*printing out the different address locations*/
  printf("This is the cb %d, This is the dbInfo %d, and this is the db %d\n",
  &tbl, &tbl->dbInfo, &tbl->db);
 /* my loop. I havent finished all the commands yet*/
  while(loop == 0){
    printf("enter a command (just the number):\n");
    printf("1. Add\n2. Delete\n3. Process\n4. Close\n5. # of processes\n");
    printf("6. exit\n");
    scanf("%d", &com);
    if(com == 1){
      recId = add_record(tbl, rec, RECORD_SIZE);
      printf("record added ID: %d\n", &recId);
    }
    else if(com == 4){
      close_table(tbl);
      loop++;
    }
    else if(com == 5){
      printf("process count %d\n", tbl->numP);
    }
    else if(com == 6){
      loop++;
    }

    }

最后是来自valgrind的错误

this is the number of processes 4
This is the cb -16777872, This is the dbInfo 90521632, and this is the db 90521640
enter a command (just the number):
1. Add
2. Delete
3. Process
4. Close
5. # of processes
6. exit
1
code gets this far
cid 4
code reaches this point
==4042== Use of uninitialised value of size 8
==4042==    at 0x108BDA: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== Use of uninitialised value of size 8
==4042==    at 0x108BE0: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== Use of uninitialised value of size 8
==4042==    at 0x108BFD: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
rec id 4code reaches this point
==4042== Conditional jump or move depends on uninitialised value(s)
==4042==    at 0x4C366F2: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== Conditional jump or move depends on uninitialised value(s)
==4042==    at 0x4C36702: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== Conditional jump or move depends on uninitialised value(s)
==4042==    at 0x4C36707: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== Conditional jump or move depends on uninitialised value(s)
==4042==    at 0x4C36729: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== Use of uninitialised value of size 8
==4042==    at 0x4C36750: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== Use of uninitialised value of size 8
==4042==    at 0x4C36753: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== Conditional jump or move depends on uninitialised value(s)
==4042==    at 0x4C3675F: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== Use of uninitialised value of size 8
==4042==    at 0x108C2C: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== Use of uninitialised value of size 8
==4042==    at 0x108C32: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== Process terminating with default action of signal 11 (SIGSEGV)
==4042==  Bad permissions for mapped region at address 0x1088A0
==4042==    at 0x108C32: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
--4042-- REDIR: 0x50db950 (libc.so.6:free) redirected to 0x4c30cd0 (free)
==4042== 
==4042== HEAP SUMMARY:
==4042==     in use at exit: 0 bytes in 0 blocks
==4042==   total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated
==4042== 
==4042== All heap blocks were freed -- no leaks are possible
==4042== 
==4042== Use --track-origins=yes to see where uninitialised values come from
==4042== ERROR SUMMARY: 14 errors from 12 contexts (suppressed: 0 from 0)
==4042== 
==4042== 1 errors in context 1 of 12:
==4042== Use of uninitialised value of size 8
==4042==    at 0x108C32: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== 1 errors in context 2 of 12:
==4042== Use of uninitialised value of size 8
==4042==    at 0x108C2C: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== 1 errors in context 3 of 12:
==4042== Use of uninitialised value of size 8
==4042==    at 0x4C36750: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== 1 errors in context 4 of 12:
==4042== Conditional jump or move depends on uninitialised value(s)
==4042==    at 0x4C36729: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== 1 errors in context 5 of 12:
==4042== Conditional jump or move depends on uninitialised value(s)
==4042==    at 0x4C36707: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== 1 errors in context 6 of 12:
==4042== Conditional jump or move depends on uninitialised value(s)
==4042==    at 0x4C36702: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== 1 errors in context 7 of 12:
==4042== Conditional jump or move depends on uninitialised value(s)
==4042==    at 0x4C366F2: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== 1 errors in context 8 of 12:
==4042== Use of uninitialised value of size 8
==4042==    at 0x108BFD: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== 1 errors in context 9 of 12:
==4042== Use of uninitialised value of size 8
==4042==    at 0x108BE0: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== 1 errors in context 10 of 12:
==4042== Use of uninitialised value of size 8
==4042==    at 0x108BDA: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== 2 errors in context 11 of 12:
==4042== Conditional jump or move depends on uninitialised value(s)
==4042==    at 0x4C3675F: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== 
==4042== 2 errors in context 12 of 12:
==4042== Use of uninitialised value of size 8
==4042==    at 0x4C36753: memmove (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4042==    by 0x108C27: add_record (in /home/abiodun/Documents/Comp 3713/Assignment3_makeup/main)
==4042==    by 0x108DAA: main (main.c:35)
==4042== 
==4042== ERROR SUMMARY: 14 errors from 12 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

[我知道这很多,而且我的概率很多,但我以前从未使用过共享内存,并且已经尝试解决了几个小时。

这是open_table函数。它以前工作过,但现在我遇到了段错误,所以我不确定。

table_t *open_table(char *name, int record_size, int max_records){

  int shm_fd;
  int table_size;
  void* ptr;
  table_t* tbl;
  table_t controlBlock;
  db_info info;
  data_block data;

  table_size = max_records * sizeof(controlBlock) * sizeof(info) *
    sizeof(data);



  if((shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666)) == -1){
    exit(EXIT_FAILURE);
  }

  ftruncate(shm_fd, table_size);
  ptr = mmap(NULL, table_size, PROT_WRITE | PROT_READ, MAP_SHARED, shm_fd, 
  0);
  tbl = ptr;
  tbl->shm_fd = shm_fd;

  tbl->max_records = max_records;
  tbl->record_size = record_size;
  if(tbl->numP >= 1){
    tbl->numP += 1;
  }
  else{
    tbl->numP = 1;
  }

  return tbl;
}

对代码混乱感到抱歉

c posix valgrind
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.