在函数中调用时,如何修复匿名创建的结构的奇怪行为?

问题描述 投票:1回答:2

我正在尝试在ANSI-C中重新创建用于教育目的的封装原则。我基本上做的是在.c文件中制作一些结构:

struct _private
{
    unsigned char SizeInBytes;
    unsigned char* matrix;
    struct Stack* S;
    unsigned char ByteX;
};

这代表了我想看不见的变量。然后在struct(class)里面的.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 ++!对于感兴趣的人:这里是新的()

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)
}
c class private encapsulation
2个回答
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
};
// ---
}

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);

这现在按预期工作,但有点慢

© www.soinside.com 2019 - 2024. All rights reserved.