在不使用 malloc 的情况下将固定大小的对象分配给指针(指向结构)
我有一个指向 struct list* ptr
的指针 该结构包含一个 char 和一个指向下一个结构的指针,它们用于结构数组,如下所示:
list array[setSize]; //setSize is defined as 20
如果不使用 malloc,我需要初始化 list* ptr
以便它具有以下所有元素数组。
我已尝试以下操作,但出现分段错误。我相信我完全误解了指针的工作原理。 (我已经很久没有接触C了)
int j = 0;
ptr = sizeof(array[j]); //i was thinking this would allocate space
for(j; j < setSize; j++)
array[j].next = array[j+1].next;
ptr = array; //i thought this would point to the beginning of the array
我看过很多关于C和链表的网站。我有书,但它们没有提供有关指针或结构的这种特殊用法的足够细节。我已经尝试了一段时间,但我确信我错过了一些东西。
我只需要了解我哪里出了问题以及该朝哪个方向前进。
感谢您的任何和所有帮助。
下面是包含更多代码的重新发布:
//this should initialize array so that ptr contains all array elements
void initialize ( list array[] ) {
int j;
for(j = 0; j < setSize - 1; j++){
array[j].next = array[j+1].next; //using the & does not seem to work, so I don't use it
array[j].next = NULL;
}
ptr = &(array[0]);
}
稍后我在自己的 malloc 函数中使用它(这就是 seg 错误发生的地方)
//this should allocate a new list cell in array
list* my_malloc () {
list* temp;
temp = ptr;
if(ptr = 0) {printf("Empty!\n"); return;}
else{ //the next line creates the seg fault
ptr = (*ptr).next;
}
return temp;
}
list* cell ( char n, list* next ) {
list* x = my_malloc();
x->value = n;
return x;
x->next = next;
return x;
}
main ( void* argv, int argc ) {
initialize(array);
list* x = nil;
char ch = a;
int i;
for ( i = 0; i < setSize; i++ )
x = cell(ch,x);
for ( i = 0; i < 10; i++ ) {
list* y = x->next;
my_free(x); //this is my own free function
x = y;
};
for ( i = 0; i < 10; i++ )
x = cell(ch,x);
}
I have a pointer to a struct list* ptr
The struct contains a char and a pointer to the next struct and these are used for an array of structures like so:
list array[setSize]; //setSize is defined as 20
Without using malloc, I need to initialize list* ptr
so that it has all the elements of the array.
I have tried the following and I get a segmentation fault. I believe I am utterly misunderstanding how pointers work. (I haven't touched C in a long while)
int j = 0;
ptr = sizeof(array[j]); //i was thinking this would allocate space
for(j; j < setSize; j++)
array[j].next = array[j+1].next;
ptr = array; //i thought this would point to the beginning of the array
I have looked at a lot of sites about C and linked list. I had books but they don't give enough detail about this particular use of pointers or structures. I have been trying this for a while but I am sure I am missing something.
I just need to understand where I am going wrong and what direction to head towards.
Thanks for any and all help.
Below is a Re-post with more code:
//this should initialize array so that ptr contains all array elements
void initialize ( list array[] ) {
int j;
for(j = 0; j < setSize - 1; j++){
array[j].next = array[j+1].next; //using the & does not seem to work, so I don't use it
array[j].next = NULL;
}
ptr = &(array[0]);
}
Later on I am using this in my own malloc function (which is where the seg fault happens)
//this should allocate a new list cell in array
list* my_malloc () {
list* temp;
temp = ptr;
if(ptr = 0) {printf("Empty!\n"); return;}
else{ //the next line creates the seg fault
ptr = (*ptr).next;
}
return temp;
}
list* cell ( char n, list* next ) {
list* x = my_malloc();
x->value = n;
return x;
x->next = next;
return x;
}
main ( void* argv, int argc ) {
initialize(array);
list* x = nil;
char ch = a;
int i;
for ( i = 0; i < setSize; i++ )
x = cell(ch,x);
for ( i = 0; i < 10; i++ ) {
list* y = x->next;
my_free(x); //this is my own free function
x = y;
};
for ( i = 0; i < 10; i++ )
x = cell(ch,x);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了这一行之外,你所拥有的一切都非常接近有意义:
我想你的意思是
sizeof
那里?无论如何,该操作是毫无意义的(并且是非法的,因为您试图将整数分配给指针类型的变量)。您不需要分配任何内存,您的array
定义已经做到了这一点。您的
for
循环确实有问题 - 您将越过数组末尾,这可能是导致分段错误的原因。要么是这样,要么是您没有初始化j
,因此它可能访问了一些不应该访问的内存。我想你可能想要类似的东西:编辑:你的段错误的原因是这一行:
Sets
ptr
to0
。我想你想要一个==
。另一点 - 你说“使用&
似乎不起作用,所以我不使用它”。这可能意味着您的list
结构声明不正确,或者至少以相当不标准的方式声明。您可能应该展示更多的程序 - 看来您对指针的工作原理有一些非常基本的误解。Everything you have there is pretty close to making sense except for this line:
I presume you mean
sizeof
there? At any rate, that operation is meaningless (and illegal, since you're trying to assign an integer to a variable of pointer type). You don't need to allocate any memory, your definition ofarray
already does that.You do have a problem with your
for
loop - you're going go one past the end of the array, which might be what's causing your segmentation fault. It's either that or the fact that you didn't initializej
, so it could be accessing some memory it shouldn't. I think you probably want something like:Edit: The reason for your segfault is that this line:
Sets
ptr
to0
. I think you want a==
there. Another point - you say "using the&
does not seem to work, so I don't use it". That probably means yourlist
structure is declared incorrectly, or at least in a pretty nonstandard way. You should probably show more of your program - it seems like you have some pretty fundamental misunderstandings of how pointers work.我认为这就是您想要的:
请注意,for 循环是针对 (ARRAY_LENGTH - 1) 项执行的,而不是针对 ARRAY_LENGTH 项执行的,因为 array[j+1] 在最后一次迭代中将超出范围。
现在您已经有了 ptr 指向数组中的第一项,您可以使用如下方式遍历它:
I think this is what you want:
Note that the for-loop is executed for (ARRAY_LENGTH - 1) items, not ARRAY_LENGTH items, since array[j+1] would be out of bounds in the last iteration.
Now that you have ptr pointing to the 1st item in the array, you can traverse through it with something like:
由于 array[j+1].next 未定义,因此这是行不通的。根据 buffer[i].next 的定义方式,您应该能够执行以下操作:
That won't work since array[j+1].next isn't defined. Depending on how
buffer[i].next
is defined, you should be able to do this:sizeof
不分配空间。如果您不想使用 malloc,则必须在堆栈上或存储全局变量的任何位置(可能是 bss)分配空间。由于您可能在数组定义中执行此操作,因此ptr = &buffer[j]
将为您提供指向缓冲区第j
元素的指针。我认为你的意思是“addressof”而不是“sizeof”,在C中拼写为&
。当然,buffer
不是array
。这要么是一个错字,要么是非常错误的事情。不管怎样,你的代码有点乱。您可能想要做的是将指针初始化为数组,然后迭代元素,设置下一个:
sizeof
doesn't allocate space. If you don't want to use malloc, you have to allocate your space either on the stack or wherever globals are stored (probably bss). Since you presumably do that with your array definition,ptr = &buffer[j]
will get you a pointer to thej
th element of the buffer. I think you meant "addressof" instead of "sizeof", which is spelled&
in C. Of course,buffer
isn'tarray
. That's either a typo or something very wrong.Anyway, your code is a bit misordered. What you probably wanted to do was initialize your pointer to
array
, then iterate through the elements, setting next: