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 ...
随机推荐
- TCC细读 - 2 核心实现
TCC,基于业务层面的事物定义,粒度完全由业务自己控制,本质上还是补偿的思路,它把事物运行过程分为try-confirm-cancel阶段,每个阶段逻辑由业务代码控制 业务活动管理器控制业务活动的一致 ...
- ubuntu16下用QT5实现对话框应用
ubuntu16下用QT5,实现对话框程序,步骤:生成界面Dialog.ui,将它应用到主程序,通过主程序显示. 一 界面练习 1 Dialog.ui界面生成 在命令行输入:designer 进入界面 ...
- 《Android Studio开发实战 从零基础到App上线》资源下载和内容勘误
转载于:https://blog.csdn.net/aqi00/article/details/73065392 资源下载 下面是<Android Studio开发实战 从零基础到App上线&g ...
- javascript隐式原型
上图是js原型关系图. javascript是一种基于对象的编程语言,但它与一般面向对象的编程语言不同,因为它没有class类的概念 什么是原型?? 我们每创建一个函数,它就会自带一个原型函数,这个原 ...
- ArcGIS自定义工具箱-修复损坏的工作空间
ArcGIS自定义工具箱-修复损坏的工作空间 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:替换数据源的工作空间 用途:针对损坏的数据源,批量进行修复 案例数 ...
- tensorflow 升级后报错:ImportError: libcudnn.so.6: cannot open shared object file: No such file or directory
我的tensorflow之前的版本是1.2的所以支持cudnn5,但是tensorflow1.3及以上就是支持cudnn6. 查看: /usr/local/cuda/lib64$ ls libcud ...
- day42 字段的增删改查详细操作
复习 # 1.表的详细操作 create table nt like ot; # 只复制表的结构包括约束 create table nt select * from ot where 1=2; # 复 ...
- 20175314 实验一 Java开发环境的熟悉
20175314 实验一 Java开发环境的熟悉 一.实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用IDEA 编辑.编译.运行.调试Java程序. 3.完成实验,撰写实验报告,注意实 ...
- python + Jquery,抓取西东网上的Java教程资源网址
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-06-15 14:01:45 # @Author : Chenjun (320 ...
- short s1 = 1; s1 = s1 + 1;和 short s1 = 1; s1 += 1;的问题,终于弄懂了
对于short s1 = 1; s1 = s1 + 1; 由于s1+1运算时会自动提升表达式的类型,所以结果是int型,再赋值给short类型s1时,编译器将报告需要强制转换类型的错误. 对于shor ...