gpt4 book ai didi

c - C语言中的多态应用问题

转载 作者:行者123 更新时间:2023-12-04 09:32:27 26 4
gpt4 key购买 nike

这是我在 stackowerflow 中的第一个问题。所以我很兴奋:)
我正在尝试在 C 中练习多态性。我已经阅读了很多文章。我用我的理解写了一个小示例代码。我的目标是创建一个形状类。此类包括字段和长度方法。从这个类派生的矩形和圆形类自己计算字段和长度方法。
但是在编译过程中出现了一个问题:
enter image description here
在 OOP_in_C_example_3_polymorphism_3.exe 中的 0x000000000000000A 处抛出异常:0xC0000005:访问冲突执行位置 0x000000000000000A。
当我删除其中一行时,它会变得更好。但这次它根据最后调用的构造函数进行操作:
enter image description here
我在哪里遇到问题?
形状.h:

#include <stdint.h>

struct Vtable;

typedef struct{
struct Vtable* vptr;
uint16_t x;
uint16_t y;

}Shape;

struct Vtable {
void(*area)(Shape* me);
void(*lenght)(Shape* me);
};

inline void area(Shape* me) {
(*me->vptr->area)(me);
}

inline void lenght(Shape* me) {
(*me->vptr->lenght)(me);
}

void shape_ctor(Shape* me, int x, int y);
形状.c:
#include "shape.h"

void shape_ctor(Shape* me,int x, int y) {
struct Vtable vtbl = { &area,&lenght };
me->vptr = &vtbl;
me->x = x;
me->y = y;
}
矩形.h:
#include "shape.h"

typedef struct {
Shape super;
uint16_t en;
uint16_t boy;
}rect;

void rect_ctor(rect* me, uint16_t x, uint16_t y, uint16_t en, uint16_t boy);
矩形:
#include "rect.h"
#include <stdio.h>

void rect_lenght(void* me);
void rect_area(void* me);


void rect_ctor(rect* me,uint16_t x, uint16_t y, uint16_t en, uint16_t boy) {
struct Vtable vtbl = { &rect_area, &rect_lenght };

shape_ctor(&me->super, x, y);
me->super.vptr = &vtbl; //override vptr
me->en = en;
me->boy = boy;
}


void rect_lenght(void* me) {
rect* me_ = (rect*)me; //downcast işlemi
printf("rectangle lenght: %d\n",(me_->en + me_->boy) * 2);
}
void rect_area(void* me) {
rect* me_ = (rect*)me; //downcast işlemi
printf("rectangle area: %d\n", (me_->en)*(me_->boy));
}
圆.h:
#include "shape.h"

typedef struct {
Shape super;
uint16_t radius;
}circle;


void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r);
圆.c:
#include "circle.h"
#include <stdio.h>

void circle_lenght(void* me);
void circle_area(void* me );


void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r) {
struct Vtable vtbl = {&circle_area,&circle_lenght};

shape_ctor(&me->super, x, y);
me->super.vptr = &vtbl;
me->radius = r;
}


void circle_lenght(void* me) {
circle* me_ = (circle*)me;
printf("circle lenght: %f\n", (me_->radius) * (2) * (3.14));
}
void circle_area(void* me){
circle* me_ = (circle*)me;
printf("circle area: %f\n", (me_->radius) * (me_->radius) * (3.14));
}
主文件:
#include <stdio.h>
#include "rect.h"
#include "circle.h"


int main() {
rect k1;
circle d1;

rect_ctor(&k1, 10, 10, 15, 20);
circle_ctor(&d1, 10, 10, 5);


k1.super.vptr->lenght(&k1);
d1.super.vptr->lenght(&d1);
}

最佳答案

But there is a problem during compilation: enter image description here


