Object pointer problem

I have a pointer to structyre Igrac and I have created a list with malloc (although I think I'm going to make it a vector after writeing this post) and when I try to acces any variable with it the program just crases.
Any idea why this might happen?
malloc() gets raw memory from the default heap. Do not use malloc()/free() in C++ unless you have a specific reason to do so (which you don't right now).

C++ provides new/delete operators as alternatives. Operator new gets memory and initialises the object on that memory location (it runs the object's constructor).

Further more, new can be overloaded, so your object can placed in custom heaps, at a specific address, ... all without changing your object.
I am using new now, but again it stops at the same pleace.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
typedef struct igrac {
    char ime[21];
    int skor,vreme;
    struct igrac *sl;
} Igrac;


void scores()
{
     FILE *file;
     char ff[21];
     
     file=fopen("scores.txt","r");
     
     if (!file) return;
     
     Igrac *prvi,*poslednji;
     
     int c,r=0;
     while (fgets(ff,20,file)!=NULL)
     {
          c=-1;
          switch(r)
          {
               case 0: if (rec2(ff,"nov\n")) { c=0; r=1; break; } else goto df1;
               case 1: if (rec2(ff,"skor\n")) { c=1; r=2; break; } else goto df;
               case 2: if (rec2(ff,"vreme\n")) { c=2; r=0; break; } else goto df;
               default:
                    df:if (rec2(ff,"nov\n")) { c=0; r=1; }
                    else df1:if (rec2(ff,"skor\n")) { c=1; r=2; }
                    else if (rec2(ff,"vreme\n")) { c=2; r=0; }
          }
          if (!prvi && c!=0) continue; //printf("\n%s",ff);
          if (fgets(ff,20,file)==NULL) break; //printf("\n%s\n%d%d",ff,c,r);
          switch(c)
          {
               case 0:
                    if (prvi) poslednji->sl=new /*(nothrow)*/ Igrac;
                    else poslednji=new /*(nothrow)*/ Igrac;
                    recjerec(poslednji->ime,ff);
                    if (!prvi) prvi=poslednji;
                    poslednji=poslednji->sl;
                    break;
               case 1:
                    poslednji->skor=tobroji(ff); break;
               case 2:
                    poslednji->vreme=tobroji(ff);
          }
     }
     fclose(file);
     
     if (!prvi) return;
     printf("\n%d\nerr",prvi->skor); //<- this test line dosen't work (same thing down there)
     for (Igrac *igr=prvi; igr; igr=igr->sl) //Testing if it read corectly
     printf("%s   %d  %d",igr->ime,igr->skor,igr->vreme);
     
}

rec2 compares 2 strings
tobroji conversts string to integer
isbroji checkes if string is integrer, I'm going to add this check later...
Initialize your variables before using them
If you look at the code more closely you would see that I did that in switch(c)
But even if I didn't shouldn't they have random or null value?
random.

Igrac *prvi,*poslednji; ¿where do you initialize them?
By the way, you are probably leaking memory.
I mixed initialization with something else...
So I gied them value NULL and added some test prints to console and turns out function which I wrote for converting strings to integrers is returning a number which dosen't exist in the file. I think I know why so I'll try to fih it in the morning.
But I'm shure that wrong number isn't the reason I can' do anything with those variables. Although from some reason after it returns the number the program stops if I initialize the pointers.
Do you really have to use goto?

That really is a C program. C++ can make your life somewhat easier, but you do have to do things differently.

For a start, the C++ standard library has a linked list thing that you can use.
How do I use that linked list?

And insted of using namespace std, how can I say that I want to use eg. cout without std:: before it?
I did this before but can't remember how.
Anyone?
Put these after the includes:

1
2
3
using std::cout;
using std::cin;
using std::endl;


It would be good if you can use these instead of printf & scanf.
Last edited on
A linked list example is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <list>
#include <iostream>

typedef std::list<int> intlist;

int main()
{
    intlist l;
    for (int i = -2; i <= 2; ++i)
        l.push_back(i);

    for (intlist::const_iterator p = l.begin(); l != l.end(); ++p)
        std::cout << *p << std::endl;

    return 0;
}
Can you or someone explain to me those list functions like this push_back ?
I can't use something if I don't know what it dose...
Look in the reference section (top left of this page), or google C++ push_back
¿what do you think it does?
here's a reference http://cplusplus.com/reference/list/list/
Ok, so I was reading about forward lists (because they are read only in one way and that is all I need), I understanded how to make this forward list object but not how to asign values to each of the elements from my structure.

But is it realy important do I use the list object insted of what I already did?
Can't you simply find out what is wrong with my corent code?

I planed to do this without using any library...
Last edited on
Topic archived. No new replies allowed.