Programming Specification
1. Define variable return_code to record the function's status.
int return_code = 0;
2. Define the exit flag: exit_flag, which is used by goto. This flag is defined at the end of function body. The content of exit_flag includes free memory and return the status of function.
exit_flag:
if(m_a)
free(m_a);
if(m_b)
free(m_b);
if(m_c)
free(m_c);
if(m_d)
free(m_d); return return_code;
3. Check the array formal parameters of function.
//check
if (NULL == a) {
return_code = -1;
goto exit_flag;
}
if (NULL == b) {
return_code = -1;
goto exit_flag;
}
4. Allocate memory
// allocate memory
m_a = (float *) malloc(n * sizeof(float));
if(!m_a){
printf("Failed to allocate memory! \n");
return_code = -1;
goto exit_flag;
}
Example:
#define IN
#define OUT int solve_tridiagonal_equation_thomas(
IN int n,
IN float a[], IN float b[], IN float c[],
IN float d[],
OUT float x[]
)
{
int return_code = 0; //check
if (NULL == a) {
return_code = -1;
goto exit_flag;
}
if (NULL == b) {
return_code = -1;
goto exit_flag;
}
if (NULL == c) {
return_code = -1;
goto exit_flag;
}
if (NULL == d) {
return_code = -1;
goto exit_flag;
}
if (NULL == x) {
return_code = -1;
goto exit_flag;
} int i = 0;
float tmp = 0;
float *m_a, *m_b, *m_c, *m_d; // allocate memory
m_a = (float *) malloc(n * sizeof(float));
if(!m_a){
printf("Failed to allocate memory! \n");
return_code = -1;
goto exit_flag;
}
m_b = (float *) malloc(n * sizeof(float));
if(!m_b){
printf("Failed to allocate memory! \n");
return_code = -1;
goto exit_flag;
}
m_c = (float *) malloc(n * sizeof(float));
if(!m_c){
printf("Failed to allocate memory! \n");
return_code = -1;
goto exit_flag;
}
m_d = (float *) malloc(n * sizeof(float));
if(!m_d){
printf("Failed to allocate memory! \n");
return_code = -1;
goto exit_flag;
} // diagonal dominant validation and copy data
bool cond1 = (abs(b[0]) > abs(c[0])) && (abs(c[0]) > 0);
bool cond2 = (abs(b[n-1]) > abs(a[n-1])) && (abs(a[n-1]) > 0); if(!(cond1 && cond2))
{
printf("Matrix is Invalid! \n");
return_code = -2;
goto exit_flag;
} for(i = 1; i < n-1; ++i)
{
if(abs(b[i]) < abs(c[i]) + abs(c[i]))
{
printf("Matrix is NOT diagonal dominant! \n");
return_code = -2;
goto exit_flag;
}
else{
m_a[i] = a[i];
m_b[i] = b[i];
m_c[i] = c[i];
m_d[i] = d[i];
}
}
memcpy(m_a, a, n * sizeof(float)); // forward elimination
for(i = 1; i < n; ++i)
{
tmp = m_a[i] / m_b[i-1];
m_b[i] = m_b[i] - tmp * m_c[i-1];
m_d[i] = m_d[i] - tmp * m_d[i-1];
} // backward substitution
x[n-1] = m_d[n-1] / m_b[n-1];
for(i = n-2; i >= 0; --i)
{
x[i] = (m_d[i] - m_c[i] * x[i+1]) / m_b[i];
} // free memory and exit
exit_flag:
if(m_a)
free(m_a);
if(m_b)
free(m_b);
if(m_c)
free(m_c);
if(m_d)
free(m_d); return return_code;
}
Programming Specification的更多相关文章
- Smashing The Browser:From Vulnerability Discovery To Exploit学习记录
浏览器Fuzz技术 漏洞挖掘 白盒挖掘 代码审计 自动化代码分析 黑盒挖掘 Fuzzing 两种Fuzzing技术 静态Fuzzing 基于变异的 文件.文档 多媒体 bf3 基于生成的 浏览器 重点 ...
- USB ISP(ICSP) Open Programmer < PWM ADC HV PID >
http://sourceforge.net/projects/openprogrammer/?source=navbar Open Programmer http://openprog.alterv ...
- 10 The Go Programming Language Specification go语言规范 重点
The Go Programming Language Specification go语言规范 Version of May 9, 2018 Introduction 介绍 Notation 符号 ...
- HDU 4223 Dynamic Programming?(最小连续子序列和的绝对值O(NlogN))
传送门 Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solvi ...
- Aspect Oriented Programming using Interceptors within Castle Windsor and ABP Framework AOP
http://www.codeproject.com/Articles/1080517/Aspect-Oriented-Programming-using-Interceptors-wit Downl ...
- The P4 Language Specification v1.0.2 Header and Fields
前言 本文参考P4.org网站给出的<The P4 Language Specification v1.0.2>的第二部分首部及字段,仅供学习:). 欢迎交流! Header and Fi ...
- hdu 4223 Dynamic Programming?
Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- Object Oriented Programming python
Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...
- Important Programming Concepts (Even on Embedded Systems) Part V: State Machines
Earlier articles in this series: Part I: Idempotence Part II: Immutability Part III: Volatility Part ...
随机推荐
- toString() 数组转字符串
var monthNames = ['Jan', 'Feb', 'Mar', 'Apr']; var myVar = monthNames.toString(); // assigns "J ...
- 25.Hibernate-配置文件.md
目录 1.主配置文件 1.1定义 1.1.1分类 1.1.2分类 1.1.3不使用配置文件生成表 1.2教程 2. 映射配置文件 1.主配置文件 1.1定义 1.1.1分类 在hibernate的配置 ...
- 深度学习中,使用regularization正则化(weight_decay)的好处,loss=nan
刚开始训练一个模型,自己就直接用了,而且感觉训练的数据量也挺大的,因此就没有使用正则化, 可能用的少的原因,我也就不用了,后面,训练到一定程度,accuracy不上升,loss不下降,老是出现loss ...
- SSM商城项目(八)
1. 学习计划 1.solr集群搭建 2.使用solrj管理solr集群 3.把搜索功能切换到集群版 4.添加商品同步到索引库 2. 什么是SolrCloud SolrCloud(solr 云 ...
- EOS keosd
[EOS keosd] The program keosd, located in the eos/build/programs/keosd folder within the EOSIO/eos r ...
- Linux 下监控用户最大进程数参数(nproc)是否到达上限的步骤:
https://www.cnblogs.com/autopenguin/p/6184886.html 1.查看各系统用户的进程(LWP)数: 注意:默认情况下采用 ps 命令并不能显示出所有的进程.因 ...
- [Java核心技术]第四章-对象与类(4.1-4.6总结)
4.1面向对象程序设计概述 OOP(面向对象编程Object Oriented Programming) OOP中数据第一位,算法第二位. 类 封装:关键在于不能让其他方法直接访问类的实例域,程序仅通 ...
- 776. Split BST 按大小拆分二叉树
[抄题]: Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree int ...
- python的基本用法(三)字符串常用函数
字符串常用函数 # s='.abcd.'# new_s=s.strip('.')#默认去掉字符串两边的空格和换行符,想去掉什么括号中就写什么# print('s',s)# print('new_s', ...
- Python开发——函数【基础】
函数的定义 以下规则 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号(). 任何传入参数和自变量必须放在圆括号中间.圆括号之间可以用于定义参数. 函数的第一行语句可以选择性地使用文档字符 ...