2016-09-25 17 views
0

私は数時間のメモリ割り当てに悩まされています。基本的には、ベクターグラフィックに新しいグラフィックエレメントを追加しなければなりません。そのためには、VectorGraphicの初期化が必要です。動的なメモリ割り当ての不一致

最初の問題は、修正されたInitVectorGraphicメソッド内のメモリ割り当てにありますか? (私は思う)今、私が悩んでいる2番目の問題は、メモリが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"); 
    } 
} 

ソリューションとは何ですか?

+2

このcまたはC++はどちらですか? –

+0

私は 'void f(int x){x = 5;}'を定義し、次に 'int i = 7; f(i); 'その後のiの値は何ですか?私が 'f(7);を実行するとどうなりますか? - その後の7の値は何ですか?最後に、 'void g(int * q){q = malloc(sizeof(int));}'を定義し、 'int * p = NULL; g(p); 'その後のpの値は? – immibis

+0

申し訳ありませんが、これはCです。@CaptainGiraffe – user3397557

答えて

0

これはポインタとの非常に標準的な混乱です。あなたは、次の変更を必要とする:あなたの古いコードで

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(); 
関連する問題