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 ...
随机推荐
- 为何要使用ViewModel
ViewModel类是用来存储和管理与UI相关的数据,在设计之初就考虑到生命周期的影响.ViewModel允许数据在屏幕旋转等配置变化后存活. Android framework管理UI控制器(如Ac ...
- leetcode338
public class Solution { public int[] CountBits(int num) { ]; ; i <= num; i++) { ; var cur = i; do ...
- django毕设之路1.0
Django的核心理念 1.更python化 2.DRY:(don't repeat yourself),不做重复的工作 3.松耦合和灵活 4.快速开发 2.Django的MTV概 M:Model模型 ...
- Codeforces Round #437 E. Buy Low Sell High
题意:买卖股票,给你n个数,你可以选择买进或者卖出或者什么都不做,问你最后获得的最大收益是多少. Examples Input 910 5 4 7 9 12 6 2 10 Output 20 Inpu ...
- Shiro Remember me设置
1. 在Spring的相关配置文件中加入如下Remember me管理器配置: <!-- rememberMe管理器 --> <bean id="rememberMeMan ...
- linux下redis4.0.2集群部署(利用Ruby脚本命令)
一.原生命令方式和Ruby脚本方式区别 利用Ruby脚本部署和用原生命令部署,节点准备的步骤都是一样的,节点启动后的握手,以及主从.槽分配,利用Ruby脚本一步就能完成,利用原生命令需要一步一步地执行 ...
- 突然发现用PHP做多条件模糊查询很简单
原文:http://blog.csdn.net/suleil1/article/details/49471099 所使用的方法:$sqlArr=array();array_push();implode ...
- python的魔术方法
什么叫魔术方法: 在python中定义以双下划线开头,有一些python自定义的函数,并且以双下划线为结尾的函数叫做魔法函数 class Company(object): def __init__(s ...
- 自定义View(四) ViewGroup 动态添加变长Tag标签 支持自动换行
欲实现如下效果: 思路很简单就2步: 1.测量出ViewGroup的大小 2.找出子View的位置 若要实现动态添加标签view,就要实现ViewGroup的onMeasure().onLayout( ...
- 【PHP面试题】通俗易懂的两个面试必问的排序算法讲解:冒泡排序和快速排序
又到了金三银四找工作的时间,相信很多开发者都在找工作或者准备着找工作了.一般应对面试,我们无可厚非的去刷下面试题.对于PHPer来说,除了要熟悉自己所做的项目,还有懂的基本的算法.下面来分享下PHP面 ...