这是在执行期间,而不是在编译期间
你有未定义的行为,因为你在离开函数后使用了局部变量的地址,你需要替换
void shape_ctor(Shape* me,int x, int y) {   
struct Vtable vtbl = { &area,&lenght };
经过
void shape_ctor(Shape* me,int x, int y) {   
static struct Vtable vtbl = { &area,&lenght };
void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r) {
struct Vtable vtbl = {&circle_area,&circle_lenght};
经过
void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r) {
static struct Vtable vtbl = {&circle_area,&circle_lenght};
我不得不将面积和长度的定义从 shape.h 移动到 shape.c,并为它们删除了无用的内联。
我还必须保护标题免受多重包含,例如:
#ifndef SHAPE_H
#define SHAPE_H

#include <stdint.h>

struct Vtable;

typedef struct{
struct Vtable* vptr;
uint16_t x;
uint16_t y;
}Shape;

struct Vtable {
void(*area)(Shape* me);
void(*lenght)(Shape* me);
};

void shape_ctor(Shape* me, int x, int y);

#endif
之后,编译和执行:
/tmp % gcc -Wall -g *.c
circle.c: In function 'circle_ctor':
circle.c:9:19: warning: initialization from incompatible pointer type [enabled by default]
static struct Vtable vtbl = {&circle_area,&circle_lenght};
^
circle.c:9:19: warning: (near initialization for 'vtbl.area') [enabled by default]
circle.c:9:19: warning: initialization from incompatible pointer type [enabled by default]
circle.c:9:19: warning: (near initialization for 'vtbl.lenght') [enabled by default]
main.c: In function 'main':
main.c:14:5: warning: passing argument 1 of 'k1.super.vptr->lenght' from incompatible pointer type [enabled by default]
k1.super.vptr->lenght(&k1);
^
main.c:14:5: note: expected 'struct Shape *' but argument is of type 'struct rect *'
main.c:15:5: warning: passing argument 1 of 'd1.super.vptr->lenght' from incompatible pointer type [enabled by default]
d1.super.vptr->lenght(&d1);
^
main.c:15:5: note: expected 'struct Shape *' but argument is of type 'struct circle *'
rect.c: In function 'rect_ctor':
rect.c:9:19: warning: initialization from incompatible pointer type [enabled by default]
static struct Vtable vtbl = { &rect_area, &rect_lenght };
^
rect.c:9:19: warning: (near initialization for 'vtbl.area') [enabled by default]
rect.c:9:19: warning: initialization from incompatible pointer type [enabled by default]
rect.c:9:19: warning: (near initialization for 'vtbl.lenght') [enabled by default]
/tmp % ./a.out
rectangle lenght: 70
circle lenght: 31.400000
/tmp %
修改 main 以分配堆中的元素,以允许 valgrind 知道它们的大小以检测可能的访问:
#include <stdlib.h>

#include "rect.h"
#include "circle.h"

int main() {
rect * k1 = malloc(sizeof(*k1));
circle * d1 = malloc(sizeof(*d1));

rect_ctor(k1, 10, 10, 15, 20);
circle_ctor(d1, 10, 10, 5);


k1->super.vptr->lenght(k1);
d1->super.vptr->lenght(d1);

free(k1);
free(d1);

return 0;
}
在 valgrind 下执行:
/tmp % valgrind ./a.out
==53229== Memcheck, a memory error detector
==53229== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==53229== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==53229== Command: ./a.out
==53229==
rectangle lenght: 70
circle lenght: 31.400000
==53229==
==53229== HEAP SUMMARY:
==53229== in use at exit: 0 bytes in 0 blocks
==53229== total heap usage: 2 allocs, 2 frees, 48 bytes allocated
==53229==
==53229== All heap blocks were freed -- no leaks are possible
==53229==
==53229== For counts of detected and suppressed errors, rerun with: -v
==53229== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
/tmp %

为了在编译时没有所有这些警告,我鼓励您添加所需的强制转换。

注意定义 vtbl 也更好,你不想让它们被错误破坏,所以
void shape_ctor(Shape* me,int x, int y) {   
static const struct Vtable vtbl = { &area,&lenght };

void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r) {
static const struct Vtable vtbl = {&circle_area,&circle_lenght};
并在 shape.h 中:
typedef struct{
const struct Vtable* vptr;
您可以声明的成员/变量/参数越多 const在编译时检测错误就越好。

关于c - C语言中的多态应用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62775040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com