C/C++高手请进,关于解析动态内存的问题
ufool 2007-01-09 04:36:18 比如有一个结构
strcut A
{
short count; // 表示name中元素的个数
char name[6];
short other;
};
有一段内存p对应于A。比如:
06 00 41 42 43 44 45 00 01 00
可以这样写:
A* pA = (A*)p;
则此时
pA->count 为 6
pA->name 为 "ABCDE"
pA->other 为 1
但这种情况在实际中很少出现,更多的情况形如:
strcut B
{
short count; // 表示name中元素的个数
char name[count];
short other;
};
此时name中元素的个数,是动态的,依赖于前面的count的值。
这种定义在C/C++中是不被允许的。
我的问题是:
有没有一种通用的方法,定义一个类似于A的结构体或类。在不复制内存的情况下,达到与pA的相同或类似使用效果。