Eux Eux -4 years ago 159
C++ Question

Visual Studio C++ 2k15 - Getting error on pointer address

this is my first post on Stack Overflow :)

Sorry if I seems egoistic, but I have an exam tomorrow and I'm facing a problem that I cannot solve. Hope to find here an answer, I tried to find out if there was already an opened 3d, but I cannot find it.

Here's my problem:
I'm writing a code in C++, using the pointer. Because of I retrieve always the same error on a larger code, I tried with an easier one, but the error persists.
The code is the following:

#include <iostream>
using namespace std;

struct EXAMPLE {
int value;
EXAMPLE *next;
};

int insertnew(EXAMPLE *&sorting, EXAMPLE val);
int printlist(EXAMPLE *&sorting);

int main() {
int i;
EXAMPLE new;
EXAMPLE *list = NULL;

for (i = 0; i < 3; i++)
{
cout << "value: " << endl;
cin >> new.value;
insertnew(list, new);

}
printlist(list);
system("Pause");
return 0;
}

int insertnew(EXAMPLE *&sorting, EXAMPLE val){
EXAMPLE *temp;
temp = new EXAMPLE;
temp->value = val.value;
temp->next = sorting;

sorting = temp;

return 0;
}

int printlist(EXAMPLE *&sorting) {
while (sorting != 0)
{
sorting = sorting->next;
cout << sorting->next << " " << sorting->value << endl;
}
return 0;
}


It's an easy LIFO structure.
I get a bad reading access error on this line:

cout << elenco->next << " " << elenco->valore << endl;


But here's the curios thing.
If I revert the lines in this way:

int printlist(EXAMPLE *&sorting) {
while (sorting != 0)
{
cout << sorting->next << " " << sorting->value << endl;
sorting = sorting->next;
}
return 0;
}


as a FIFO structure, I got no error at all!

Can you help me understanding where the problem is?
Thanks in advance

//edit

Uops, find out my error.

The while was supposed to be a recursive function, dunno why I inserted a while, assuming it will function as a recursive function.

just solved

cout << lista->value << endl;
while (list != NULL)
{
list = list->next;
if (list != 0) {
cout << list->value << endl;
}


}

Sorry

Answer Source

Let's take a look at your example code:

int printlist(EXAMPLE *&sorting) {
    while (sorting != 0) //As long as "sorting" is not null
    {
        sorting = sorting->next; //Set "sorting" to the next value
        cout << sorting->next << " " << sorting->value << endl; //print the value
    }
    return 0;
}

Just play this code through if you have only one element in your LIFO list.

int printlist(EXAMPLE *&sorting) {
    while (sorting != 0) //"sorting" is not null
    {
        sorting = sorting->next; //Set "sorting" to the next value, which is null
        cout << sorting->next << " " << sorting->value << endl; //crash because "sorting" is null
    }
    return 0;
}

This will happen every time you hit the last element.

You can change your code to a do-while-loop with an additional if, or add an if with a break within your current loop. But I would recommend to go with a for-loop on this one.

Also you don't need to pass the pointer by reference. You even shouldn't do it, as you don't want to change your list when printing it (as your current code does...)

How about something like this:

int printlist(EXAMPLE * sorting) {
    for (EXAMPLE * current = sorting; current != 0; current = current->next)
    {
        cout << current << " " << current->value << " -> " << current->next << endl;
    }
    return 0;
}

I also changed the output a little bit to show more interesting data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download