- 接下来我们将比对C++(vector)代码与把该代码编译出的程序反编译后的ida伪代码。
- 下面每一个标题下的第一个代码片为C++代码,第二个为ida对应伪代码。
一、vector声明的C++实现与ida伪代码
#include <iostream>
#include <vector>
using namespace std;
1.声明一个向量
//声明一个int型向量
vector<int> test1;
getchar();
//创建一个vector
std::vector<int,std::allocator<int>>::vector(&v16, argv, envp);
getchar();
2.声明一个有初始大小的向量
//声明一个初始大小为5的int向量
vector<int> test2(5);
getchar();
//创建初始容量大小为5的vector
std::allocator<int>::allocator(&v17);
std::vector<int,std::allocator<int>>::vector(&v15, 5LL, &v17);
std::allocator<int>::~allocator(&v17);
getchar();
3.声明一个有初始大小和初始值的向量
//声明一个初始大小为10且值都是1的向量
vector<int> test3(10,1);
getchar();
//创建初始容量大小为10并且将元素初始化为1的vector
std::allocator<int>::allocator(&v18);
v19 = 1;
std::vector<int,std::allocator<int>>::vector(&v14, 10LL, &v19, &v18);
std::allocator<int>::~allocator(&v18);
getchar();
4.用已初始化的变量初始化一个新的向量
//声明并用num向量初始化test4向量
int num = 1;
vector<int> test4(num);
getchar();
//声明并用num向量初始化test4向量
v23 = 1;
std::allocator<int>::allocator(&v20);
std::vector<int,std::allocator<int>>::vector(&v13, v23, &v20);
std::allocator<int>::~allocator(&v20);
getchar();
5.用已知向量的部分值初始化新向量
//用向量test3的第0个到第9个值初始化test5
vector<int> test5(test3.begin(), test3.end());
getchar();
std::allocator<int>::allocator(&v21);
//这两句在c++中相当于v14.begin()以及v14.end()
v3 = std::vector<int,std::allocator<int>>::end(&v14);
v4 = std::vector<int,std::allocator<int>>::begin(&v14);
//这句看着很长,其实也就是构造函数,将::提取出来就可以看出std::vector<...>::vector<...>(...)
std::vector<int,std::allocator<int>>::vector<__gnu_cxx::__normal_iterator<int *,std::vector<int,std::allocator<int>>>,void>(
&v12,
v4,
v3,
&v21);
std::allocator<int>::~allocator(&v21);
getchar();
6.用数组来初始化向量
//将array[1]~array[4]范围内的元素作为test6的初始值
int array[5] = {1, 2, 3, 4, 5};
vector<int> test6(&array[1], &array[4]);
getchar();
v7 = 1;
v8 = 2;
v9 = 3;
v10 = 4;
v11 = 5;
std::allocator<int>::allocator(&v22);
//将array[1]~array[4]范围内的元素作为向量v22的初始值,v8为数组下标为1的元素,v11为最后一个
std::vector<int,std::allocator<int>>::vector<int *,void>(&v6, &v8, &v11, &v22);
std::allocator<int>::~allocator(&v22);
getchar();
———————————————————我是分割线—————————————————————————————
二、vector函数的C++实现与ida伪代码
1.进行数值压入的操作
//创建一个vector,并且将5个数值压入容器
vector<int> test;
for(int i=0; i<5; i++)
test.push_back(i);
getchar();
//用"一、"的知识,这里创建了一个vector
std::vector<int,std::allocator<int>>::vector(&v10, argv, envp);
for ( i = 0; i <= 4; ++i )
//这里相当于v10.push_back(i)
std::vector<int,std::allocator<int>>::push_back(&v10, &i);
getchar();
2.输出容器大小
//输出容器大小
cout << test.size();
getchar();
//求v10的大小,相当于v3 = v10.size();
v3 = std::vector<int,std::allocator<int>>::size(&v10);
//相当于cout<<v3;
std::ostream::operator<<(&std::cout, v3);
getchar();
3.删除容器的元素
//判断容器是否为空,若非空,删除一个元素
if(!test.empty())
test.pop_back();
getchar();
if ( (unsigned __int8)std::vector<int,std::allocator<int>>::empty(&v10) ^ 1 )
std::vector<int,std::allocator<int>>::pop_back(&v10);
getchar();
4.重设容器大小并赋值
//重新设置容器大小并赋值
test.resize(5, 2);
getchar();
//这个resize有没有发觉很像“一、”的构造函数,第一个参数为一个char变量的地址,第二个为容器初始大小,第三个为初始数据的地址
//相当于v10.resize(5,2);
v11 = 2;
std::vector<int,std::allocator<int>>::resize(&v10, 5LL, &v11);
getchar();
5.两个容器比较是否相等
//创建一个新容器,并判断新容器跟旧容器是否相等
vector<int> test1(5,2);
if(test1 == test)
cout << "Right!" << endl;
getchar();
std::allocator<int>::allocator(&v12);
v13 = 2;
std::vector<int,std::allocator<int>>::vector(&v9, 5LL, &v13, &v12);
std::allocator<int>::~allocator(&v12);
//这里判断两个容器是否相等,相当于v9 == v10
if ( (unsigned __int8)std::operator==<int,std::allocator<int>>(&v9, &v10) )
{
v4 = std::operator<<<std::char_traits<char>>(&std::cout, "Right!");
std::ostream::operator<<(v4, &std::endl<char,std::char_traits<char>>);
}
getchar();
6.给容器赋值
//将区间[first,last)的元素赋值到当前的test1容器中,或者赋n个值为x的元素到vector容器中,会覆盖以前的内容
test1.assign(test.begin(), test.end());
getchar();
//将下面句子简化可以看出他就是v9.assign(v6,v5);
//其实就是v9.assign(v10.begin(), v10.end());
//具体怎么简化的话,你就看::,不要看模板,那只是类型的问题
std::vector<int,std::allocator<int>>::assign<__gnu_cxx::__normal_iterator<int *,std::vector<int,std::allocator<int>>>,void>(
&v9,
v6,
v5);
getchar();
7.清空容器的值
test.clear();
getchar();
//清空容器元素,相当于v10.clear();
std::vector<int,std::allocator<int>>::clear(&v10);
getchar();
———————————————————我是分割线—————————————————————————————
三、IDA伪代码的最后
在ida伪代码最后势必会有一个调用析构函数释放空间的过程。
//析构函数
std::vector<int,std::allocator<int>>::~vector(&v9);
std::vector<int,std::allocator<int>>::~vector(&v10);
———————————————————我是分割线—————————————————————————————
【相关:vector容器】