从指针数组中的地址索引
Index from address in array of pointers?
下面的代码是满足以下要求的解决方案:
"更改 §27.9 中
Link
和List
的表示形式,而不更改函数提供的用户界面。在Links
数组中分配Link
,并让成员:first
、last
、prev
和next
int
s(数组中的索引(。"
- 练习 6 第 27 章 - 编程:使用 C++ B. Stroustrup 的原则和实践
该接口继承自侵入式双向链表的普通实现。我添加了 bool 数组(和相关函数(来跟踪内存:
#include <iostream>
struct Link
{
int next;
int prev;
};
//------------------------------------------------------------------------------------
struct List
{
Link** head;
int first; // points to the current first node
int last;
bool* available;
int list_size;
int get_index()
{
for (int i = 0; i < list_size; ++i)
{
if (available[i] == true)
{
available[i] = false;
return i;
}
}
throw std::bad_alloc("bla bla!n");
}
List()
{
list_size = 30;
head = new Link*[list_size];
available = new bool[list_size];
first = -1;
last = -1;
for (int i = 0; i < list_size; ++i)
{
available[i] = true;
}
}
void List::push_back(Link* l)
{
if (l == nullptr)
{
throw std::invalid_argument("bla bla!n");
}
int index = get_index();
head[index] = l;
if (last != -1)
{
head[last]->next = index;
head[index]->prev = last;
}
else
{
first = index;
head[index]->prev = -1;
}
last = index;
head[index]->next = -1;
}
void push_front(Link* l)
{
if (l == nullptr)
{
throw std::invalid_argument("bla blan");
}
int index = get_index();
head[index] = l;
if (first != -1)
{
head[first]->prev = index;
head[index]->next = first;
}
else
{
last = index;
head[index]->next = -1;
}
first = index;
head[index]->prev = -1;
}
// index = ptr - base
std::ptrdiff_t index_from_address(Link* l) { return l - head[0]; }
Link* front() const { return head[first]; }
};
//------------------------------------------------------------------------------------
int main()
{
List l;
for (int i = 0; i < 10; ++i)
{
l.push_back(new Link());
}
for (int i = 0; i < 10; ++i)
{
l.push_front(new Link());
}
std::cout <<"first = "<< l.first <<", index = " << l.index_from_address(l.front());
getchar();
}
预期成果:
first = 19, index = 19
实际结果:
first = 19, index = 194
为什么?
l - head[0]
在这里,您可以比较两个指针的值。您允许数组中的所有指针默认初始化,因此它们的值是不确定的,因此访问值的行为是不确定的。
您可能打算index_from_address
查找存储特定指针对象的索引 - 而不是指向的对象,因为指向的对象不在 head
指向的数组中。为此,您必须添加一大堆&
:
Link*& front() const // return a reference to the pointer object, not a copy
// take a reference to the pointer as an argument, add const for good measure
std::ptrdiff_t index_from_address(Link*& l) const
// compare the addresses of the pointers, rather than values
{ return &l - &head[0]; }
相关文章:
- 将数组的地址分配给变量并删除
- 数组索引的值没有增加
- 空基优化子对象的地址
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 查找最接近的大于当前数字的数字的索引
- 如何在c++程序中找到函数的地址
- 在C++中调整向量中的索引
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 被解释为低级别const的const对象的地址
- 重载元组索引运算符-C++
- 将地址分配给本地指针后,公共对象的变量将消失
- 从指针数组中的地址索引
- C++多维数组逗号索引地址
- 如何通过地址获取元组中元素的索引
- 从成员的地址获取成员的索引
- 获取给定地址的std::vector元素的索引
- 获取虚成员函数的真实地址(或vTable中的索引)
- 查找数组中给定索引的第一个和最后一个字节的地址