2017-01-25 1 views
-5

Ich habe eine Anwendung, die Grafikelemente speichern soll. Ich habe ein Problem, bei dem ich nicht auf eine Variable in einer Struktur zugreifen kann. Hier ist was ich bisher habe.Ein Wert in einer Struktur in C kann nicht festgelegt werden. FEHLER: 0xC0000005: Schreibort für Zugriffsverletzung 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); 

Immer wenn ich strcpy(pElement->name,name); versuchen zuweisen es sagt mir Zugriffsverletzung.

Die beiden anderen Dateien, mit denen ich arbeite, können nicht geändert werden und stammen aus einem Lehrbuch namens 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; 
    } 
} ///:~ 

und die .h-Datei ...

//: 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 und C++ sind verschiedene Sprachen. Bitte verwenden Sie nur das relevante Sprach-Tag. – kaylum

+1

'GraphicElement * pElement = nullptr; strcpy (pElement-> Name, Name); '. Ist das Problem nicht klar? Was denkst du, wie du auf einen Nullzeiger zugreifen kannst? – kaylum

+0

Was ist die Frage, die Sie stellen möchten? –

Antwort

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

Irgendwo zwischen der oberen Linie und der unteren Zeile über Sie Speicher für pElement zuweisen müssen.

pElement = new GraphicElement();

Bedenken Sie auch eine std::shared_ptr anstelle eines Rohzeiger verwenden, da Sie diese C++ markiert.

Verwandte Themen