简体   繁体   English

在C ++中将指针存储和检索到数组中

[英]storing and retrieving a pointer in an array in c++

I have a large char array which is functioning as a memory pool and want to store a pointer in the first position of the array which points to whatever the next open position in the pool is, so every time something is allocated to the pool the pointer would point to the byte that follows the ones which were just allocated. 我有一个用作存储池的大型char数组,想在该数组的第一个位置存储一个指针,该指针指向池中下一个打开的位置,因此每次向该池分配一些指针时将指向刚分配的字节之后的字节。 My only problem is I am not quite sure how to store the pointer in the array and then be able to modify it in other functions since the only place the pointer will exist is in the array[0] position. 我唯一的问题是我不太确定如何将指针存储在数组中,然后能否在其他函数中对其进行修改,因为指针将存在的唯一位置是在array [0]位置。 Can anyone point me in the right direction on this? 有人可以为此指出正确的方向吗?

the char array is declared like: char数组的声明如下:

char pool[size];

What you really want is an index into that array that tells you where to insert. 您真正想要的是该数组的索引,告诉您要插入的位置。

You could declare a structure: 您可以声明一个结构:

struct pool
{
    char poolData[size];
    int insertIndex;
};

So that you always have the pool memory and index where you want to insert to together. 这样一来,您便始终拥有要插入其中的池内存和索引。 Or, just have a separate variable and pass them together to whoever needs to use it. 或者,只需有一个单独的变量,然后将它们一起传递给需要使用它的任何人。

char pool[size];
int insertIndex;

There's no need to "hijack" the first element of the array and use it differently than the rest of the array; 无需“劫持”数组的第一个元素,并以不同于数组其余部分的方式使用它; just declare another variable to track the state of the pool. 只需声明另一个变量以跟踪池的状态即可。

I think what you need is an index to remember where is the next empty spot on the pool (initially will be zero). 我认为您需要的是一个索引,以记住池中的下一个空白点在哪里(最初为零)。

char pool[size];
int index = 0;

Then everytime you insert a new element, you just increment it: 然后,每次插入新元素时,只需增加它:

if(index < size) {
  pool[index] = 123;
  index++;
}

If you can't follow the recommendations of the other answers because you absolutely must use the pool to store all information in it, the safest way to store the integer information in the char-array is to use memcpy (I am using C++11 syntax): 如果由于绝对必须使用该池在其中存储所有信息而无法遵循其他答案的建议,则在char数组中存储整数信息的最安全方法是使用memcpy (我正在使用C ++ 11种语法):

#include <cstring>
#include <iostream>

int main()
{
  using size_type = std::size_t;
  static constexpr size_type size = 1000;
  char pool[size];

  /* Store 12 as integer information at the beginning
     of the pool: */    
  size_type next_free = 12;
  std::memcpy(pool,&next_free,sizeof(size_type));

  /* Retrieve it: */
  size_type retrieved = 0;
  std::memcpy(&retrieved,pool,sizeof(size_type));

  /* This will output 12: */    
  std::cout << retrieved << std::endl;

  return 0;
}

Of course this implies that the first sizeof(size_type) entries of the pool must not be used to store any actual characters. 当然,这意味着不得将池的第一个sizeof(size_type)条目用于存储任何实际字符。 The lowest entry you can actually use is pool[sizeof(size_type)] . 您实际可以使用的最低条目是pool[sizeof(size_type)]

char **pPoolEnd = (char **) pool;

to initialize the pointer you want. 初始化所需的指针。

*pPoolEnd = pool + sizeof(char **);

to make it point to its own end (eg when there's nothing else in the pool). 使其指向自己的终点(例如,当泳池中没有其他东西时)。

However, why would you want to do this? 但是,为什么要这样做呢? It's confusing, error prone and probably unnecessary. 这很混乱,容易出错,并且可能是不必要的。 Others have pointed to much better alternatives. 其他人指出了更好的选择。 Assuming I had such a pool in the first place, I would probably go with one of those, or simply use a separate pointer, char *poolEnd along with pool . 假设我首先有一个这样的池,我可能会选择其中一个,或者简单地使用单独的指针char *poolEndpool

Also, it's bad style to expose your implementation details to users ("pool end pointer is at pool[0]") and even worse to expect them to deal with them ("please update pool[0] whenever you'd like to allocate from the pool"). 另外,将实现细节向用户公开是不好的风格(“池结束指针位于pool [0]”),更糟糕的是期望他们与他们打交道(“只要您想分配,请更新pool [0]”)从游泳池中”)。 Think malloc and free ; 想想mallocfree ; expose simple function interfaces to your users. 向用户公开简单的功能界面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM