这个指向指针的指针为什么不能分配内存?
时间:2010-08-09
来源:互联网
#define POLYGONMAX 100
struct Edge {
int vmax;
float deltau;
float u;
float n;
struct Edge *next;
};
struct Edge **polygon_ET;
......
polygon_ET = (struct Edge **)malloc(POLYGONMAX * sizeof(struct Edge *));
if (polygon_ET != NULL){
*(polygon_ET + 0) = (struct Edge *)malloc(YMAX * sizeof(struct Edge));
*(polygon_ET + 1) = (struct Edge *)malloc(YMAX * sizeof(struct Edge));
*(polygon_ET + 2) = (struct Edge *)malloc(YMAX * sizeof(struct Edge));
for (i = 0; i < 3; i++){
/* *(polygon_ET + i) = (struct Edge *)malloc(YMAX * sizeof(struct Edge));*/
if (*(polygon_ET + i) != NULL)
printf ("\nIn main: polygon_ET[%d] is not NULL", i);
else if (*(polygon_ET + i) == NULL)
printf ("\nIn main: polygon_ET[%d] is NULL", i);
getch();
}
}
程序执行后,只有polygon_ET + 0不为空,而其它polygon_ET+i均为空指针?为什么?
作者: liklstar 发布时间: 2010-08-09
作者: zhangsuozhu 发布时间: 2010-08-09
In main: polygon_ET[1] is not NULL
In main: polygon_ET[2] is not NULL
正常,楼主幻觉了
作者: north423 发布时间: 2010-08-09
In main: polygon_ET[1] is not NULL
In main: polygon_ET[2] is no ...
north423 发表于 2010-08-09 18:13
我就奇怪了,在我这里就是为空。我用另外一个类似的程序试验是成功的,就这个不行。
作者: liklstar 发布时间: 2010-08-09
作者: north423 发布时间: 2010-08-09
楼主!危险!
用这个吧 polygon_ET = (struct Edge **)calloc(sizeof(struct Edge *), POLYGONMAX );
作者: zhangsuozhu 发布时间: 2010-08-09
楼主!危 ...
zhangsuozhu 发表于 2010-08-09 18:22
试过了,还是不行!
作者: liklstar 发布时间: 2010-08-09
作者: liwangli1983 发布时间: 2010-08-09
north423 发表于 2010-08-09 18:20
#include <stdio.h>
#include <stdlib.h>
#define VERTEXMAX 50
#define YMAX 480
#define POLYGONMAX 100
struct point {
float u;
int v;
float n;
};
struct point vertexarray[POLYGONMAX][VERTEXMAX];
struct polygon{
float a;
float b;
float c;
float d;
int color;
int vmax;
int pi;
struct polygon *next;
};
struct polygon *PT[YMAX];
struct polygon *APL = NULL;
struct Edge {
int vmax;
float deltau;
float u;
float n;
struct Edge *next;
};
struct Edge **polygon_ET;
struct EdgePad{
float ul;
float deltaul;
int vlmax;
float ur;
float deltaur;
int vrmax;
float nl;
float deltanu; /* -a/c 当u变化时n的增量*/
float deltanv; /* -b/c 当v变化时n的增量*/
int PI;
struct EdgePad *next;
} *AEPL = NULL;
/*this founction calculate the "a", "b", "c", "d"four values of one plate which the polygon reside on*/
calcabcd (struct point *p1, struct point *p2, struct point *p3, float *a, float *b, float *c, float *d)
{
float v1a, v1b, v1c;
float v2a, v2b, v2c;
v1a = p2->u - p1->u;
v1b = p2->v - p1->v;
v1c = p2->n - p1->n;
v2a = p3->u - p1->u;
v2b = p3->v - p1->v;
v2c = p3->n - p1->n;
*a = v1b * v2c - v1c * v2b;
*b = v1c * v2a - v1a * v2c;
*c = v1a * v2b - v1b * v2a;
*d = -1 * *a * p1->u + -1 * *b * p1->v + -1 * *c * p1->n;
}
/*this function calculate the max v value of one polygon*/
int calcvmax (struct point va[POLYGONMAX][VERTEXMAX], int row) /*ptarr is a two dimention array, here is ptarr[] that is the ith row of the array*/
{
int vmax = 0, i = 0;
while (va[row].n != -100000.0) {
if (vmax < va[row].v)
vmax = va[row].v;
i++;
}
return (vmax);
}
int calcvmin(struct point va[POLYGONMAX][VERTEXMAX], int row)
{
int vmin = 0, i = 0;
while (va[row].n != -100000.0) {
if (vmin > va[row].v)
vmin = va[row].v;
i++;
}
return (vmin);
}
int calcplgnnum(struct point va[POLYGONMAX][VERTEXMAX])
{
int i = 0;
while (va[0].n != -100000.0)
i++;
return (i);
}
struct polygon * insertpolygon (struct polygon *plgn, struct polygon *activepoly)
{
struct polygon *pt;
pt = activepoly;
while (pt->next != NULL)
pt = pt->next;
pt->next = plgn;
plgn = NULL;
return (activepoly);
}
buildPT(struct point va[POLYGONMAX][VERTEXMAX], struct polygon *PT[])
{
int i = 0, plgnnum = 0, vmin;
struct polygon *plgn = NULL, *p;
plgnnum = calcplgnnum(va);
for (i = 0; i < plgnnum; i++) {
plgn = (struct polygon *)malloc(sizeof(struct polygon));
calcabcd (&va[0], &va[1], &va[2], &(plgn->a), &(plgn->b), &(plgn->c), &(plgn->d));
plgn->vmax = calcvmax (va, i);
plgn->color = (i + 2) % 16;
if (plgn->color == 2)
plgn->color = 3;
plgn->pi = i;
vmin = calcvmin (va, i);
p = PT[vmin];
PT[vmin] = plgn;
plgn->next = p;
}
}
calcedgenum(int plgn_id)
{
int i = 0;
while (vertexarray[plgn_id].n != -1)
i++;
return (i);
}
buildET (struct point vertexarray[POLYGONMAX][VERTEXMAX], int plgn_id, struct Edge **polygon_ET)
{
int edge_num = 0, i = 0, m, l;
struct Edge *e0, *e;
edge_num = calcedgenum(plgn_id);
for (i = 0 ; i < YMAX; i++)
(*(polygon_ET + plgn_id) + i)->next = NULL;
for (i = 0; i < edge_num; i++) {
m = (i + 1) % edge_num;
if (vertexarray[plgn_id].v > vertexarray[plgn_id][m].v)
l = vertexarray[plgn_id][m].v;
else
l = vertexarray[plgn_id].v;
if ((*(polygon_ET + plgn_id) + l)->n == -100000.0)
e = *(polygon_ET + plgn_id) + l;
else
e = (struct Edge *)malloc(sizeof(struct Edge));
if (vertexarray[plgn_id].v > vertexarray[plgn_id][m].v) {
e->next = NULL;
e->vmax = vertexarray[plgn_id].v;
e->u = vertexarray[plgn_id][m].u;
e->n = vertexarray[plgn_id][m].n;
} else if (vertexarray[plgn_id].v < vertexarray[plgn_id][m].v) {
e->next = NULL;
e->vmax = vertexarray[plgn_id][m].v;
e->u = vertexarray[plgn_id].u;
e->n = vertexarray[plgn_id].n;
}
e->deltau = (vertexarray[plgn_id][m].u - vertexarray[plgn_id].u) / (vertexarray[plgn_id][m].v - vertexarray[plgn_id].v);
printf ("\nIn buildET: e->vmax=%d, e->u=%f, e->n=%f, e->deltau", e->vmax, e->u, e->n, e->deltau);
getch();
if ((*(polygon_ET + plgn_id) + l)->next != NULL) {
e0 = (*(polygon_ET + plgn_id) + l)->next;
(*(polygon_ET + plgn_id) + l)->next = e;
e->next = e0;
}
}
}
setva (struct point va[POLYGONMAX][VERTEXMAX])
{
va[0][0].u = 10;
va[0][0].v = 0;
va[0][0].n = 0;
va[0][1].u = 0;
va[0][1].v = 10;
va[0][1].n = 0;
va[0][2].u = 0;
va[0][2].v = 0;
va[0][2].n = 10;
va[1][0].u = 30;
va[1][0].v = 0;
va[1][0].n = 0;
va[1][1].u = 30;
va[1][1].v = 30;
va[1][1].n =0;
va[1][2].u = 0;
va[1][2].v = 30;
va[1][2].n = 0;
va[1][3].u = 0;
va[1][3].v = 0;
va[1][3].n = 0;
}
main()
{
int i = 0, j = 0, plgnnum;
struct polygon *p;
struct Edge *ep;
for (i = 0; i < POLYGONMAX; i++)
for (j = 0; j < VERTEXMAX; j++)
vertexarray[j].n = -100000.0;
setva (vertexarray);
for (i = 0; i < YMAX; i++)
PT = NULL;
buildPT (vertexarray, PT);
/*this bloc of code is for test, please delete it after test successfuly*
for (i = 0; i < YMAX; i++) {
p = PT;
while (p != NULL) {
printf ("\nIn main: %dth polygon's a=%f, b=%f, c=%f, d=%f, vmax=%d", p->pi, p->a, p->b, p->c, p->d, p->vmax);
p = p->next;
}
}
getch();
/*the above code is for test, please delete it after test*/
polygon_ET = (struct Edge **)malloc(POLYGONMAX * sizeof(struct Edge *));
if (polygon_ET != NULL){
for (i = 0; i < POLYGONMAX; i++){
*(polygon_ET + i) = (struct Edge *)malloc(YMAX * sizeof(struct Edge));
if (*(polygon_ET + i) != NULL)
printf ("\nIn main: polygon_ET[%d] is not NULL", i);
else if (*(polygon_ET + i) == NULL)
printf ("\nIn main: polygon_ET[%d] is NULL", i);
getch();
}
}
for (i = 0; i < POLYGONMAX; i++)
if ((polygon_ET != NULL) && (*(polygon_ET + i) != NULL)){
for (j = 0; j < YMAX; j++)
if ((*(polygon_ET + i) + j) != NULL){
(*(polygon_ET + i) + j)->n = -100000.0;
printf ("\nIn main: polygon_ET[%d][%d] = %f", i, j, (*(polygon_ET + i) + j)->n);
}
getch();
} else
printf ("\nIn main: polygon_ET[%d] is NULL", i);
plgnnum = calcplgnnum (vertexarray);
for (i = 0; i < plgnnum; i++)
buildET(vertexarray, i, polygon_ET);
/*the following code is for test, please delete them after test successfully*
for (i = 0; i < POLYGONMAX; i++)
for (j = 0; j < YMAX; j++){
ep = *(polygon_ET + i) + j;
while (ep != NULL) {
printf ("\n%dth polygon, vmin=%d, vmax=%d", i, j, ep->vmax);
ep = ep->next;
}
};
/*the above code is for test, please delete them after test successfully*/
for (i = 0; i < POLYGONMAX; i++)
free(*(polygon_ET + i));
free(polygon_ET);
}
相关部分就这些,其它都没关系!
作者: liklstar 发布时间: 2010-08-09
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28