结构体中指向 char 的灵活指针数组

发布于 2024-09-24 03:29:57 字数 1080 浏览 8 评论 0原文

我正在尝试使用以下结构实现环形缓冲区

/*head, tail are indexes of the head and tail of ring buffer
 *count is the number of elements; size is the max size of buffer
 *rbArray is an array to pointer of char used to store strings    
 */
struct rb{
  int head;
  int tail;
  int count;
  int size;
  char *rbArray[];
};

然后我使用以下函数创建字符串缓冲区:

 struct rb *create(int n){
     /*allocate memory for struct*/
     struct rb *newRb = (struct rb*)malloc(sizeof(struct rb)+ n*sizeof(char *));
     assert(newRb);

     int i;
     for(i=0;i<n;i++)
        newRb->rbArray[i] = NULL;

     /*put head and tail at the beginning of array
     initialize count, set max number of elements*/
     newRb->head = 0;
     newRb->tail = 0;
     newRb->count = 0;
     newRb->size = n;

     return newRb;
   }

我在 main 中调用此函数:

 struct rb *newRB = (struct rb*)create(100);

但是,我在为结构分配内存的步骤中遇到了问题。在调试模式下,我可以看到 head、tail、count 的值被分配了非常奇怪的大数字,但不是 0。程序在第一步之后挂起,没有抛出任何异常。

有人可以帮我解释这个问题吗?我该如何修复它?

I'm trying to implement a ring buffer with the following struct

/*head, tail are indexes of the head and tail of ring buffer
 *count is the number of elements; size is the max size of buffer
 *rbArray is an array to pointer of char used to store strings    
 */
struct rb{
  int head;
  int tail;
  int count;
  int size;
  char *rbArray[];
};

Then I use the following function to create a string buffer:

 struct rb *create(int n){
     /*allocate memory for struct*/
     struct rb *newRb = (struct rb*)malloc(sizeof(struct rb)+ n*sizeof(char *));
     assert(newRb);

     int i;
     for(i=0;i<n;i++)
        newRb->rbArray[i] = NULL;

     /*put head and tail at the beginning of array
     initialize count, set max number of elements*/
     newRb->head = 0;
     newRb->tail = 0;
     newRb->count = 0;
     newRb->size = n;

     return newRb;
   }

I call this function in main:

 struct rb *newRB = (struct rb*)create(100);

However, I have problem right at the step allocating memory for struct. In the debugging mode, I can see the value of head, tail, count, were assigned very strange large numbers but not 0. And program hangs after this very first step without throwing me any exception.

Could someone help me explain this problem please? How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

也只是曾经 2024-10-01 03:31:37

非常感谢你们的帮助。我让它与 char** 一起使用,它绝对比灵活地使用数组成员容易得多。

然而,我想知道,当你有
字符**数组;
你可以使用 array[i],它会给你一个指向 char 的指针。为什么如果我们有
字符*数组;
我们不能使用 array[i] 来获取 char 吗?

希望我在这里说得足够清楚。

谢谢

Thank you guys a lot for helping. I made it work with char** and it's definitely much easier than working flexibly array member.

However, I wonder, when you have
char **array;
you can use array[i] and it will give you a pointer to char. Why if we have
char *array;
we cannot use array[i] to get a char?

Hope I make myself clear enough here.

Thanks

风月客 2024-10-01 03:31:12

以下应该是执行相同操作的更短方法:

struct rb *create(int n)
{
    struct rb * newRb = calloc(sizeof(struct rb) + n*sizeof(char*), 1);
    newRb->size = n;    
    return newRb;
}

这将所有分配的空间设置为 0,然后正确设置 size 字段。

The following should be a shorter way to do the same:

struct rb *create(int n)
{
    struct rb * newRb = calloc(sizeof(struct rb) + n*sizeof(char*), 1);
    newRb->size = n;    
    return newRb;
}

This sets all the allocated space to 0 and then sets the size field correctly.

﹏半生如梦愿梦如真 2024-10-01 03:30:44

我很难阅读您的代码,但根据我收集的信息,您可能想要执行以下操作:

struct rb *create(int n)
{
    struct rb newRb = calloc(1, sizeof(struct rb));
    newRb->rbArray = calloc(n, sizeof(char*));

    newRb->count = n;

    return newRb;
}

calloc 将确保分配的空间的内容设置为零。另外,在第一次调用 malloc 时仅分配额外的 n*sizeof(char*) 似乎很可疑。

I have a hard time reading your code, but from what I gather, you probably want to do something along the lines of:

struct rb *create(int n)
{
    struct rb newRb = calloc(1, sizeof(struct rb));
    newRb->rbArray = calloc(n, sizeof(char*));

    newRb->count = n;

    return newRb;
}

calloc will make sure the contents of the allocated space are set to zero. Also, just allocating an additional n*sizeof(char*) with your first call to malloc seems fishy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文