在不使用 malloc 的情况下将固定大小的对象分配给指针(指向结构)

发布于 2024-10-31 14:52:20 字数 1782 浏览 3 评论 0原文

我有一个指向 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 技术交流群。

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

发布评论

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

评论(4

神也荒唐 2024-11-07 14:52:20

除了这一行之外,你所拥有的一切都非常接近有意义:

ptr = sizeOf(buffer[j]);

我想你的意思是 sizeof 那里?无论如何,该操作是毫无意义的(并且是非法的,因为您试图将整数分配给指针类型的变量)。您不需要分配任何内存,您的 array 定义已经做到了这一点。

您的 for 循环确实有问题 - 您将越过数组末尾,这可能是导致分段错误的原因。要么是这样,要么是您没有初始化 j,因此它可能访问了一些不应该访问的内存。我想你可能想要类似的东西:

for (j = 0; j < setSize - 1; j++) 
{
    array[j].next = &array[j+1];
}

编辑:你的段错误的原因是这一行:

if(ptr = 0) {printf("Empty!\n");  return;}

Sets ptr to 0。我想你想要一个 == 。另一点 - 你说“使用 & 似乎不起作用,所以我不使用它”。这可能意味着您的 list 结构声明不正确,或者至少以相当不标准的方式声明。您可能应该展示更多的程序 - 看来您对指针的工作原理有一些非常基本的误解。

Everything you have there is pretty close to making sense except for this line:

ptr = sizeOf(buffer[j]);

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 of array 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 initialize j, so it could be accessing some memory it shouldn't. I think you probably want something like:

for (j = 0; j < setSize - 1; j++) 
{
    array[j].next = &array[j+1];
}

Edit: The reason for your segfault is that this line:

if(ptr = 0) {printf("Empty!\n");  return;}

Sets ptr to 0. 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 your list 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.

你爱我像她 2024-11-07 14:52:20

我认为这就是您想要的:

#define ARRAY_LENGTH (20)

struct list
{
    char ch;
    struct list* next;
};

struct list array[ARRAY_LENGTH];   // array's memory is allocated with this, no need to use malloc
struct list* ptr = &(array[0]);    // ptr now points to the 1st item in the array
int j;                             // I'm declaring 'j' outside of the for loop just in case you are using c and not c++

for(j=0 ; j < ARRAY_LENGTH-1 ; j++)
    array[j].next = &(array[j+1]); // .next of jth item in array now points to the following item

array[j].next = NULL;              // assign .next of last item to NULL, so you know when you have reached the end of the array when traversing it.

请注意,for 循环是针对 (ARRAY_LENGTH - 1) 项执行的,而不是针对 ARRAY_LENGTH 项执行的,因为 array[j+1] 在最后一次迭代中将超出范围。

现在您已经有了 ptr 指向数组中的第一项,您可以使用如下方式遍历它:

struct list* curItem;  // this will be your iterator to traverse the array
curItem = ptr;         // initialize your iterator to point to the 1st item
while(curItem != NULL) // note that with this, you don't need to know the length of the array
{
    printf("Current item's .ch is %c\r\n", curItem->ch); // don't forget to include the header for printf
    curItem = curItem->next;
}

I think this is what you want:

#define ARRAY_LENGTH (20)

struct list
{
    char ch;
    struct list* next;
};

struct list array[ARRAY_LENGTH];   // array's memory is allocated with this, no need to use malloc
struct list* ptr = &(array[0]);    // ptr now points to the 1st item in the array
int j;                             // I'm declaring 'j' outside of the for loop just in case you are using c and not c++

for(j=0 ; j < ARRAY_LENGTH-1 ; j++)
    array[j].next = &(array[j+1]); // .next of jth item in array now points to the following item

array[j].next = NULL;              // assign .next of last item to NULL, so you know when you have reached the end of the array when traversing it.

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:

struct list* curItem;  // this will be your iterator to traverse the array
curItem = ptr;         // initialize your iterator to point to the 1st item
while(curItem != NULL) // note that with this, you don't need to know the length of the array
{
    printf("Current item's .ch is %c\r\n", curItem->ch); // don't forget to include the header for printf
    curItem = curItem->next;
}
羁绊已千年 2024-11-07 14:52:20
array[j].next = array[j+1].next;

由于 array[j+1].next 未定义,因此这是行不通的。根据 buffer[i].next 的定义方式,您应该能够执行以下操作:

array[j].next = &array[j+1];
array[j].next = array[j+1].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:

array[j].next = &array[j+1];
鸩远一方 2024-11-07 14:52:20

sizeof 不分配空间。如果您不想使用 malloc,则必须在堆栈上或存储全局变量的任何位置(可能是 bss)分配空间。由于您可能在数组定义中执行此操作,因此 ptr = &buffer[j] 将为您提供指向缓冲区第 j 元素的指针。我认为你的意思是“addressof”而不是“sizeof”,在C中拼写为&。当然,buffer不是array 。这要么是一个错字,要么是非常错误的事情。

不管怎样,你的代码有点乱。您可能想要做的是将指针初始化为数组,然后迭代元素,设置下一个:

ptr = array;
for (j = 0; j < setSize; j++)
    array[j].next = &array[j+1];
array[setSize - 1] = NULL; // The last element was set to a lie.

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 the jth element of the buffer. I think you meant "addressof" instead of "sizeof", which is spelled & in C. Of course, buffer isn't array. 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:

ptr = array;
for (j = 0; j < setSize; j++)
    array[j].next = &array[j+1];
array[setSize - 1] = NULL; // The last element was set to a lie.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文