【问题标题】:C pointer to pointer and queue指向指针和队列的 C 指针
【发布时间】:2020-01-30 08:06:57
【问题描述】:

我有一个接受指向指针的方法

typedef struct message_struct_t {
  struct mystruct_t **obj;  //not sure if this correct
  char name[50];
};



int server(mystruct _t **obj) {
   //i am able to push this to a queue
   //but first i store this in my temp struct
   message_struct_t *message = malloc(sizeof(message_struct_t));
   message->obj = obj;
   message->name = "Jaime";
   myqueue_enqueue(message);

   //some mutex and locking mechanism here
   for(int i = 0; i < num_threads; i++)
      pthread_create(&threads[i], NULL, send_message, NULL);
}

我需要获取这个对象并将其传递给我将在线程池中处理的方法

   int send_message() {
       //i check and there is an item in my queue, but when i try to assign it to a new struct for 
       //handling i get errors
       struct message_struct *received = malloc(sizeof(message_struct)); //ERROR HERE
       printf("did I get here ? \n", received->name"); //ERROR

       free(received);
   }

我得到的错误是这样的

  ==20730==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61d0000102b0 at pc 0x55bab7c01cf5 bp 0x7f4d8a2fe980 sp 0x7f4d8a2fe0f8 
    READ of size 2089 at 0x61d0000102b0 thread T1
        #0 0x55bab7c01cf4 in printf_common(void*, char const*, __va_list_tag*) (/project_main+0x5ccf4)

0x61d0000102b0 is located 0 bytes to the right of 2096-byte region [0x61d00000fa80,0x61d0000102b0)
allocated by thread T1 here:
    #0 0x55bab7c6c7b0 in malloc 

in printf_common(void*, char const*, __va_list_tag*)
Shadow bytes around the buggy address:
  0x0c3a7fffa000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    
  0x0c3a7fffa010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    
  0x0c3a7fffa020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    
  0x0c3a7fffa030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    
  0x0c3a7fffa040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    
=>0x0c3a7fffa050: 00 00 00 00 00 00[fa]fa fa fa fa fa fa fa fa fa
  0x0c3a7fffa060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa    
  0x0c3a7fffa070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa    
  0x0c3a7fffa080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa    
  0x0c3a7fffa090: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa    
  0x0c3a7fffa0a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa    
Shadow byte legend (one shadow byte represents 8 application bytes): 
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb

现在我是 C 的新手,我可能在某个地方犯了一些明显的错误,我希望得到一些关于我可能在哪里分配错误的建议(也许是指针?)。

【问题讨论】:

    标签: c pointers queue


    【解决方案1】:

    您的send_message 应如下所示:

    void *send_message(void *args) {
        struct message_struct *received  = *((struct message_struct *) args);
        // ....
        free(received );
    }
    

    如果您无法更改 send_message 原型,则必须将其包装在 void * func(void *args) 中以某种方式,我认为这对于维护接收到的消息的状态来说太过分了。

    另外,您需要确保正确释放获取的资源。

    【讨论】:

    • 谢谢,但是如果我使用队列,我会尝试将请求放入队列中,然后在发送消息方法中,当我在 send_message 中从队列中提取时进行处理
    • 如果我想通过thread creation 传递它会很好,但我想排队工作。这是我在某处犯错的地方:(
    • @DunkRuZ,那你先看看如何实现并发队列。您可以轻松地在各个线程中工作,而无需锁定。
    猜你喜欢
    • 2021-08-14
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 2023-03-11
    • 2017-05-13
    • 1970-01-01
    • 2011-04-24
    相关资源
    最近更新 更多