About to go crazy...

Oats

Newcomer
Code:
#include "class_club_public.h"
#include "class_club_prive.h"
#include "fonctions.h"

void main()
{
  Club *pr;
  Club** liste;
	
  pr = new Club_Prive(); //statement 1
  (*liste) = new Club_Prive(); //statement 2
  (*(liste+0)) = new Club_Prive(); //statement 3
  (*(liste+1)) = new Club_Prive(); //statement 4
  liste[0] = new Club_Prive(); //statement 5	
}


I have an abstract class called Club. Another class, which inherits from Club, called Club_Prive. Now class Club_Prive works perfectly fine; constructors, membre functions and all.

In the code above, statment 1 works fine. Everytime I try to run statement 2 (3 or 4 or 5), the program crashes; compiles perfectly though. Wtf is up with this shit?
 
"liste" is a pointer to a pointer - you are not initializing it before you use it for the first time - that's why your program crashes. Try to add " liste = new Club *[3];" between statement 1 and 2 - this will allocate an array of 3 pointers to Club and make "liste" point to that array.

Also, statement 5 introduces a memory leak.
 
Thx for the help, greatly appreciated. I have few more questions.

If I was to write somethng like this: liste = new Club*;. Could I assign values to *(liste+i)? For example: *(liste+1) = new Club_Prive();. Further, could that lead to problems, i.e. *(liste+1) already containing some valid data?

If I was to write somethng like this: liste = new Club*;. Could I assign values to *(*liste+i)? I would need to initialize *liste first, right?

Also, am I correct to assume that trying to assign anything to *listre+i makes no sense?

Finally can you expand a bit on the memory leak thing?
 
Oats said:
Thx for the help, greatly appreciated. I have few more questions.

If I was to write somethng like this: liste = new Club*;. Could I assign values to *(liste+i)? For example: *(liste+1) = new Club_Prive();. Further, could that lead to problems, i.e. *(liste+1) already containing some valid data?
if you write "liste = new Club *;" you end up allocating only a single pointer, so that if you try to assign anything to *(liste+i) when i is anything else than 0 you will overwrite memory you haven't allocated, probably leading to a crash at some point. "*(liste+1) = new Club_Prive();" would certainly lead to problems in this particular case.
If I was to write somethng like this: liste = new Club*;. Could I assign values to *(*liste+i)? I would need to initialize *liste first, right?
For *(*liste+i) to work, you would need to allocate an array of Clubs like follows: "*liste = new Club[some_integer_larger_than_i];". Then you could assign a Club object directly to *(*liste+i), like follows: "Club foo /*=whatever*/; *(*liste+i)=foo;". Given that you have already stated that Club is an abstract class, this probably will not work anyway.

(*liste+i) is not the same as *(liste+i) - the unary * operand has a higher precedence than +. (*liste + i) means to first get the datum pointed to by liste, then add i to it. *(liste+i) means to first add together i and liste, then get (or write to) the datum pointed to by the result. Probably confusing as hell if you aren't used to C/C++-type pointer arithmetic - you probably should do some experimentation to get used to how it works.
Also, am I correct to assume that trying to assign anything to *listre+i makes no sense?
That's right.
Finally can you expand a bit on the memory leak thing?
Simple. *liste and liste[0] means exactly the same thing in C/C++ ( a is always equivalent to *(a+b) in C/C++) , and you are assigning twice to the same pointer - first time in statement 1, second time in statement 5. After the assignment in statement 5, you no longer have any pointers to the Club_Prive you allocated in statement 1, so you cannot free/delete it later even if you wanted to. And C++ is not kind or smart enough to do it for you, unlike e.g. Java.
 
Back
Top