簡體   English   中英

動態內存分配不匹配

[英]Dynamic memory allocation mismatch

現在,我在內存分配上停留了幾個小時。 基本上,我必須向矢量圖形添加一個新的圖形元素, VectorGraphic必須對VectorGraphic進行初始化。

第一個問題是InitVectorGraphic方法內部的內存分配是固定的嗎? (我認為)現在我InitVectorGraphic的第二個問題是,即使從InitVectorGraphic方法分配了內存, pElementsAddGraphicElement方法內部也沒有內存。 (即,即使使用一種方法( 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM