Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Prog2
/
eloadas_peldak
/
ea_05
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
A prog2-höz tartozó friss repo anyagok itt elérhetőek:
https://git.iit.bme.hu/
Commit
e01d2e5e
authored
Mar 09, 2020
by
Szeberényi Imre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kommentek
parent
8daf658a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
12 deletions
+89
-12
otthonMegMukodott/main.cpp
+21
-6
otthonMegMukodott/otthonMegMukodott.cbp
+2
-2
virt_destruktor/Makefile
+40
-0
virt_destruktor/main.cpp
+26
-4
No files found.
otthonMegMukodott/main.cpp
View file @
e01d2e5e
/*
* Demonstrációs program tipikus hibára, ami memória szivárgást okoz.
* A hiba UNIX/LINUX környezetben demonstrálható könnyen, mivel ott a
* futtatandó program heap használata könnyen korlátozható.
* Fordítás: make
* Két program készül: virtDestr és nemVirtDestr
* Futtatás: make run
* Futtatáskor korlátozzuk a heap méretét ami a nemVirtDestr változatnak nem elég
*/
#include <iostream>
#include <cstdlib>
#ifndef VIRT // ha nem definiált akkor üres lesz
# define VIRT
#endif // VIRT
/// A VIRTUAL makró vagy üres, vagy virtual kulcsszót tartalmaz
#ifndef VIRTUAL // ha nem definiált akkor üres lesz
# define VIRTUAL
#endif // VIRTUAL
struct
A
{
VIRT
~
A
()
{
VIRT
UAL
~
A
()
{
std
::
cout
<<
'.'
;
};
};
/// Amennyiben az alaposztály destruktora nem virtuális,
/// akkor nem fog meghívódni a származtatott osztály destruktora.
class
B
:
public
A
{
char
*
p
;
public
:
B
()
{
p
=
new
char
[
1000
];}
~
B
()
{
delete
[]
p
;
}
~
B
()
{
delete
[]
p
;
std
::
cout
<<
'*'
;
}
};
int
main
(
int
argc
,
char
*
argv
[])
{
...
...
@@ -25,7 +38,9 @@ int main(int argc, char *argv[]) {
for
(
int
i
=
0
;
i
<
num
;
i
++
)
{
A
*
pa
=
new
B
;
delete
pa
;
delete
pa
;
/// B példányát alaposztály típusú pointeren keresztül érjük el.
/// Ezért csak az alaposztály destruktora hívódik, ha az nem
/// virtuális.
}
std
::
cout
<<
"
\n
Vege"
<<
std
::
endl
;
return
0
;
...
...
otthonMegMukodott/otthonMegMukodott.cbp
View file @
e01d2e5e
...
...
@@ -7,7 +7,7 @@
<Option
compiler=
"gcc"
/>
<Build>
<Target
title=
"Debug"
>
<Option
output=
"bin/Debug/
virt_destruktor
"
prefix_auto=
"1"
extension_auto=
"1"
/>
<Option
output=
"bin/Debug/
otthonMegMukodott
"
prefix_auto=
"1"
extension_auto=
"1"
/>
<Option
object_output=
"obj/Debug/"
/>
<Option
type=
"1"
/>
<Option
compiler=
"gcc"
/>
...
...
@@ -16,7 +16,7 @@
</Compiler>
</Target>
<Target
title=
"Release"
>
<Option
output=
"bin/Release/
virt_destruktor
"
prefix_auto=
"1"
extension_auto=
"1"
/>
<Option
output=
"bin/Release/
otthonMegMukodott
"
prefix_auto=
"1"
extension_auto=
"1"
/>
<Option
object_output=
"obj/Release/"
/>
<Option
type=
"1"
/>
<Option
compiler=
"gcc"
/>
...
...
virt_destruktor/Makefile
0 → 100644
View file @
e01d2e5e
# Makefile az othhonMegMukodott peldaprogram forditasara es futtatasara
# Solaris (ural2) es Linux ala.
#
# Hasznalat:
# make
# legeneralja mindket valtozatot
# make run
# futtatja mindket valtozatot
src
=
main.cpp
prog1
=
nemVirtDestr
prog2
=
virtDestr
targets
=
$(prog1)
$(prog2)
CXX
=
g++
# # a C++ fordito neve
CXXFLAGS
=
-pedantic
-Wall
# # C++ kapcsolok: legyen bobeszedu,
CXXFLAGS
+=
-g
# # ... es legyen debug info is
.PHONY
:
all
all
:
$(targets)
$(prog1)
:
$(src)
$(CXX)
-DVIRTUAL
=
$(src)
-o
$@
$(prog2)
:
$(src0)
$(CXX)
-DVIRTUAL
=
virtual
$(src)
-o
$@
.PHONY
:
run
run
:
$(targets)
@
echo
"Virtualis destruktorral"
./virtDestr
@
echo
@
echo
"Virtualis destruktor nelkul"
./nemVirtDestr
# takaritas igeny szerint
.PHONY
:
clean
clean
:
rm
-f
$(targets)
virt_destruktor/main.cpp
View file @
e01d2e5e
/*
* Demonstrációs program virtualis destruktor működésére.
* Az alaposztály destruktora a VIRTUAL makrótól függően
* virtuális, vagy nem.
* Fordítsa le a programot mindkét változatban és probálja ki.
* A nem virtulális változat memória szivárgást jelez.
*
* UNIX/LINUX környezetben a fordítást, és futtatást make segíti:
* Fordítás: make
* Két program készül: virtDestr és nemVirtDestr
* Futtatás: make run
*/
#ifndef MEMTRACE
# define MEMTRACE // ha nem definiált akkor definiáljuk
#endif // MEMTRACE
#include "memtrace.h"
#define virtual
/// A VIRTUAL makró vagy üres, vagy virtual kulcsszót tartalmaz
#ifndef VIRTUAL // ha nem definiált akkor üres lesz
# define VIRTUAL
#endif // VIRTUAL
struct
A
{
virtual
~
A
()
{};
VIRTUAL
~
A
()
{};
};
/// Amennyiben az alaposztály destruktora nem virtuális,
/// akkor nem fog meghívódni a származtatott osztály destruktora.
class
B
:
public
A
{
char
*
p
;
public
:
...
...
@@ -15,6 +35,8 @@ public:
int
main
()
{
A
*
pa
=
new
B
;
delete
pa
;
delete
pa
;
/// B példányát alaposztály típusú pointeren keresztül érjük el.
/// Ezért csak az alaposztály destruktora hívódik, ha az nem
/// virtuális.
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment