Openfoam基于C++分配内存的基础
提示:此处是对OpenFOAM内OpenFOAM-9/applications/test/alloc内文件的注释以及改动
前言
提示:为了学习OpenFOAM才对此处文件进行注释,方便未来的学习
例如:在OpenFOAM的学习中经常看到许多的new其实际是分配内存。
一、文件结构
test/alloc文件下的使用tree命令查看结构会发现1个directory和6个files。
分别是Make/files Make/options Test.C Test_alloc.C Test_malloc.C Test_new.C
提示:使用wmake编译器是为了更好管理头文件和库以及管理产生的执行文件。
Make文件夹中的是files和options文件。
- files写入的是要进行编译的C++文件,以及编译后产生的执行文件存放路径和名字。
- options写入的是在C++文件中使用的头文件和库的路径,因为OpenFOAM对C++的内容进行了扩展,形成了自己的头文件和库,所以在使用C++进行编写求解器的时候需要对文件路径进行写入。
- Test_alloc.C功能:使用new分配内存地址给指针
- Test_new.C功能:使用new分大量存储数组的内存地址给指针(为了看内存的地址,这里被我改写了)
- Test_malloc.C功能:使用malloc函数分配内存,注意与new的两大不同点。
- Test.C功能:不仅仅是对new的运用,也可以理解成为OpenFOAM中基础数据类型(表类型)list的定义方式。
提示:有些地方因为stearm头文件不存在的原因,但IOstearm存在,所以也就没有加路径,直接使用改换头文件解决。
二、Make文件夹
1.files
代码如下(示例):
#执行的文件名
#Test-alloc.C
#Test-new.C
#Test-malloc.C
Test.C
#执行命令的定义
#EXE = $(FOAM_USER_APPBIN)/Test-alloc
EXE = $(FOAM_USER_APPBIN)/Test
#EXE = $(FOAM_USER_APPBIN)/Test-new
#EXE = $(FOAM_USER_APPBIN)/Test-malloc
编译后的EXE执行文件存储在$(FOAM_USER_APPBIN)文件夹中,命名为Test。
$(FOAM_USER_APPBIN)=/home/UserName/OpenFOAM/UserName-9/platforms/linux64GccDPInt32Opt/bin
三、C++文件
1.Test_alloc.C
代码如下(示例):
//这头文件报错,提示不存在,使用iostream替换
//#include "stream.h"
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
/*(1)malloc 向系统申请分配指定size个字节的内存空间。返回类型是 void* 类型。
void* 表示未确定类型的指针。C,C++规定,void* 类型可以强制转换为任何其它类型的指针。
(2)从函数声明上可以看出。malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可
以自动计算所需要大小。*/
main()
{
int *ptrs[5];
std::cerr << "allocating ints\n";
for (int i=0; i<5; i++)
{
/*malloc 函数返回的是 void * 类型,如果你写成:p = malloc (sizeof(int)); 则程序无法通过编
译,报错:“不能将 void* 赋值给 int * 类型变量”。所以必须通过 (int *) 来将强制转换。*/
ptrs[i] = (int*)malloc(sizeof(int));
std::cerr<<sizeof(int)<<std::endl;
std::cerr<<sizeof(double)<<std::endl;
std::cout<<" "<<std::endl;
}
// for (;;);
/* cerr << "deallocating ints\n";
for (i=0; i<500000; i++)
{
free(ptrs[i]);
}
cerr << "allocating double\n";
double* array = (double*)malloc(500000*sizeof(double));
for (;;);*/
}
2.Test-new.C
代码如下(示例):
#include <iostream>
using namespace std;
main()
{
int i,j;
int* intPtrs[5];
cerr << "allocating ints\n";
for (int i=0; i<5; i++)
{
intPtrs[i] = new int[1];//分配出一个int型内存空间,将首地址给intptrs int型指针
cerr<< intPtrs[i]<<" ";
}
cerr << "allocated ints\n";
cerr << "deallocating ints\n";
for (int i=0; i<5; i++)
{
delete[] intPtrs[i];
}
cerr << "deallocated ints\n";
cerr << "allocating doubles\n";
double* doubles[5];
for (j=0;j<5;j++)
{
//分配大小分配大小为 sizeof(double) * 5;
doubles[j] = new double[5];//分配出5个double型内存空间,存放double的数组,数组中有5个元素,首地址给指针
cerr<< doubles[j]<<" ";
//可以输出动态内存里面有啥
//cerr<< *double[j]<<" ";
}
for (int i=0; i<5; i++)
{
delete[] doubles[i];
}
}
3.Test_malloc.C
代码如下:
//这头文件报错,提示不存在,使用iostream替换
//#include "stream.h"
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
/*(1)malloc 向系统申请分配指定size个字节的内存空间。返回类型是 void* 类型。
void* 表示未确定类型的指针。C,C++规定,void* 类型可以强制转换为任何其它类型的指针。
(2)从函数声明上可以看出。malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可
以自动计算所需要大小。*/
main()
{
int *ptrs[5];
std::cerr << "allocating ints\n";
for (int i=0; i<5; i++)
{
/*malloc 函数返回的是 void * 类型,如果你写成:p = malloc (sizeof(int)); 则程序无法通过编
译,报错:“不能将 void* 赋值给 int * 类型变量”。所以必须通过 (int *) 来将强制转换。*/
ptrs[i] = (int*)malloc(sizeof(int));
std::cerr<<sizeof(int)<<std::endl;
std::cerr<<sizeof(double)<<std::endl;
std::cout<<" "<<std::endl;
}
// for (;;);
/* cerr << "deallocating ints\n";
for (i=0; i<500000; i++)
{
free(ptrs[i]);
}
cerr << "allocating double\n";
double* array = (double*)malloc(500000*sizeof(double));
for (;;);*/
}
4.Test
代码如下:
/*stdlib 头文件即standard library标准库头文件。stdlib.h里面定义了五种类型、一些宏和通用工具函数。
类型例如size_t、wchar_t、div_t、ldiv_t和lldiv_t;
宏例如EXIT_FAILURE、EXIT_SUCCESS、RAND_MAX和MB_CUR_MAX等等;
常用的函数如malloc()、calloc()、realloc()、free()、system()、atoi()、
atol()、rand()、srand()、exit()等等。*/
#include <stdlib.h>
#include <iostream>
class Int //创建一个Int类
{
int I;
public:
Int(){}
operator int()
{
return I;
}
};
template<class T> //创建一个T类型,T类型等同于int、float、double等类型
class List : public T //创建了一个名为list的类(在OpenFoam中存在一个基本的列表类型,就是由此而来)
{
T* v;
int sz;
public:
List()
{
v = new T[sz=11];
}
List(int s) //C++特有性质,依据输入的参数进行函数的重载
{
v = new T[sz=s];
std::cout<<"v指针的地址="<<v<<std::endl;
std::cout<<"v地址内存储的值是:"<< *v<<std::endl;
}
~List() //释放指针
{
delete[] v;
}
inline int size() const; //C++特有性质,内联函数,可以在类外面在再定义函数的具体内容
};
template<class T> //对类中的size函数进行具体定义
inline int List<T>::size() const
{
return sz;
}
#include <iostream>
int main()
{
typedef List<Int> intList;
intList list(20); //在OpenFoam中可以直接定义列表: list<type> name(size)
std::cout << "listsize:" <<list.size() << "\n";
std::cerr <<"over" <<std::endl;
return 0;
}
四、编译
在终端中敲入命令如下:
//在alloc文件目录下
$wmake
//成功后,直接使用可以执行文件的名称就可运行
$Test