柔性数组

#include <iostream>
#include <memory>

using uint8 = unsigned char;

struct ListNode {
ListNode* pNextNode;
uint8 u8Test[1];
};

class ListTest {
public:
ListTest() :pHead_(nullptr) {}
~ListTest() { delete pHead_;}
void addNodeFormHead(int);

ListNode* getHead()
{
return pHead_;
}
ListNode* pHead_;
};

void ListTest::addNodeFormHead(int len)
{
ListNode* pNode = (ListNode*)malloc(sizeof(ListNode*) + len);
if (pNode == nullptr)
{
return;
}
memset(pNode, sizeof(ListNode*), 0);

if (pHead_ == nullptr)
{
pHead_ = pNode;
}
else
{
pNode->pNextNode = pHead_;
pHead_ = pNode;
}
}


int main()
{
std::shared_ptr<ListTest> p(new ListTest());
p->addNodeFormHead(10);
ListNode* pNode = p->getHead();
//std::cout << sizeof(ListNode) << std::endl;
uint8* pu8 = (uint8*)pNode + sizeof(ListNode*);
for (int i = 0; i < 10; ++i)
{
pu8[i] = '1';
}

for (int i = 0; i < 10; ++i)
{
std::cout << pu8[i] << std::endl;
}
return 0;
}

posted @ 2017-05-26 14:39  c.p  阅读(103)  评论(0)    收藏  举报