Computing desk | ||
---|---|---|
< May 30 | << Apr | May | Jun >> | June 1 > |
Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
#include <iostream>
#include <vector>
struct Tree {
bool is_leaf;
union {
int leaf;
std::vector<Tree> children;
};
Tree(): is_leaf(false), children() {/*std::cout << "default constructor called\n";*/}
Tree(const Tree& t) {/*std::cout << "copy constructor called\n";*/ *this = t;}
Tree operator= (const Tree& t) {
/*std::cout << "copy assignment called\n";*/
if (is_leaf = t.is_leaf) {leaf = t.leaf;}
else {children = t.children;}
/*std::cout << " copy assignment ends\n";*/
return *this;
}
~Tree() {
/*std::cout << "destructor called\n";*/
if (!is_leaf) {children.~vector();}
/*std::cout << " destructor ends\n";*/
}
};
int main() {
Tree t;
Tree u(t);
}
I am trying to implement a tree-like data structure in C++. Basically a
Tree
is either a data-containing leaf node or consists of one or more children subtree. I supposed a tagged union would best fit this situation. Because I used vector
— which has non-trivial special member functions — to store the list of children, I
had to provide the special member functions of Tree
myself (pre-C++11 doesn't even allow it). I don't know how to do this properly so I just go with what mekes sense in my mind. The above code, when ran, has a runtime error but I couldn't figure out how it happens. Here is the output when uncommented:
default constructor called copy constructor called copy assignment called destructor called destructor called destructor called
Then the error occurs. I have tried removing the if
line in the destructor but then it just prints
... destructor called destructor ends destructor called destructor ends ...
infinitely. Replacing *this = t
with "proper" construction doesn't help either.
I tried to run through a debugger but still don't know how and why the destructor was called. Currently I am in a state of utter confusion, don't know what part of C++ I'm missing or what to search now (I tried googling but no avail. And returning to cppreference just confuse me more). Please help. In case it matters, I'm using MinGW on Windows 10.
The original code is much longer and more complicated but this is a seperate minimum working example to ease debugging for me (and you). 14.177.103.250 ( talk) 08:32, 31 May 2018 (UTC)
int
to create leaf, but not shown. That default constructor just fits more with the rest of my code.
14.177.103.250 (
talk) 04:18, 1 June 2018 (UTC)TreeNode::isALeaf()
predicate with a simple inline test for children.empty()
??? Then no need to explicitly call consrtuctor and destructor, which may easily go wrong. --
CiaPan (
talk) 15:53, 31 May 2018 (UTC)
nullptr
) and create a vector if necessary with explicit call to new
: void TreeNode::AddAChild (TreeNode* node) {
if (childrenptr == nullptr)
childrenptr = new vector<TreeNode>;
childrenptr->push_back (node);
}
bool TreeNode::IsALeaf() { return childrenptr == nullptr || childrenptr->empty(); }
TreeNode::~TreeNode() {
delete childrenptr;
}
if (is_leaf = t.is_leaf)
is_leaf
property of the source object.t
to *this
you must take four cases into account:
*this
and t
are leaves → you just copy the internal data;*this
and t
are internal nodes → you use the vector's copy constructor;*this
is a leaf, but t
has children → you must 'manually' construct the destination vector before copying children;*this
has children, but t
is a leaf → you need to destroy the children vector before copying the union's leaf
member.Tree operator= (const Tree& t) {
std::cout << "copy assignment called\n";
if (is_leaf) {
if (t.is_leaf) {
leaf = t.leaf;
} else {
children = std::vector<Tree>(t.children.size());
for (int i = 0; i < t.children.size(); ++i) {
childreni = t.childreni];
}
}
} else {
if (t.is_leaf) {
children.~vector();
leaf = t.leaf;
} else {
children = t.children;
}
}
is_leaf = t.is_leaf;
std::cout << " copy assignment ends\n";
return *this;
}
14.177.103.250 ( talk) 04:38, 2 June 2018 (UTC)
If I'm correct RAM between the years '95 to '99, for a home computer, was usually something from 32MB to 128MB . However, back then, people already had browsers, PDFs, Word and Excel. How could they run with so little RAM? What's going wrong with today's computers? From the point of view of the user experience, how were things like for the users of the '90s compared to our present day? Are my 4GB RAM idle most of the time, when I'm just navigating the web, writing a text or doing similar stuff? -- Doroletho ( talk) 16:46, 31 May 2018 (UTC)
The minimum size that I could write for a DOS program in the 1980s was about 9KB.– Bloatware! Here's the source code for a text-only terminal program that I wrote in 1991:
Extended content
|
---|
; World's shortest comms program! ; Use MODE to set the comm port parameters then syntax is: T comm_port ; Alt-X quits ; Requires DSR and CTS from the port .radix 16 quitkey equ 2D00 ;Alt-X code segment assume cs:code,ds:code,es:code,ss:code org 0100 start: mov al,ds:[82] ;get comm port sub al,"1" cbw push ax rx: pop dx push dx mov ah,3 ;check port for data int 14 rcr ah,1 jnc tx ;if nothing, check keyboard mov ah,2 ;read comm port pop dx push dx int 14 mov dl,al ;write to STDOUT mov ah,2 int 21 tx: mov ah,1 ;check keyboard int 16 jz rx ;if nothing check comm port again mov ah,0 ;get key in AL int 16 cmp ax,quitkey ;check for quit key pop dx je quit mov ah,1 ;send to comm port push dx int 14 jmp rx ;check comm port again quit: ret code ends end start |
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
... PC DOS 7 ...– The program did work (within its stated limitations) on MS-DOS 3.3x. It's possible that it won't work correctly under later versions. The reference book that I used (Dettmann and Johnson's DOS Programmer's Reference, 3rd edition), which covers up to DOS 5, says of
int 21
, function 2, "Microsoft recommends that you longer use this function ... Support for this function may end at any time." (I used it because it requires fewer opcodes than the recommended replacement function 40h. I wasn't writing with a view to long term support either
I see a push ax without a corresponding pop.–
push ax
is followed immediately by pop dx
, which transfers the port number from AX
to DX
, for use by int 14
. Given that the pop
is already used to restore DX
after int 21
, push
is shorter than mov dx,ax
.It should be noted that this little program does not set any serial port parameters,– Hence the comment at the top about using MODE to do the set up. The way to get the "smallest" program was to do the bare minimum - as much as possible was left to the operating system, including initial setup with existing DOS commands. The program was written for size, not speed or ease of use, which is why it uses BIOS (
int 14
) and DOS (
int 21
) calls (and
polling rather than
interrupts) to do everything.I'm running Windows 7. I've just downloaded Python for Windows and the setup program put the executable in a directory called
C:\Users\TheUser\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.6\
When I try to look for that directory with either Explorer or the Command Prompt shell, it doesn't appear in the listing either Explorer or the shell "dir" command shows.
For example the directory C:\Users\TheUser\AppData\ doesn't appear at all (let alone its subdirectories).
Why is that? And how can I force Windows to display a true listing of the contents of directories instead of it hiding some directories and files?
Thanks. Basemetal 21:05, 31 May 2018 (UTC)
dir /AH
shows only hidden files; dir /A
shows all files (and directories), whatever their attributes.
AndrewWTaylor (
talk) 08:51, 1 June 2018 (UTC)Many thanks to all three. Basemetal 13:03, 1 June 2018 (UTC)
I'm running Windows 7. I'm trying to get source code of a program residing in this directory and I can see it is in the form of a tar archive (extension .tar.gz). Next to it there is a very small file with extension .tar.gz.sig. Can anyone tell me what it does? Do I need to download it also in order to unpack the archive? And one other question on tar: Is tar only used to compress source code or could there be executables in that tar archive? I'm looking for source code only. Thanks. Basemetal 21:17, 31 May 2018 (UTC)
How can I write a bash script that does this?
command & command & command & sleep 3600 kill everything spawned above
That is, let each of the spawned commands run (mostly sleeping) for an hour, then stop. — Tamfang ( talk) 21:41, 31 May 2018 (UTC)
kill %1 kill %2 kill %3
Computing desk | ||
---|---|---|
< May 30 | << Apr | May | Jun >> | June 1 > |
Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
#include <iostream>
#include <vector>
struct Tree {
bool is_leaf;
union {
int leaf;
std::vector<Tree> children;
};
Tree(): is_leaf(false), children() {/*std::cout << "default constructor called\n";*/}
Tree(const Tree& t) {/*std::cout << "copy constructor called\n";*/ *this = t;}
Tree operator= (const Tree& t) {
/*std::cout << "copy assignment called\n";*/
if (is_leaf = t.is_leaf) {leaf = t.leaf;}
else {children = t.children;}
/*std::cout << " copy assignment ends\n";*/
return *this;
}
~Tree() {
/*std::cout << "destructor called\n";*/
if (!is_leaf) {children.~vector();}
/*std::cout << " destructor ends\n";*/
}
};
int main() {
Tree t;
Tree u(t);
}
I am trying to implement a tree-like data structure in C++. Basically a
Tree
is either a data-containing leaf node or consists of one or more children subtree. I supposed a tagged union would best fit this situation. Because I used vector
— which has non-trivial special member functions — to store the list of children, I
had to provide the special member functions of Tree
myself (pre-C++11 doesn't even allow it). I don't know how to do this properly so I just go with what mekes sense in my mind. The above code, when ran, has a runtime error but I couldn't figure out how it happens. Here is the output when uncommented:
default constructor called copy constructor called copy assignment called destructor called destructor called destructor called
Then the error occurs. I have tried removing the if
line in the destructor but then it just prints
... destructor called destructor ends destructor called destructor ends ...
infinitely. Replacing *this = t
with "proper" construction doesn't help either.
I tried to run through a debugger but still don't know how and why the destructor was called. Currently I am in a state of utter confusion, don't know what part of C++ I'm missing or what to search now (I tried googling but no avail. And returning to cppreference just confuse me more). Please help. In case it matters, I'm using MinGW on Windows 10.
The original code is much longer and more complicated but this is a seperate minimum working example to ease debugging for me (and you). 14.177.103.250 ( talk) 08:32, 31 May 2018 (UTC)
int
to create leaf, but not shown. That default constructor just fits more with the rest of my code.
14.177.103.250 (
talk) 04:18, 1 June 2018 (UTC)TreeNode::isALeaf()
predicate with a simple inline test for children.empty()
??? Then no need to explicitly call consrtuctor and destructor, which may easily go wrong. --
CiaPan (
talk) 15:53, 31 May 2018 (UTC)
nullptr
) and create a vector if necessary with explicit call to new
: void TreeNode::AddAChild (TreeNode* node) {
if (childrenptr == nullptr)
childrenptr = new vector<TreeNode>;
childrenptr->push_back (node);
}
bool TreeNode::IsALeaf() { return childrenptr == nullptr || childrenptr->empty(); }
TreeNode::~TreeNode() {
delete childrenptr;
}
if (is_leaf = t.is_leaf)
is_leaf
property of the source object.t
to *this
you must take four cases into account:
*this
and t
are leaves → you just copy the internal data;*this
and t
are internal nodes → you use the vector's copy constructor;*this
is a leaf, but t
has children → you must 'manually' construct the destination vector before copying children;*this
has children, but t
is a leaf → you need to destroy the children vector before copying the union's leaf
member.Tree operator= (const Tree& t) {
std::cout << "copy assignment called\n";
if (is_leaf) {
if (t.is_leaf) {
leaf = t.leaf;
} else {
children = std::vector<Tree>(t.children.size());
for (int i = 0; i < t.children.size(); ++i) {
childreni = t.childreni];
}
}
} else {
if (t.is_leaf) {
children.~vector();
leaf = t.leaf;
} else {
children = t.children;
}
}
is_leaf = t.is_leaf;
std::cout << " copy assignment ends\n";
return *this;
}
14.177.103.250 ( talk) 04:38, 2 June 2018 (UTC)
If I'm correct RAM between the years '95 to '99, for a home computer, was usually something from 32MB to 128MB . However, back then, people already had browsers, PDFs, Word and Excel. How could they run with so little RAM? What's going wrong with today's computers? From the point of view of the user experience, how were things like for the users of the '90s compared to our present day? Are my 4GB RAM idle most of the time, when I'm just navigating the web, writing a text or doing similar stuff? -- Doroletho ( talk) 16:46, 31 May 2018 (UTC)
The minimum size that I could write for a DOS program in the 1980s was about 9KB.– Bloatware! Here's the source code for a text-only terminal program that I wrote in 1991:
Extended content
|
---|
; World's shortest comms program! ; Use MODE to set the comm port parameters then syntax is: T comm_port ; Alt-X quits ; Requires DSR and CTS from the port .radix 16 quitkey equ 2D00 ;Alt-X code segment assume cs:code,ds:code,es:code,ss:code org 0100 start: mov al,ds:[82] ;get comm port sub al,"1" cbw push ax rx: pop dx push dx mov ah,3 ;check port for data int 14 rcr ah,1 jnc tx ;if nothing, check keyboard mov ah,2 ;read comm port pop dx push dx int 14 mov dl,al ;write to STDOUT mov ah,2 int 21 tx: mov ah,1 ;check keyboard int 16 jz rx ;if nothing check comm port again mov ah,0 ;get key in AL int 16 cmp ax,quitkey ;check for quit key pop dx je quit mov ah,1 ;send to comm port push dx int 14 jmp rx ;check comm port again quit: ret code ends end start |
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
... PC DOS 7 ...– The program did work (within its stated limitations) on MS-DOS 3.3x. It's possible that it won't work correctly under later versions. The reference book that I used (Dettmann and Johnson's DOS Programmer's Reference, 3rd edition), which covers up to DOS 5, says of
int 21
, function 2, "Microsoft recommends that you longer use this function ... Support for this function may end at any time." (I used it because it requires fewer opcodes than the recommended replacement function 40h. I wasn't writing with a view to long term support either
I see a push ax without a corresponding pop.–
push ax
is followed immediately by pop dx
, which transfers the port number from AX
to DX
, for use by int 14
. Given that the pop
is already used to restore DX
after int 21
, push
is shorter than mov dx,ax
.It should be noted that this little program does not set any serial port parameters,– Hence the comment at the top about using MODE to do the set up. The way to get the "smallest" program was to do the bare minimum - as much as possible was left to the operating system, including initial setup with existing DOS commands. The program was written for size, not speed or ease of use, which is why it uses BIOS (
int 14
) and DOS (
int 21
) calls (and
polling rather than
interrupts) to do everything.I'm running Windows 7. I've just downloaded Python for Windows and the setup program put the executable in a directory called
C:\Users\TheUser\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.6\
When I try to look for that directory with either Explorer or the Command Prompt shell, it doesn't appear in the listing either Explorer or the shell "dir" command shows.
For example the directory C:\Users\TheUser\AppData\ doesn't appear at all (let alone its subdirectories).
Why is that? And how can I force Windows to display a true listing of the contents of directories instead of it hiding some directories and files?
Thanks. Basemetal 21:05, 31 May 2018 (UTC)
dir /AH
shows only hidden files; dir /A
shows all files (and directories), whatever their attributes.
AndrewWTaylor (
talk) 08:51, 1 June 2018 (UTC)Many thanks to all three. Basemetal 13:03, 1 June 2018 (UTC)
I'm running Windows 7. I'm trying to get source code of a program residing in this directory and I can see it is in the form of a tar archive (extension .tar.gz). Next to it there is a very small file with extension .tar.gz.sig. Can anyone tell me what it does? Do I need to download it also in order to unpack the archive? And one other question on tar: Is tar only used to compress source code or could there be executables in that tar archive? I'm looking for source code only. Thanks. Basemetal 21:17, 31 May 2018 (UTC)
How can I write a bash script that does this?
command & command & command & sleep 3600 kill everything spawned above
That is, let each of the spawned commands run (mostly sleeping) for an hour, then stop. — Tamfang ( talk) 21:41, 31 May 2018 (UTC)
kill %1 kill %2 kill %3