出于教育目的,我正在尝试重新创建ANSI-C中的封装原理。我本质上所做的就是在 .c 文件中建立一些结构:
struct _private
{
unsigned char SizeInBytes;
unsigned char* matrix;
struct Stack* S;
unsigned char ByteX;
};
代表我想看不见的变量。然后在struct(类)内部的 .h 文件中,创建了一个不透明的指针:
struct Maze
{
void* _private;
};
我稍后在构造函数中分配如下:
void* Maze_ctor(void* self, va_list *ap)
{
struct Maze* this = self;
this->DimX = va_arg(*ap, unsigned char);
this->DimY = va_arg(*ap, unsigned char);
this->_private = &(struct _private) // passing address of struct to void*
{
.SizeInBytes = this->DimX*this->DimY >> 1,
.S = new(Stack),
.ByteX = this->DimX % 8 > 0 ? this->DimX / 8 + 1 : this->DimX / 8
};
//
private.matrix = (unsigned char*)malloc(private.ByteX*this->DimY);
S = new(Stack); // this in my new() and it works similar to C++ new
for (int i = 0; i < private.ByteX*this->DimY; i++)
*(private.matrix + i) = 0;
}
这时一切正常,但随后我试图调用Next()方法:
int Next(void* self, ...)
{
struct Maze* this = self;
struct _private *r = this->_private;
short t;
toBinary(this); // after this point the struct private breaks
}
toBinary()的原型是:
void toBinary(const void* self)
{
// somehow char local is defined and equals to 204??
struct Maze *this = self;
struct _private *r = this->_private;
unsigned char local; // right after this point SizeInBytes equals to 204!
...
}
问题是:如何解决此问题。 禁止使用C ++! 对于感兴趣的人:这是new()
void* new(const void* _class,...)
{
const struct Class* class = _class; // we need to convert pointer from void* to class* safely
void *p = calloc(1, class->size); // allocation of memory for class .using size param
assert(p); // if Null -> throw an error
*(const struct Class**)p = class; // safe assignment of class pointer to (value) of p, to have memory and built in funcs
if (class->ctor) // if has constructor with some dynal in it, execute with varargs on its input
{
va_list ap;
va_start(ap, _class); //
p = class->ctor(p, &ap); // pass arguments as a list of pointers.
va_end(ap);
}
return p; //returns a pointer to class pointer (weird but worx)
}
答案 0 :(得分:2)
如注释中所指出,问题在于您创建了一个本地对象并将其分配给指针this
。在该函数之外,this
的值无效。
您编码,
void* Maze_ctor(void* self, va_list *ap)
{
//....
// this creates a temporary object and will be destroyed after Maz_ctor returns.
this->_private = &(struct _private) // passing address of struct to void*
{
.SizeInBytes = this->DimX*this->DimY >> 1,
.S = new(Stack),
.ByteX = this->DimX % 8 > 0 ? this->DimX / 8 + 1 : this->DimX / 8
};
// ---
}
答案 1 :(得分:0)
感谢@Sami Kuhmonen指出动态分配,并感谢@CS Pei进行错误分析。我为解决此问题所做的事情是:
struct Maze
{
char _private[32]; // allocate the memory size of struct(32)
}
// assign values to void ptr
private.SizeInBytes = this->DimX*this->DimY >> 1;
private.S = new(Stack);
private.ByteX = this->DimX % 8 > 0 ? this->DimX / 8 + 1 : this->DimX / 8;
private.matrix = (unsigned char*)malloc(private.ByteX*this->DimY);
现在可以按预期运行,但速度稍慢