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 ...
随机推荐
- javaee_正则表达式基础和常用表达式
正则基础: 1.1 -字符集 [ ] : 代表单个字符. [^] : 除了该字符外的所有单个字符. [a-zA-Z] : [a-z] || [A-Z]. [a-d[m-p]] :[a, d] || [ ...
- Java中的包装数据类型
基本类型 包装器类型 boolean Boolean char Character int Integer byte Byte short Short long Long float Float do ...
- EOS踩坑记
[EOS踩坑记] 1.每个account只能更新自己的contract,即使两个account的秘钥相同,也不允许. 如下,使用alice的权限来更新james的contract.会返回 Missin ...
- [leetcode]64. Minimum Path Sum最小路径和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 获取CNVD的cookie
def getCookie(self): #获取cookie spiderFirefox = webdriver.Firefox() spiderFirefox.get("http://ww ...
- PHP并发之Swoole
<?php /** * Created by PhpStorm. * User: zhezhao * Date: 2016/10/20 * Time: 10:51 */ $url_arr = a ...
- java ssh执行shell脚本
1.添加依赖 com.jcraft:jsch ch.ethz.ganymed:ganymed-ssh2:262 2.获取连接 conn = new Connection(ip, port); conn ...
- Android通过手机搭建服务器,WIFI建立热点实现C/S聊天室通信功能
应用效果图: 客户端 ...
- KD-树(上)
来自于https://zhuanlan.zhihu.com/p/23966698 思路篇 导语:kd 树是一种二叉树数据结构,可以用来进行高效的 kNN 计算.kd 树算法偏于复杂,本篇将先介绍以二叉 ...
- qsort例子
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> typ ...