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;
}
时间: 2024-11-07 20:09:15

Programming Specification的相关文章

(译)Cg Programming/Unity(Cg编程/Unity)

最近在学习Unity3d中的shader编程,能找到的中文资料比较少,于是,尝试翻译一下wiki Books上的资料,以方便其他跟我一样的入门学习者.由于是第一次翻译技术资料,经验不足,难免出错,请路过的大神们批评指正,共同帮助我等新手少走弯路,谢谢. 下面翻译开始: (原文:https://en.wikibooks.org/wiki/Cg_Programming/Unity) Cg programming in the game engine Unity is considerably eas

Java Language Programming Design (One)

Chapter One.              Introduction to JAVA (1)Preliminary Knowledge a)Java Language Specification b)Java API c)Java Edition: Java SE,Java EE,Java ME d)Java Environment:  JDK,JRE,JVM e)Java Development Tools:eclipse,MyEclipse,NetBeans... Note:You

Object Oriented Programming python

new concepts of the object oriented programming : class encapsulation inheritance polymorphism the three features of an object are : identity, state and behaviora class is an abstraction which regroup objects who have the same attributes and the same

hdu 4223 Dynamic Programming?

Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solving complex problems by breaking them down

Java Programming Tutorial Java Native Interface (JNI)

1.  Introduction At times, it is necessary to use native codes (C/C++) to overcome the memory management and performance constraints in Java. Java supports native codes via the Java Native Interface (JNI). JNI is difficult, as it involves two languag

SWAGGER SPECIFICATION

OpenAPI Specification (fka Swagger RESTful API Documentation Specification) Version 2.0 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "

Aspect Oriented Programming using Interceptors within Castle Windsor and ABP Framework AOP

http://www.codeproject.com/Articles/1080517/Aspect-Oriented-Programming-using-Interceptors-wit Download sample application (or see the latest on Github) Contents Introduction What is Aspect Oriented Programming (AOP) and Method Interception? Manual W

JVMS Specification(2)-Compiling for the Java Virtual Machine

Subsections 2       Compiling for the Java Virtual Machine 2.1        Format of Examples 2.2        Use of Constants, Local Variables, and Control Constructs 2.3        Arithmetic 2.4        Accessing the Runtime Constant Pool 2.5        More Control

Apache Avro? 1.7.6 Specification

Apache Avro? 1.7.6 Specification Introduction Schema Declaration Primitive Types Complex Types Records Enums Arrays Maps Unions Fixed Names Aliases Data Serialization Encodings Binary Encoding Primitive Types Complex Types JSON Encoding Sort Order Ob