结构体中指向 char 的灵活指针数组
我正在尝试使用以下结构实现环形缓冲区
/*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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
非常感谢你们的帮助。我让它与 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
以下应该是执行相同操作的更短方法:
这将所有分配的空间设置为
0
,然后正确设置size
字段。The following should be a shorter way to do the same:
This sets all the allocated space to
0
and then sets thesize
field correctly.我很难阅读您的代码,但根据我收集的信息,您可能想要执行以下操作:
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:
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.