比较两个地址,列表C

问题描述

如果我有一个很长的char数组[100],其中存储了一个结构列表,并且如果我想最后添加一个结构,该如何检查它是否超出边界?

例如,

static char arr[100];
typedef NODE* node_ptr;

typedef struct node
{
   char a;
   char b;
   int size;
   node_ptr next;
}NODE;

//arr already contains few node in it.

//size: the new node size,I want to add in the end
node_ptr add_node(node_ptr last,size_t size)
{
    node_ptr new;
    if(last+2*sizeof(NODE)+size<arr+100)
         //add new node
       return new;
}

enter image description here

如何检查新节点是否超出数组边界?

解决方法

这是COMMENT

不要投票-可以随意使用DV。

您问的是一个难题,很难回答。您还需要考虑一些更复杂的情况,例如

enter image description here

enter image description here

enter image description here

小型阵列将在很短的时间内变成碎片。这是uC开发人员尝试避免这种内存分配的原因之一。嵌入式编程中还有其他类似池的技术。