[C++]Doubly Linked List with Iterator

During the study week I tried to implement an integer list using Visual C++. My goal is to be as similar to as the list and iterator in the standard library. My goal is to simulate the implementation of the standard library list and iterator.

There are many people having issues with insertion and deletion of the iterator. In order to delete the boundaries of the list, the iterator need to have the access of the front and back of the list. According to the standard iterator, it does not have such a function:

A sample source main is:

#include "DoublyLinkedList.h"
#include
using namespace std;
int main(){
	DoublyLinkedList dll;
	dll.pushFront(12);
	dll.pushFront(11);
	dll.pushFront(10);
	dll.pushBack(13);
	dll.pushBack(14);
	dll.pushFront(9);
	while (!dll.isEmpty()){
		cout << "[DLL]Element:" << dll.getBack() << endl;
		dll.popBack();
	}
	int a[] = {55,66,77,88,99};
	DoublyLinkedList dll2(a, a + 5);
	for (DoublyLinkedList::iterator it = dll2.begin(); it != dll2.end(); ++it){
		cout << "[DLL2]Element:" << *it << endl;
	}
	return 0;
}

Output:

[DLL]Element:14
[DLL]Element:13
[DLL]Element:12
[DLL]Element:11
[DLL]Element:10
[DLL]Element:9
[DLL2]Element:55
[DLL2]Element:66
[DLL2]Element:77
[DLL2]Element:88
[DLL2]Element:99

Source Code(Please feel free to fork & contribute): https://github.com/Parthas-Menethil/DoubleLinkedList

