-3

Assuming I have the base.h class

#ifndef BASE_H
#define BASE_H

class base
{
public:
    base(){}
    virtual void id() const{ std::cout << "base\n"; }
    virtual ~base(){}
protected:
private:
};

#endif // BASE_H

and the derived.h class

#ifndef DERIVED_H
#define DERIVED_H

#include <base.h>

class derived : public base
{
public:
    derived(){}
    void id() const{ std::cout << "derived\n"; }
protected:
private:
};

#endif // DERIVED_H

I started testing different outcomes having this code in the main

int main()
{
    base* bas;
    derived* der;
    base statbas;
    derived statder;

    *bas = statbas;   // here it crashes

    bas = new base;   

    *bas=statbas;     // here it works. It also worked with bas=&statbas;

    bas->id();

    return 0;
}

It seems that I need to allocate some memory for the pointer bas otherwise the program will crash. This thing confuses me mainly because I cannot imagine to store memory for an object (bas in this case) that does not have any variables (the class base is formed just by functions).

Another thing that confuses me it's that apparently *bas=statbas (bas points to statbas) and bas=&statbas (bas has the memory address of statbas) are not equal, in fact the first makes the program to crash while the second works.

I assume all these issues are connected to the function which is virtual in the base class, indeed when id() is not declared as virtual the program does not crash (but obviously does not work correctly either).

Can someone clear my mind about how the memory is managed in this case?

Arun A S
  • 6,421
  • 4
  • 29
  • 43
RDGuida
  • 546
  • 4
  • 15
  • Your question have noting to do with virtual functions. Make sure you understand how pointers work first. – ftynse Feb 16 '15 at 14:12
  • And just in case you were mistaken about what 'static' variables are (I am guessing from variable name). Variables declared in the function scope are not static – ftynse Feb 16 '15 at 14:15
  • I know static variables are other things, I just needed a name – RDGuida Feb 16 '15 at 15:07

3 Answers3

1
*bas = statbas;   // here it crashes

Because you dereference bas which is uninitialized and thus you get undefined behaviour.

bas = new base;
*bas=statbas;     // here it works. It also worked with bas=&statbas;

It works because bas was initialized on previous row and points to a valid object in memory.bas=&statbas does something else. It assigns the bas to point to another object, causing the newly allocated object to leak because it's no longer pointed to by anything and therefore can never be deleted.

It seems that I need to allocate some memory for the pointer bas otherwise the program will crash.

Correct.

This thing confuses me mainly because I cannot imagine to store memory for an object (bas in this case) that does not have any variables (the class base is formed just by functions).

You can allocate memory for objects that have no members in exactly the same way as you do for objects that do have members.

Another thing that confuses me it's that apparently *bas=statbas (bas points to statbas) and bas=&statbas (bas has the memory address of statbas) are not equal, in fact the first makes the program to crash while the second works.

That's not what the first one does. The first one dereferences bas and then copies statbas to the dereferenced object. The second one sets bas to point to statbas.

I assume all these issues are connected to the function which is virtual in the base class, indeed when id is not declared as virtual the program does not crash (but obviously does not work correctly either).

Nope, the issue is not really connected to the virtual function. You simply dereference an uninitialized pointer. The virtuality happens to be something that luckily caused the undefined behaviour to be different.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • How come it does not produce unexpected beheaviour when the function is not declared as virtual? – RDGuida Feb 16 '15 at 15:25
  • There is no behaviour that can be "expected" when the behaviour is undefined. Therefore no behaviour should be unexpected either. – eerorika Feb 16 '15 at 15:56
0

When you dereference a pointer, it should point to something. Yours is not initialized at all.

To make a pointer point to the address of an automatic variable, you should use take its address as bas = &statbas.

ftynse
  • 787
  • 4
  • 9
0

It seems that I need to allocate some memory for the pointer bas otherwise the program will crash. This thing confuses me mainly because I cannot imagine to store memory for an object (bas in this case) that does not have any variables (the class base is formed just by functions).

When you declare a pointer, regardless of type it is initially not pointing anywhere.

int* p;

Assigning to such a pointer will certainly crash just as writing

int n;

is creating a variable n with some arbitrary value.

Another thing that confuses me it's that apparently *bas=statbas (bas points to statbas) and bas=&statbas (bas has the memory address of statbas) are not equal, in fact the first makes the program to crash while the second works.

If bas would have pointed somewhere then

*bas = statbase;

would have copied the contents of statbase to where bas was pointing, since bas is not pointing anywhere particular you get a crash.

writing

 bas = &statbase;

is valid since bas is a pointer you assign it to point to an instance of 'base' namely statbase.

I assume all these issues are connected to the function which is virtual in the base class, indeed when id is not declared as virtual the program does not crash (but obviously does not work correctly either).

You assume wrong, all these issues are connected to memory management and pointers.

AndersK
  • 35,813
  • 6
  • 60
  • 86