我有point.h
个和polygon.h
个文件,以及与它们相关的.c
个文件.在point.h
年内
// point.h
#ifndef POINT_H
#define POINT_H
typedef struct Point point;
point *point_alloc(void);
void *point_free(point *);
void *point_init(point *, float, float);
void *point_print(const point *);
float point_get_x(const point *);
float point_get_y(const point *);
void *point_set_x(point *, float);
void *point_set_y(point *, float);
#endif
在point.c
年里,我有
// point.c
#include <stdlib.h>
#include <stdio.h>
#include "point.h"
#define GET_VALUE_ERROR (2L)
struct Point
{
float x;
float y;
};
point *point_alloc(void)
{
point *pt = malloc(sizeof *pt);
if(NULL == pt)
{
fprintf(stderr, "Could not allocate point.\n");
return NULL;
}
else
{
return pt;
}
}
void *point_free(point *pt)
{
if(NULL == pt)
{
fprintf(stderr, "Could not free point.\n");
return NULL;
}
else
{
free(pt);
pt = NULL;
return NULL;
}
}
void *point_init(point *pt, float x, float y)
{
if(NULL == pt)
{
fprintf(stderr, "Cannot initiate point.\n");
return NULL;
}
else
{
pt -> x = x;
pt -> y = y;
return NULL;
}
}
void *point_print(const point *pt)
{
if(NULL == pt)
{
fprintf(stderr, "Cannot print point.\n");
return NULL;
}
else
{
printf("Point at (%f, %f)\n", pt -> x, pt -> y);
return NULL;
}
}
float point_get_x(const point *pt)
{
if(NULL == pt)
{
fprintf(stderr, "Cannot get point.\n");
return GET_VALUE_ERROR;
}
else
{
return pt -> x;
}
}
float point_get_y(const point *pt)
{
if(NULL == pt)
{
fprintf(stderr, "Cannot get point.\n");
return GET_VALUE_ERROR;
}
else
{
return pt -> y;
}
}
void *point_set_x(point *pt, float x)
{
if(NULL == pt)
{
fprintf(stderr, "Cannot get point.\n");
return NULL;
}
else
{
pt -> x = x;
return NULL;
}
}
void *point_set_y(point *pt, float y)
{
if(NULL == pt)
{
fprintf(stderr, "Cannot get point.\n");
return NULL;
}
else
{
pt -> y = y;
return NULL;
}
}
而在polygon.h
岁的时候我有
// polygon.h
#ifndef POLYGON_H
#define POLYGON_H
typedef struct Polygon polygon;
polygon *polygon_alloc(unsigned);
void *polygon_free(polygon *);
void *polygon_init(polygon *, unsigned, float, point *);
void *polygon_print(const polygon *);
unsigned polygon_get_nside(const polygon *);
void *polygon_set_nside(polygon *, unsigned);
point *polygon_get_centre(const polygon *);
void *polygon_set_centre(polygon *, point *);
point *polygon_get_vertex(const polygon *, unsigned);
void *polygon_set_vertex(polygon *, unsigned, float, float);
#endif
在polygon.c
年里,我有
// polygon.c
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "point.h"
#include "polygon.h"
#ifndef M_PI
#define M_PI (3.14159265358979323846264338327950288)
#endif
#define GET_NSIDE_ERROR (2U)
struct Polygon
{
unsigned nside;
point *centre;
point **vertices;
};
polygon *polygon_alloc(unsigned nside)
{
if(nside == 0)
{
fprintf(stderr, "Cannot have a 0-sided polygon.\n");
return NULL;
}
else
{
polygon *poly = malloc(sizeof(*poly));
if(NULL == poly)
{
fprintf(stderr, "Cannot allocate polygon.\n");
return NULL;
}
else
{
poly -> nside = nside;
poly -> centre = point_alloc();
if(NULL == poly -> centre)
{
fprintf(stderr, "Cannot allocate polygon centre.\n");
free(poly);
poly = NULL;
return NULL;
}
else
{
poly -> vertices = malloc(nside * sizeof(point*));
if(NULL == poly -> vertices)
{
fprintf(stderr, "Cannot allocate polygon vertices.\n");
free(poly -> centre);
poly -> centre = NULL;
free(poly);
poly = NULL;
return NULL;
}
else
{
for(unsigned i = 0; i < nside; i++)
{
poly -> vertices[i] = point_alloc();
if(NULL == poly -> vertices[i])
{
fprintf(stderr, "Cannot allocate node %u.\n", i);
for(unsigned j = 0; j < i; j++)
{
free(poly -> vertices[j]);
poly -> vertices[j] = NULL;
}
free(poly -> centre);
poly -> centre = NULL;
free(poly -> vertices);
poly -> vertices = NULL;
free(poly);
poly = NULL;
}
}
}
}
}
return poly;
}
}
void *polygon_free(polygon *poly)
{
if(NULL == poly)
{
fprintf(stderr, "Cannot free polygon.\n");
return NULL;
}
else
{
if(NULL == poly -> centre)
{
fprintf(stderr, "Cannot free polygon centre.\n");
return NULL;
}
else
{
free(poly -> centre);
poly -> centre = NULL;
if(NULL == poly -> vertices)
{
fprintf(stderr, "Cannot free polygon vertices.\n");
return NULL;
}
else
{
for(unsigned i = 0; i < poly -> nside; i++)
{
if(NULL == poly -> vertices[i])
{
fprintf(stderr, "Cannot free vertex %u.\n", i);
return NULL;
}
else
{
free(poly -> vertices[i]);
poly -> vertices[i] = NULL;
}
}
free(poly -> vertices);
poly -> vertices = NULL;
}
}
free(poly);
poly = NULL;
}
return NULL;
}
void *polygon_init(polygon *poly, unsigned nside, float radius, point *centre)
{
if(NULL == poly)
{
fprintf(stderr, "Polygon not existing.\n");
return NULL;
}
else if(NULL == centre)
{
fprintf(stderr, "Polygon centre not existing.\n");
return NULL;
}
else
{
polygon_set_nside(poly, nside);
polygon_set_centre(poly, centre);
for(unsigned i = 0; i < poly -> nside; i++)
{
float angle = (1 + 2 * i) * (M_PI / poly -> nside);
float x = radius * cos(angle);
float y = radius * sin(angle);
polygon_set_vertex(poly, i, x, y);
}
return NULL;
}
}
void *polygon_set_nside(polygon *poly, unsigned nside)
{
if(NULL == poly)
{
fprintf(stderr, "Cannot set polygon number of sides.\n");
return NULL;
}
else
{
poly -> nside = nside;
return NULL;
}
}
void *polygon_set_centre(polygon *poly, point *centre)
{
if(NULL == poly)
{
fprintf(stderr, "Cannot set polygon number of sides.\n");
return NULL;
}
else
{
poly -> centre = centre;
return NULL;
}
}
// Rest of implementation
显然,多边形的分配和/或释放是不正确的.为了看到这一点,我写下了这main.c
个读数
#include <stdio.h>
#include <stdlib.h>
#include "point.h"
#include "polygon.h"
int main() {
unsigned nside = 3;
float radius = 1.0;
point* centre = point_alloc();
point_init(centre, 0.0, 0.0);
polygon* poly = polygon_alloc(nside);
polygon_init(poly, nside, radius, centre); // this one is a problem
polygon_print(poly);
polygon_free(poly);
point_free(centre);
exit(EXIT_SUCCESS);
}
compiled using the command gcc -Wall -Wextra -Wpedantic -Werror -std=c99 .\point.c .\polygon.c .\main.c -o .\main.exe
, I have a a "red cross in my IDE (I use VSCode
), as illustrated here in the picture (which indicates that something is wrong) and I do not know neither from where it comes, nor how to solve it?
100
多亏了下面的答案,我更正了分配/释放函数,并能够发现问题是由polygon_init
函数引起的,但我不能准确地指出哪里出了问题?