2017-01-25 14 views
-5

グラフィック要素を格納すると思われるアプリケーションがあります。私は構造内の変数にアクセスできないという問題を抱えています。ここに私がこれまで持っていたものがあります。Cの構造体に値を設定できません。エラー:0xC0000005:アクセス違反の書き込み場所0x00000000

#include <crtdbg.h> 
#include <string.h> 
#include <stdio.h> 
#include "CLib.h" 

enum{ RUNNING = 1 }; 

struct Point 
{ 
    int x, y; 
}; 

struct Line 
{ 
    Point start; 
    Point end; 
}; 

struct GraphicElement 
{ 
    enum{ SIZE = 256 }; 
    char name[SIZE]; 
    CStash Lines; // a Stash of Lines 
}; 

struct VectorGraphic 
{ 
    CStash Elements; // a Stash of GraphicElements 
    }; 

    void AddGraphicElement(VectorGraphic*); 
    void ReportVectorGraphic(VectorGraphic*); 
    void CleanUpVectorGraphic(VectorGraphic*); 

    VectorGraphic Image; 
int main() 
{ 
    char response; 
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); 

    // it's a Stash of GraphicElements initialize(&(Image.Elements),sizeof(GraphicElement)); 
    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"); 
    } 
    return 0; 
} 

void AddGraphicElement(VectorGraphic* pImage){ 


    int i = 0, counter = 0; 
    int xPointStart = 0, yPointStart = 0; 
    int xPointEnd = 0, yPointEnd = 0; 
    char name[50]; 
    int lineNumber = 0; 


    GraphicElement *pElement = nullptr; 
    Line *pLine = nullptr; 


    initialize(&(Image.Elements), sizeof(GraphicElement)); 



    printf("ADDING A Graphic Element\n"); 
    printf("Please enter the name of the new GraphicElement(<256 characters): "); 
    fflush(stdin); 
    scanf("\n%[^\n]s", &name); 
    fflush(stdin); 

    strcpy(pElement->name,name); 

いつでも、私はそれは私に、アクセス違反を伝えるstrcpy(pElement->name,name);を割り当ててみてください。

他の2つのファイルは変更できません。Thinking in C++というテキストブックからのものです。

//: C04:CLib.cpp {O} 
// Implementation of example C-like library 
// Declare structure and functions: 
#include "CLib.h" 
#include <iostream> 
#include <cassert> 
using namespace std; 
// Quantity of elements to add 
// when increasing storage: 
const int increment = 100; 

void initialize(CStash* s, int sz) 
{ 
    s->size = sz; 
    s->quantity = 0; 
    s->storage = nullptr; 
    s->next = 0; 
} 

int add(CStash* s, const void* element) 
{ 
    if (s->next >= s->quantity) //Enough space left? 
     inflate(s, increment); 
    // Copy element into storage, 
    // starting at next empty space: 
    int startBytes = s->next * s->size; 
    unsigned char* e = (unsigned char*)element; 
    for (int i = 0; i < s->size; i++) 
     s->storage[startBytes + i] = e[i]; 
    s->next++; 
    return(s->next - 1); // Index number 
} 

void* fetch(CStash* s, int index) 
{ 
    // Check index boundaries: 
    assert(0 <= index); 
    if (index >= s->next) 
     return 0; // To indicate the end 
    // Produce pointer to desired element: 
    return &(s->storage[index * s->size]); 
} 

int count(CStash* s) 
{ 
    return s->next; // Elements in CStash 
} 

void inflate(CStash* s, int increase) 
{ 
    assert(increase > 0); 
    int newQuantity = s->quantity + increase; 
    int newBytes = newQuantity * s->size; 
    int oldBytes = s->quantity * s->size; 
    unsigned char* b = new unsigned char[newBytes]; 
    for (int i = 0; i < oldBytes; i++) 
     b[i] = s->storage[i]; // Copy old to new 
    delete[](s->storage); // Old storage 
    s->storage = b; // Point to new memory 
    s->quantity = newQuantity; 
} 

void cleanup(CStash* s) 
{ 
    if (s->storage != 0) 
    { 
     cout << "freeing storage" << endl; 
     delete[]s->storage; 
    } 
} ///:~ 

と.hファイル...どこかpElement用のメモリを割り当てる必要があります上記のトップラインとボトムラインの間

//: C04:CLib.h 
// Header file for a C-like library 
// An array-like entity created at runtime 

typedef struct CStashTag { 
    int size;  // Size of each space 
    int quantity; // Number of storage spaces 
    int next;  // Next empty space 
    unsigned char* storage;// Dynamically allocated array of bytes 
} CStash; 

void initialize(CStash* s, int size); 
void cleanup(CStash* s); 
int add(CStash* s, const void* element); 
void* fetch(CStash* s, int index); 
int count(CStash* s); 
void inflate(CStash* s, int increase); 
///:~ 
+2

CとC++は異なる言語です。関連する言語タグのみを使用してください。 – kaylum

+1

'GraphicElement * pElement = nullptr; strcpy(pElement-> name、name); '。問題は明確ではないのですか?ヌルポインタにアクセスするとどうなると思いますか? – kaylum

+0

質問したい質問は何ですか? –

答えて

0
GraphicElement *pElement = nullptr; 
// ... 
strcpy(pElement->name,name); 

pElement = new GraphicElement();

また、あなたがこのC++をタグ付け与えstd::shared_ptrの代わりに、生のポインタを、使用することを検討してください。

関連する問題