[Master C++ Pointer #1]Retrieve private and protected stuffs in C++

[You may only do this practice on Linux..Coz windows behaves weirdly allocating memory for stack variables~ :( ]

 

This practice is made by Parthas Menethil(Luke.J.Sun). All rights reserved.

C++ is such a flexible language to have fun. You can break the common rule an do fantastic things. In this post, I am going to work with you to hack private information out from classes using only pointers!

I have three classes declared and instantiated one instance of each class in main. Some of the values inside the class are inaccessible by normal methods. We are going to pull them out from the memory.

NOTE: This program should be complied targeting 32bit operating system. The preferred compiler would be GCC 4.6.3

/*	How to access values in a class abnormally
 *
 * */
#include <iostream>
using namespace std; // Not a good habit, just for testing
class A{
private:
	int apples;
	double weight;
	unsigned int bugs;
public:
	A() : apples(100), weight(99.9989), bugs(123456){}
};
class B{
private:
	double exam_grade;
protected:
	double test_grade;
public:
	double quiz_grade;
	B(double exam, double test, double quiz){
		exam_grade = exam;
		test_grade = test;
		quiz_grade = quiz;
	}
	virtual void placeholder(){ /* Nothing to show */ }
};
struct Date{
private:
	int day;
	int month;
	int year;
public:
	Date(int d, int m, int y){
		day = d;
		month = m;
		year = y;
	}
};
class C : public B{
protected:
	Date *d;
public:
	C() : B(55, 50, 60.9){
		d = new Date(20, 9, 1993);
	}
	~C(){
		delete d;
	}
};
int main(){
	A a;
	B b(99,98,97);
	C c;
	cout<<"C++ is flexible!"<<endl<<endl;
	cout<<"Example done by Parthas Menethil(Luke.J.Sun)"<<endl<<endl;
	cout<<"Size of class A: "<<sizeof(a)<<endl;
	cout<<"->int(4) + double(8) + uint(4) = 16"<<endl;
	cout<<"Size of class B: "<<sizeof(b)<<endl;
	cout<<"->virtual table pointer(4) + double(8) * 3 = 28"<<endl<<endl;
	cout<<"Now let's retrieve everything from memory..."<<endl;
	/* Instance a */
	cout<<"In instance a:"<<endl;
	// Take stuffs from the beginning pointer
	cout<<"[Private]apples:"<<*(int*)(&a)<<endl;
	// Change the pointer to a char pointer so offset would moved by 1 per increasement, use sizeof to count the size of variable type
	cout<<"[Private]weight:"<<*(double*)((char*)(&a) + sizeof(int))<<endl;
	cout<<"[Private]bugs:"<<*(unsigned int*)((char*)(&a) + sizeof(int) + sizeof(double))<<endl<<endl;
	/* Instance b */
	cout<<"Ininstance b:"<<endl;
	// Pass the initial virtual table pointer
	cout<<"[Private]exam_grade:"<<*(double*)((char*)&b + 4)<<endl;
	// Move forward to pass the first double and retrieve the second one
	cout<<"[Protected]test_grade:"<<*(double*)((char*)&b + 4 + sizeof(double))<<endl;
	// Move forward to pass the second double and retrive the third one
	// The use of sizeof is not necessary because sometimes we know the variable type length
	// double has the length of 8
	cout<<"[Public]quiz_grade:"<<*(double*)((char*)&b + 4 + sizeof(double) + 8)<<endl<<endl;
	/* Let's try to retrieve the values in the Date object in C class */
	/* Instance c */
	cout<<"Instance c:"<<endl;
	cout<<"[Protected]d->day:"<<"[Your answer?]"<<endl;
	cout<<"[Protected]d->month:"<<"[Your answer?]"<<endl;
	cout<<"[Protected]d->year:"<<"[Your answer?]"<<endl;
	return 0;
}

At the beginning, I counted and explained the sizes as well as structures of class A and B, and then retrieved all values from A and B using only pointers.

To get the variables’ values from class C is tricky, you can try to do it by yourself! I believe that you can find the right answers out!

My solution will be released in the next post about this topic. Before that, try it out!

The right output should be:

C++ is flexible!

Example done by Parthas Menethil(Luke.J.Sun)

Size of class A: 16
->int(4) + double(8) + uint(4) = 16
Size of class B: 28
->virtual table pointer(4) + double(8) * 3 = 28

Now let’s retrive everything from memory…
In instance a:
[Private]apples:100
[Private]weight:99.9989
[Private]bugs:123456

Ininstance b:
[Private]exam_grade:99
[Protected]test_grade:98
[Public]quiz_grade:97

Instance c:
[Protected]d->day:20
[Protected]d->month:9
[Protected]d->year:1993

Try to match my output. There could be many ways to do the samething in c++.

[GoLang]A simple game – Rock Paper Scissors Lizard Spock

Last week, I just noticed that in this semester(2013 Fall), the Intro C Language(IPC144) course’s assignment 1 is a game called Rock Paper Scissors Lizard Spock. The first I encountered this game was when I was learning Python language on a online course. Unfortunately, I quit that course coz I was way too busy, but the game concept was embed into my mind.

Since it has been a long time that I have not actually  on my normal study pace due to the “DEATH” of my deeply loved 4-year-old laptop. Now thanks to my dear sister like friend, I got a temporary laptop and am able to be back on the track of studying Go Language.

Today I am going to make a “Rock Paper Scissors Lizard Spock” game using Go Language. I have seen many people use tons of if statements to judge the result of two players. The judgement part is indeed the tricky programming part of this game, and I am going to think of a clear way to implement it. Finally that part results into four lines of code. I cannot be 100 percent sure that it it right, but anyway hopefully there would be no bug.

My code would be available publicly on GitHub. Please feel free to fork and improve.

In addition, absolutely, I will finish the bonus part of that game which is Human versus Artificial Intelligence. It would be coming soon.

GitHub Repo(The folder is called RPSLS): https://github.com/Parthas-Menethil/GoLangStudy

Parthas.Menethil (Luke.J.Sun)

Basic Math

It took me so long time because that I was doing all of this on an IPAD!
This basic math program takes advantages of the object oriented programming and is built based on git source managing system and all commit histories are stored on GitHub.

GitHub-BasicMath

Although our dear professor H did not cover the arguments in C++ on the last lecture, we, who are good at self studying, worked out the exercise.

This project takes advantages of the following new stuffs covered in OOP344:
Preprocessor macro
GitHub
Arguments
Object Oriented Programming

Fortunately my iPad does compatible with all stuffs needed. XD

Source Code:
Coming Soon. Please check GitHub while it is unavailable.

How integers in VC++ 2012 are stored in memory on windows

This article is written by Luke(Parthas.Menethil.Sun). All rights reserved.

When we assign an integer to a variable, in fact, we store that number in some standard format into our memory somewhere. I have always been curious about how they are stored into our memory. Let’s check it out here.

Based on different languages, compilers, compiling configurations and systems, the answer may be various. My environment is using Visual Studio 2012 with the CL 17.00.51106.1 for x86 complier on Windows 7.

My testing code is simple:

#include
int main(){
	int i = 0x12ABCDEF;   // Assign a hexadecimal number
	printf(&quot;%x&quot;, &amp;i);     // Get the memory address of that variable
	return 0;
}

First I assign a number to fill all bytes of the integer and output its memory address.

While running, we can use hex editors like WinHex to monitor what’s going on in the memory.

We can see at the specific offset, the variable in each byte is stored in the reverse order.

How to change the password on you INT322 website

As you guys who have a zenit account, your website should be:https://zenit.senecac.on.ca/~username/

But when you visit that webpage there’s a prompt pops up asking  username and password and you cannot see the website without providing them.

How to make your own password without removing them?

The htpasswd can add a username for you:

 

htpasswd -b ~/.htpasswd username password

The command will generate a token string based on provided username and password and attach the token string to the .htpasswd file in your home directory.

NOTE: Do NOT overwrite or delete any previous things from that file or your professor cannot access your website.

FAQ for geeks:

1. I found that there are some other information in the .htpasswd file and they seem to be our professor’s username and password. Could we take advantage of that to see my classmates’ web pages?

Technically and ethically NO. Those passwords are generated by some hash arithmetic which is irreversible.

2. There are some online Apache password file generater. Should I use them?

NO. Not every site is using HTTPS protocol to protect your data during the transmission, your username and password can be sniffed by others. Even though they use the safe protocol, your username and password might be recorded by their website which creates potential threats for you.

This article is written by Luke(Parthas.Menethil.Sun). All rights reserved.

新年快乐, Happy 2013

倒计时舞台烟花

这么多年了,感觉这个新年过得最特别。

 

下午和两个姐姐约着去了市中心,参加了在市政厅前面举行的一个露天的跨年晚会。

这是我在演唱会有史以来站得离中央舞台最近的一次,只有差不多15米左右。

貌似去了很多明星……不过偶一个都不认识……现场唱的歌一个都不熟……气氛倒是很High的。感觉老外们平时的日子要多无聊有多无聊,这种时候比谁都HIGH,又蹦又跳又 晃的。

最后倒计时那短短几秒,舞台两边的烟花很绚丽。接踵而来的是更耀眼的烟花,足足有两分钟。

这里的公交系统很人性化,31号10点到1号凌晨4点公交系统大部分都延时运行并且全部免费。走的时候才发现,加拿大原来是可以有很多人的……地铁入口这个挤啊。路旁不时有陌生人互相尖叫,互相喊着happy (fuckin) new year。

市中心离家33.5公里……

这次我回家的时间晚到超记录了,凌晨3:25!

希望以后还可以有这么好的回忆!各位新年快乐!

困得一塌糊涂,写完日志……睡觉……

解决OPENVPN在Win7下安装的问题

安装OPENVPN的时候,有一步会给系统安装一个TAP驱动。但是在Windows7下,这个驱动因为没有签名从而无法正常使用。

在连接的时候会出现这样的错误:

Fri Nov 05 09:07:24 2004 us=747770 CreateFile failed on TAP device:
\.Global{A3B605BE-B118-49BA-92C3-C6ADFD7A364C}.tap: The system
cannot find the file specified. (errno=2)

解决方案就是:

  1. 如果已经安装OPENVPN,请完全卸载。
    If installed – uninstall the current copy of OpenVPN.
  2. 从这个地址下载OPENVPN安装程序
    Download the installer from one of the following links:
    http://openvpn.net/release/openvpn-2.1_rc19-install.exe
  3. 下载完成之后
    鼠标右键安装程序->属性->兼容性->以Windows Vista兼容模式运行这个程序
    Rt-Click Installer>>Properties>>Compatibility>>X Run this program in compatibility mode for: Windows Vista
    同时勾选以管理员身份运行此程序
    Privilege Level:
    X Run this program as an administrator
    确定
    Then Click Ok
  4. 运行安装程序完成安装
    Now run the installer

小贴士:不要忘记在OpenVPN GUI程序属性的兼容性选项卡中勾选[以管理员身份运行此程序]。

Tip: Set also under properties in the OpenVPN GUI “run this programm as an administrator”

我重装电脑必装的软件

电脑慢了不稳定了,最好的方法就是重新安装一下系统,软件自然也要重新 安装了。这里列出了我使用电脑必备的一些软件,不知道是否和你一样呢?

WinRAR

Daemon Tools

Chrome

搜狗拼音输入法

百度百科对于薛定谔之猫的解释真是糟糕透了

前几天偶然知道了这么一个物理问题 – 薛定谔之猫,听起来很是有趣。于是我首先使用百度百科来了解这是一个什么问题,但是看了几遍一头雾水 ~ 最后我想我终于理解了,感觉这个百科写得真是糟糕透了。

 

想必大家对“薛定谔之猫实验”还很陌生,我就简单介绍一下。

 

以爱因斯坦为首的科学家提出的量子力学阐释,微观粒子存在一种性质叫 Superposition ,即为空间中处于叠加态微观粒子的位置是不确定的,有可能在任何地方,但只有当你检测的时候,这个粒子才会出现在某个地方。这种事如果发生在宏观世界的日常生活中,就好比:我在家中何处是不确定的,你看我一眼,我就突然现身于某处——客厅、餐厅、厨房、书房或卧室都有可能;在你看我之前,我像云雾般隐身在家中,穿墙透壁到处游荡。

 

对于这种现象,薛定谔是想不通的,于是他提出了这样一个有趣的实验,即为“薛定谔之猫实验”:

把一只猫放进一个封闭的盒子里,然后把这个盒子连接到一个包含一个放射性原子核和一个装有有毒气体的容器的实验装置。这个放射性原子核在何时衰变是不确定的,如果发生衰变,它将会发射出一个粒子,而发射出的这个粒子将会触发这个实验装置,打开装有毒气的容器,从而杀死这只猫。根据量子力学,未进行观察时,这个原子核处于已衰变和未衰变的叠加态,但是,如果在一个小时后把盒子打开,实验者只能看到“衰变的原子核和死猫”或者“未衰变的原子核和活猫”两种情况。哥本哈根对量子力学的解释说,没有测量之前,一个粒子的状态模糊不清,处于各种可能性的混合叠加。如果是这样的话,在薛定谔之猫试验中,“检测器-锤子-毒药瓶”这条因果链,将铀原子的“衰变-未衰变叠加态”与猫的“死-活叠加态”联系在一起,使量子力学的微观不确定性变为宏观不确定性;微观的混沌变为宏观的荒谬——猫要么死了,要么活着,难道会是死活叠加状态么? ( 这两段部分内容参照借鉴了百度百科 )

 

 

在大家初步了解之后,我要说说百度百科在哪些地方对人有误导

首先百度百科将薛定谔之猫定义为”佯谬”。

佯谬是什么?我从未听过 ~ 还是根据百度百科的解释:“ 佯谬( yáng miù )佯谬就是指看上去是一个错误,但实际上是正确的。 ”

这里直接说明实际上是正确的,但是薛定谔之猫完全是一个谜一般的问题,实际上薛定谔本人最终并没有说这只猫应该是死还是活,还是死活叠加。至今没有令人信服的解释,而且根本没有确定结果,何谈正确与错误?

进一步谈,佯谬的英文解释是 Paradox ,但我更认可将这个词解释为悖论。如果用悖论来说,这就说得通了。悖论是指:“ 悖论指在逻辑上可以推导出互相矛盾之结论,但表面上又能自圆其说的命题或理论体系。 ”薛定谔猫实验存在逻辑矛盾,但是却可以发生 ( 这里并不是指用真猫,而是等效替代物,例如一组微观粒子 ) 。

一句话:说薛定谔之猫是正确的就犹如问三滴水哪个先落到地面是正确的一样令人无法理解,既然如此何来佯谬一说?

 

其次看完整个试验后,我回到上面的总体陈述,总体陈述说薛定谔之猫实验是为了解决爱因斯坦的祖母悖论-即平行宇宙之说,这使我更加疑惑。

首先我质疑这个和祖母悖论有什么关系?也许是我物理学得不好,找不到其中的联系,这个暂且不谈。

那么另一方面,薛定谔猫实验只是被提出的理想实验,而且并没有实际的结果 ( 即使想你也想不出 ~ 虽然说答案只是普普通通的三选一 ) ,怎么可以说这个实验可以解释祖母悖论?

我之后到 WIKI 百科草草看了一下英文的介绍,只提到了 EPR 悖论,这个与祖母悖论虽然不能说相差十万八千里,但是很大程度上来向不能混为一谈。

一句话:到现在我也不知道这个薛定谔之猫实验怎样解释祖母悖论。

 

其实,我认为 WIKI 百科对于这个的解释还是很客观严密的。首先定义薛定谔之猫实验被认为是 Paradox 时用到了 Usually 一词,可见用词谨慎。

之后说这个实验是薛定谔对于量子力学的一个讨论,他不完全相信量子力学,于是提出了这个实验问相信量子力学的人该如何解释。至今人们仍然在尝试去解释这个实验,目前来说还没有令众人信服的通用广泛的解释。

 

PS:WIKI 百科薛定谔之猫中文页面大家就不要参考了,我看第一句话把 thought experiment 翻译成了思想实验后就果断关掉了这个页面,作为一个大陆人,我感觉不靠谱。

 

作者:Parthas.Menethil.Sun

版权所有

转载请注明

保留一切权力

Copyright

All Rights Reserved