C++ "Bus error: 10" and working with pointers -
i'm doing data structures exercise , have been blocked since yesterday bus error, reckon because i'm doing bad things memory. cannot figure out exactly.
these requirements have established practice:
- able add product (any way do) list
- able retrieve product in list @ current position (next, prev, movetostart, movetoend… there's cursor pointer, called "actual" here)
- any changes retrieved product should updated in data structure (ie. list::retrieve(*product), product->visits++)
this code have. apologies var names, have in spanish , therefore names in spanish.
class producto { // product public: string marca; double precio; int visitas; int compras; producto () {} producto (string m, double p, int v = 0, int c = 0) : marca(m), precio(p), visitas(v), compras(c) {} }; class nodo { public: producto valor; // value nodo *siguiente; // next nodo *anterior; // prev nodo (producto p, nodo *a = null, nodo *s = null) : valor(p), anterior(a), siguiente(s) {} }; class lista { private: nodo *inicio; nodo *final; nodo *actual; public: lista(); bool esta_vacia(); // empty? bool es_final(); // end? int insertar(producto p); // insert given p void moverprincipio(); // "move beginning" void siguiente(); // "next" void imprimir(); // "print" int leer(producto *p); // read, return 0 or 1 if successful, return product ref }; lista::lista() { this->inicio = null; this->final = null; this->actual = null; } bool lista::esta_vacia() { return (this->inicio == null); } bool lista::es_final() { return (this->actual == null); } void lista::moverprincipio() { this->actual = this->inicio; } void lista::siguiente() { if(!this->es_final()) { this->actual = this->actual->siguiente; } } void lista::imprimir() { int = 1; producto *p; this->moverprincipio(); while(!this->es_final()) { if(this->leer(p) == 0) { cout << << ".- ##" << p->marca << "##, views ##" << p->visitas << "##\n"; p->visitas++; i++; this->siguiente(); } } } int lista::leer(producto *p) { if(this->actual != null) { *p = this->actual->valor; return 0; } else { return 1; } } int lista::insertar(producto p) { if(this->esta_vacia()) { nodo *tmp = new nodo(p); this->inicio = tmp; this->final = this->inicio; } else { nodo *tmp = new nodo(p, this->final); this->final->siguiente = tmp; this->final = tmp; } return 0; }
i have removed unnecessary code. how i'm using (and failing miserably):
lista *productos = new lista(); productos->insertar(producto("shoes", 19.90)); productos->insertar(producto("socks", 25.00)); // should expect views = 0 productos->imprimir(); // now, views = 1 productos->imprimir();
upon execution, thing "bus error: 10" when doing imprimir ("print"), first time. insertion works without errors (but wrong there too).
my idea hold product inside node, , give reference location when returning it, changes reflected there (for example, increase view or purchase counter of retrieved element, reflects change when reading list later).
i'd extremely thankful if point out mistakes i'm doing here.
thanks!!
update here's compilable example.
you pass pointer lista::leer
, want write value it. writing in unallocated memory. probably, wanted pointer actual
element.
first of all, need modify signature:
int lista::leer(producto **p);
note double star, since writing pointer itself.
then, have assign pointer actual->valor
in lista::leer
:
*p = &(this->actual->valor);
finally, have pass pointer p
in lista::imprimir
:
if(this->leer(&p) == 0) { // ... }
alternatively, might modify lista::leer
return pointer , check if nullptr
/null
.
Comments
Post a Comment