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 ...
随机推荐
- leetcode160
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...
- Django02-路由系统urls
一.路由配置系统(URLconf) 分为:静态路由动态路由 1.URL配置 URL配置(URLconf)就像Django所支撑网站的目录.它的本质是URL与该URL调用的视图函数之间的映射表 语法: ...
- 去BAT,你应该要看一看的面试经验总结(转)
来源微信公众号『easyserverdev』 http://mp.weixin.qq.com/s/UZljzFMjobo1wzCguY7NDg 说下我的面试经验吧,都是亲身经历,不喜勿喷: 我去年12 ...
- elasticsearch-权威指南笔记-基础部分
参考这里的文档es权威指南 话说这个坑爹的文档是2.x版本的es,英文版本也是,所以就没啥好抱怨的了. 官方教程中有很多坑 例如,需要启动text上的索引. 还有就是get这个是不能带json的,所以 ...
- 使用JavaMail发送邮件-从FTP读取图片并添加到邮件正文发送
业务分析: 最近工作需要,需要从FTP读取图片内容,添加到邮件正文发送.发送邮件正文,添加附件采用Spring的MimeMessageHelper对象来完成,添加图片也将采用MimeMessageHe ...
- JAVA企业级应用TOMCAT实战
1. Tomcat简介 原文链接:https://blog.oldboyedu.com/java-tomcat/ Tomcat是Apache软件基金会(Apache Software Foundati ...
- JS游戏控制时间代码
var canvas = new HGAME.canvas();var testBox=document.getElementById('boxRender');testBox.appendChild ...
- jenkins+sonarQube代码质量扫描 并排除指定的目录
sonar.projectKey=dev1-news-paymentsonar.projectName=dev1-news-paymentsonar.projectVersion=$BUILD_NUM ...
- xcode项目打不开:incompatible project version问题
低版本xcode打开高版本xcode项目或库工程的时候就会出现,打不开的问题 解决 1可以重建创建工程,将文件拷贝到新工程 2 相对一,较简单 找到.xcodeproj文件 右键 显示包内容,找到pr ...
- [leetcode]36. Valid Sudoku验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...