1

I have started studying C++ after some years in C# and other languages. I am facing the class arguments (constructors, inheritance, copy etc) and I was trying to write a bad sample code. Below is a sample class (.h and .cpp):

#ifndef SAMPLE_H
#define SAMPLE_H

#include <iostream>

class Sample
{
    public:
        Sample();
        //Sample(const Sample& s);
        virtual ~Sample();
        int *s_array;
    protected:

    private:

};

void print(const Sample *s);

#endif // SAMPLE_H

Sample::Sample()
{
    std::cout<<"create sample\n";
    s_array=new int[10];
    std::cout<<"alloc memory 10 int array\n";

    for(int i=0; i<10; ++i)
    {
        s_array[i]=i;
    }
}

Sample::~Sample()
{
    //dtor
    std::cout<<"Dealloc memory 10 int array\n";
    delete [] s_array;
    std::cout<<"destroy sample\n";
}

void print(const Sample *s)
{
    std::cout<<s<<" "<<s->s_array<<'\n';

    for(int i=0; i<10; ++i)
    {
        std::cout<<s->s_array[i]<<" ";
    }
    std::cout<<"\n\n";
}

Then in main

#include <iostream>
#include "Sample.h"

using namespace std;

int main()
{
    cout<<endl<<"Let's try the Copy const WRONG....  "<<endl;
    Sample *s1=new Sample();
    print(s1);
    Sample s2(*s1);
    cout<<endl<<"What is s2 ???  "<<endl;
    print(&s2);
    delete s1;
    cout<<endl<<"What is s2 NOW after s1 delete???  "<<endl;
    print(&s2);
    return 0;
}

I wanted to test the dangers of NOT to use the copy constr and i expected to see after the deletion of s1 a totally 'dirty' array (i.e., 10 random values or even a crash) This is the output I gain (Win 10 pro, IDE CodeBlock, GNU Gcc compiler): Let's try the Copy const WRONG.... create sample alloc memory 10 int array 0x1ba110 0x1b6e48 0 1 2 3 4 5 6 7 8 9

What is s2 ??? 0x6efdf0 0x1b6e48 0 1 2 3 4 5 6 7 8 9

Dealloc memory 10 int array destroy sample

What is s2 NOW after s1 delete??? 0x6efdf0 0x1b6e48 1812296 1769664 2 3 4 5 6 7 8 9

Dealloc memory 10 int array destroy sample

Why only the first two items of s_array are 'dirty' and the remaining 8 are good? Why the deletion of object s1 does not free the whole memory pointed by s2? Thanx in advance Diego

bitdiego
  • 113
  • 10
  • 1
    Possible duplicate of [Why is undefined behavior allowed (as opposed to not compiling/crashing)?](https://stackoverflow.com/questions/2802271/why-is-undefined-behavior-allowed-as-opposed-to-not-compiling-crashing) – Sneftel Aug 21 '19 at 13:18
  • When you say "free the whole memory" - do you expect a physical hole in a RAM chip, so the memory is no longer accessible? Do you expect the compiler to spend CPU cycles to deliberately manufacture random values to overwrite previous memory contents with? Of course the memory still holds whatever was written to it most recently. – Igor Tandetnik Aug 21 '19 at 15:04
  • hello, igor, thank you for the reply. my question rose because of the result i had from the application. i mean, i thought that when i free an allocated piece of memory, then that memory region should contain an unpredictable value. if i had allocated space for a, say, single integer, i could have find right to see a strange value such as 12397454587421 after trying to access that memory location after free action. but why, in my example, only the first two locations of the array are 'dirty'? why not the first three? or simply the first? it depends on the compiler? or on the machine? thanx – bitdiego Aug 23 '19 at 09:27

0 Answers0