动态内存分配不匹配
问题描述:
现在我坚持使用内存分配几个小时。基本上,我必须添加一个新的图形元素的矢量图形,其初始化VectorGraphic
是必要的。动态内存分配不匹配
第一个问题是用InitVectorGraphic
方法内的存储器分配被固定? (我认为)现在我坚持的第二个问题是,即使内存分配自InitVectorGraphic
方法,pElements
在AddGraphicElement
方法中没有内存。 (即,即使是在一个方法(InitVectorGraphic
法)初始化之后,改变不会在其他方式体现)
这里是我的全码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
enum{
RUNNING = 1
};
struct Point
{
int x, y;
};
struct Line
{
Point start;
Point end;
};
struct GraphicElement
{
enum{
SIZE = 256
};
unsigned int numLines;
Line* pLines;
char name[SIZE];
};
typedef struct
{
unsigned int numGraphicElements;
GraphicElement* pElements;
}VectorGraphic;
void InitVectorGraphic(VectorGraphic* image){
image = (VectorGraphic*)malloc(sizeof(VectorGraphic));
(*image).pElements = (GraphicElement*)malloc(sizeof(GraphicElement)* 256);
//Problem part 1
};
void AddGraphicElement(VectorGraphic* image){
printf("\nADDING A Graphic Element"); //Problem part 2
int index = (*image).numGraphicElements;
printf("\nPlease enter the name of the new GraphicElement(<256 characters): ");
scanf("%s", &(*image).pElements[index].name);
printf("How many lines are there in the new GraphicElement? ");
scanf("%d", &(*image).pElements[index].numLines);
(*image).pElements[index].pLines = (Line*)malloc(sizeof(Line)* (*image).pElements[index].numLines);
for (int i = 0; i < (*image).pElements[index].numLines; i++){
Line line;
printf("Please enter the x coord of the start point of line index %d: ", i);
scanf("%d", &line.start.x);
printf("Please enter the y coord of the start point of line index %d: ", i);
scanf("%d", &line.start.y);
printf("Please enter the x coord of the end point of line index %d: ", i);
scanf("%d", &line.end.x);
printf("Please enter the y coord of the end point of line index %d: ", i);
scanf("%d", &line.end.y);
(*image).pElements[index].pLines[i] = line;
}
(*image).numGraphicElements = (*image).numGraphicElements + 1;
printf("Added");
//add graphicElement to Image
};
void ReportVectorGraphic(VectorGraphic* image){
printf("\nVectorGraphic Report");
for (int i = 0; i < (*image).numGraphicElements; i++){
printf("\nReporting Graphic Element #%d", i);
printf("\nGraphic Element name: %s", (*image).pElements[i].name);
for (int j = 0; j < (*image).pElements[i].numLines; j++){
printf("\nLine #%d start x: %d", j, (*image).pElements[i].pLines[j].start.x);
printf("\nLine #%d start y: %d", j, (*image).pElements[i].pLines[j].start.y);
printf("\nLine #%d end x: %d", j, (*image).pElements[i].pLines[j].end.x);
printf("\nLine #%d end y: %d", j, (*image).pElements[i].pLines[j].end.y);
}
}
};
void CleanUpVectorGraphic(VectorGraphic* image){
free(image);
};
VectorGraphic Image;
int main()
{
char response;
InitVectorGraphic(&Image);
while (RUNNING)
{
printf("\nPlease select an option:\n");
printf("1. Add a Graphic Element\n");
printf("2. List the Graphic Elements\n");
printf("q. Quit\n"); printf("CHOICE: ");
fflush(stdin);
scanf("%c", &response);
switch (response)
{
case '1':AddGraphicElement(&Image);
break;
case '2':ReportVectorGraphic(&Image);
break;
case 'q':CleanUpVectorGraphic(&Image);
return 0;
default:printf("Please enter a valid option\n");
}
printf("\n");
}
}
什么解决的办法?
答
这是非常标准的指针混淆。你需要以下变化:
VectorGraphic* Image; //make it a pointer
void InitVectorGraphic(VectorGraphic** image) //expect pointer-to-pointer
{
*image = (VectorGraphic*)malloc(sizeof(VectorGraphic)); //assign allocated buffer to pointer
(*image)->pElements = (GraphicElement*)malloc(sizeof(GraphicElement)* 256);
}
InitVectorGraphic(&Image); //call remains the same
在你的旧代码,您正在泄漏内存,你能想到的形象当作如下局部定义的变量:
执行InitVectorGraphic(VectorGraphic* image)
{
image = malloc(); //Here you are allocating memory to a local variable not to the address Image
//no free()'ing of image variable
}
步骤,按您的旧代码是简单地做两个任务,第二个任务覆盖原来的一个:
VectorGraphic *image = &Image;
image = malloc();
这是c或C++吗? –
当我定义void void(int x){x = 5;}'然后做int i = 7; f(i);'以后我的价值是多少?如果我做了'f(7)',那么怎么样?' - 之后7的价值是多少?最后,如果我定义了'void g(int * q){q = malloc(sizeof(int));}'并且执行int * p = NULL; g(p);'p之后的价值是多少? – immibis
对不起,这是在C. @CaptainGiraffe – user3397557