Pointer und Kopie
-
Hallo,
ich habe ein Bib zu Verfügung, die einige Vorgaben hat, wie das typedef (siehe unten). Ich möchte aus einer Fkt. ein Objekt (möglichst mit wenig Kopien) an den cellPtrPtr übergeben bzw. wiederrum in sich speichern. Ich habe versucht das Schema zu skizzieren. Ich hoffe es wird klar, was das soll! Ich sehe vor lauter Pointern nicht mehr durch...bitte um Hilfe! Die Zeile mit der ich nicht klar komme ist markiert.
#include <iostream> class cell { public : cell input(); private : int index; }; static cell list; typedef cell* cellPtr; // muss so bleiben cell cell::inputExo( ) { list.index=2000; return ( &list ); } int main() { cellPtr* cellPtrPtr; // ist vorgegeben und muss bleiben! cellPtrPtr = new cellPtr[3]; // 3 ist beliebig cell* myPtr; // kann geändert werden cellPtrPtr[0] = myPtr->inputExo(); // wie geht das??? }Danke schon mal,
Jo
-
Lass cell::input einen cellPtr zurückgeben. (Nebenbei: myPtr muss auf ein Objekt zeigen, bevor du eine Methode aufrufen kannst. Und du hast einmal input deklariert aber inputExo definiert).
-
Also wie ich das jetzt verstanden habe, dann etwa so. Nur bekommen ich jetzt ein Speicherzugriffsfehler!?
#include <iostream> class cell { public : cell* inputExo(); private : int index; }; typedef cell* cellPtr; static cellPtr list; cell* cell::inputExo( ) { list->index=2000; return ( list ); } int main() { cellPtr* cellPtrPtr; // ist vorgegeben und sollte bleiben! cellPtrPtr = new cellPtr[3]; cell* myPtr = 0; cell mycell; myPtr = &mycell; cellPtrPtr[0] = myPtr->inputExo(); }
-
wenn ich valgrind mache, dann kommt sowas:
valgrind --show-reachable=yes ./fun -v
==5395== Memcheck, a memory error detector.
==5395== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==5395== Using LibVEX rev 1658, a library for dynamic binary translation.
==5395== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==5395== Using valgrind-3.2.2.SVN, a dynamic binary instrumentation framework.
==5395== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==5395== For more details, rerun with: -v
==5395==
==5395== Invalid write of size 4
==5395== at 0x4008D7: cell::input() (in /home/jo/project/exodustest/fun)
==5395== by 0x400978: main (in /home/jo/project/exodustest/fun)
==5395== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==5395==
==5395== Process terminating with default action of signal 11 (SIGSEGV)
==5395== Access not within mapped region at address 0x0
==5395== at 0x4008D7: cell::input() (in /home/jo/project/exodustest/fun)
==5395== by 0x400978: main (in /home/jo/project/exodustest/fun)
==5395==
==5395== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 3 from 1)
==5395== malloc/free: in use at exit: 24 bytes in 1 blocks.
==5395== malloc/free: 1 allocs, 0 frees, 24 bytes allocated.
==5395== For counts of detected errors, rerun with: -v
==5395== searching for pointers to 1 not-freed blocks.
==5395== checked 191,632 bytes.
==5395==
==5395== LEAK SUMMARY:
==5395== definitely lost: 0 bytes in 0 blocks.
==5395== possibly lost: 0 bytes in 0 blocks.
==5395== still reachable: 24 bytes in 1 blocks.
==5395== suppressed: 0 bytes in 0 blocks.
SpeicherzugriffsfehlerWas jemand wo der Fehler liegt!?
-
NoPanic2007 schrieb:
==5395== Invalid write of size 4
==5395== at 0x4008D7: cell::input() (in /home/jo/project/exodustest/fun)
==5395== by 0x400978: main (in /home/jo/project/exodustest/fun)
==5395== Address 0x0 is not stack'd, malloc'd or (recently) free'dCompilier und Linke mal mit -g, dann kommt in der zweiten Zeile bestimmt "(in xyz.cpp:17)". Ich nehme mal an Du weist dem cellPtr namens list nirgendwo eine gültige Adresse zu?
-
NoPanic2007 schrieb:
...
#include <iostream> class cell { public : cell* inputExo(); private : int index; }; typedef cell* cellPtr; static cellPtr list; cell* cell::inputExo( ) { list->index=2000; return ( list ); } int main() { cellPtr* cellPtrPtr; // ist vorgegeben und sollte bleiben! cellPtrPtr = new cellPtr[3]; cell* myPtr = 0; cell mycell; myPtr = &mycell; cellPtrPtr[0] = myPtr->inputExo(); }"list" ist ein Zeiger, der wohin zeigt ?
Anders ausgedrückt: Wellches "cell-Objekt" soll bei dieser Anweisung verändert werden:list->index=2000;??
Und schonmal für später: Wo "delete"st Du den mit new angeforderten Speicher ?
Gruß,
Simon2.
-
Ah okay,ich glaub, jetzt funktioniert es! Jetzt ist einiges klar geworden.
Danke!Am Ende mach ich noch:
delete [] cellPtrPtr;