我在一个函数中使用realloc()
并使用相同的算法时遇到问题,但在另一个函数中则无效。我不明白自己在哪里犯错。
对于initVertex
功能,此算法工作正常。但是在initFace
中,每次我尝试为faces
分配内存时,都会说:
realloc(): invalid next size.
我正在读取的文件为.obj
格式:
对于顶点:v -0.187264 0.10108 0.266814
用于面部:f 938/938/936 944/944/942 936/936/934
这里是代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "Scene.h"
#define MAX_ID 10
#define LIMIT 10
#define MEM_INCREASE 10
void setArrToZero(char *arr) {
for (int i = 0; i < LIMIT; ++i) {
*(arr + i) = 0;
}
}
void initFace(Object *obj, char *line, int length, int *maxFaces) {
int i = 0, strCounter = 0;
(obj->faces)[obj->numberOfFaces].size = 0;
(obj->faces)[obj->numberOfFaces].vertex = (int*) malloc(MAX_ID * sizeof(int));
char *numStr = (char*) malloc(length * sizeof(char));
enum boolean flag = FALSE;
if (obj->numberOfFaces == *maxFaces) {
obj->faces = (Face*) realloc(obj->faces,
(*maxFaces + MEM_INCREASE) * sizeof(Face));
*maxFaces += 10;
}
while (i != length) {
char c = line[i];
if (c == ' ' && flag == FALSE) //turn the flag on
flag = TRUE;
if (c == '/' && flag == TRUE) { //turn the flag off and initialize Face.
flag = FALSE;
(obj->faces)[obj->numberOfFaces].vertex[obj->faces->size] = atoi(
numStr);
(obj->faces)[obj->numberOfFaces].size++; // increase number of vertexes of the face.
setArrToZero(numStr);
strCounter = 0;
}
if (flag == TRUE) {
numStr[strCounter] = c;
strCounter++;
}
if ((obj->faces)[obj->numberOfFaces].size == MAX_ID) {
break;
}
i++;
}
obj->numberOfFaces++;
free(numStr);
}
void initVertex(Object *obj, char *line, int *maxVertexes) {
float x, y, z;
if (line[1] != ' ')
return;
else {
if (obj->numberOfVertexes == *maxVertexes) {
obj->vertexes = (Vertex*) realloc(obj->vertexes,
sizeof(Vertex) * (*maxVertexes + MEM_INCREASE));
*maxVertexes += 10;
}
sscanf(line, "%*[^-0123456789]%f%*[^-0123456789]%f%*[^-0123456789]%f",
&x, &y, &z);
(obj->vertexes)[obj->numberOfVertexes].x = x;
(obj->vertexes)[obj->numberOfVertexes].y = y;
(obj->vertexes)[obj->numberOfVertexes].z = z;
obj->numberOfVertexes++;
}
}
Object* createObject(char *fileName) {
Object *obj = (Object*) malloc(sizeof(Object));
int maxVertexes = 10, maxFaces = 10;
obj->numberOfFaces = 0;
obj->numberOfVertexes = 0;
obj->vertexes = (Vertex*) malloc(maxVertexes * sizeof(Vertex));
obj->faces = (Face*) malloc(maxFaces * sizeof(Face));
FILE *inputF;
size_t len = 0;
char *line = NULL;
inputF = fopen(fileName, "r");
if (inputF == NULL) {
printf("File not exist!");
return NULL;
}
while (!feof(inputF)) {
getline(&line, &len, inputF);
char c = line[0];
switch (c) {
case 'v':
initVertex(obj, line, &maxVertexes);
break;
case 'f':
initFace(obj, line, len, &maxFaces);
break;
default:
break;
}
}
return obj;
}
int main() {
Object *obj = createObject("Donald.obj");
printf("done");
}
initVertex()
重新分配数组vertexes
之前,它使用越界索引numberOfVertexes
。
[initFace()
在这里两次使用越界索引numberOfFaces
:
(obj->faces)[obj->numberOfFaces].size = 0;
(obj->faces)[obj->numberOfFaces].vertex = (int*) malloc(MAX_ID * sizeof(int));
并且只有after完成后才重新分配数组faces
。
超出范围访问数组faces
会引发未定义的行为。从那时起任何事情都会发生。在这种特殊情况下,程序的内存管理似乎已经搞砸了。