原文地址http://pfacka.binaryparadise.com/articles/guide-to-advanced-programming-in-C.html
c语言是系统程序,嵌入式系统以及很多其他应用程序的一种选择。然而似乎对计算机不是特别感兴趣,才不会去接触c语言,熟悉c语言的各个方面,以及特别多的细节是一个巨大的挑战。本文试着提供较多的资料来阐述其中的一部分。包括:int类型的转换,内存分配,数组的指针转换,显式的内存函数,Interpositioning(不太理解) ,向量变化。
int main(int argc, char** argv) {
long i = -1;
if (i < sizeof(i)) {
printf("OK\n");
}
else {
printf("error\n");
}
return 0;
}#include <stdio.h>
#include <stdint.h>
#include <malloc.h>
#include <errno.h>
#define VECTOR_OK 0
#define VECTOR_NULL_ERROR 1
#define VECTOR_SIZE_ERROR 2
#define VECTOR_ALLOC_ERROR 3
struct vector {
int *data;
size_t size;
};
int create_vector(struct vector *vc, size_t num) {
if (vc == NULL) {
return VECTOR_NULL_ERROR;
}
vc->data = 0;
vc->size = 0;
/* check for integer and SIZE_MAX overflow */
if (num == 0 || SIZE_MAX / num < sizeof(int)) {
errno = ENOMEM;
return VECTOR_SIZE_ERROR;
}
vc->data = calloc(num, sizeof(int));
/* calloc faild */
if (vc->data == NULL) {
return VECTOR_ALLOC_ERROR;
}
vc->size = num * sizeof(int);
return VECTOR_OK;
}
int grow_vector(struct vector *vc) {
void *newptr = 0;
size_t newsize;
if (vc == NULL) {
return VECTOR_NULL_ERROR;
}
/* check for integer and SIZE_MAX overflow */
if (vc->size == 0 || SIZE_MAX / 2 < vc->size) {
errno = ENOMEM;
return VECTOR_SIZE_ERROR;
}
newsize = vc->size * 2;
newptr = realloc(vc->data, newsize);
/* realloc faild; vector stays intact size was not changed */
if (newptr == NULL) {
return VECTOR_ALLOC_ERROR;
}
/* upon success; update new address and size */
vc->data = newptr;
vc->size = newsize;
return VECTOR_OK;
}
char *ptr = NULL;
/* ... */
void nullfree(void **pptr) {
void *ptr = *pptr;
assert(ptr != NULL)
free(ptr);
*pptr = NULL;
} #include <stdlib.h>
#include <stdint.h>
#define MAX_REF_OBJ 100
#define RC_ERROR -1
struct mem_obj_t{
void *ptr;
uint16_t count;
};
static struct mem_obj_t references[MAX_REF_OBJ];
static uint16_t reference_count = 0;
/* create memory object and return handle */
uint16_t create(size_t size){
if (reference_count >= MAX_REF_OBJ)
return RC_ERROR;
if (size){
void *ptr = calloc(1, size);
if (ptr != NULL){
references[reference_count].ptr = ptr;
references[reference_count].count = 0;
return reference_count++;
}
}
return RC_ERROR;
}
/* get memory object and increment reference counter */
void* retain(uint16_t handle){
if(handle < reference_count && handle >= 0){
references[handle].count++;
return references[handle].ptr;
} else {
return NULL;
}
}
/* decrement reference counter */
void release(uint16_t handle){
printf("release\n");
if(handle < reference_count && handle >= 0){
struct mem_obj_t *object = &references[handle];
if (object->count <= 1){
printf("released\n");
free(object->ptr);
reference_count--;
} else {
printf("decremented\n");
object->count--;
}
}
}
void cleanup_release(void** pmem) {
int i;
for(i = 0; i < reference_count; i++) {
if(references[i].ptr == *pmem)
release(i);
}
}
void usage() {
int16_t ref = create(64);
void *mem = retain(ref);
__attribute__((cleanup(cleanup_release), mem));
/* ... */
}
* helper macros */
#define __COMB(X,Y) X##Y
#define COMB(X,Y) __COMB(X,Y)
#define __CLEANUP_RELEASE __attribute__((cleanup(cleanup_release)))
#define retain_auto(REF) retain(REF); int16_t __CLEANUP_RELEASE COMB(__ref,__LINE__) = REF
void cleanup_release(int16_t* phd) {
release(*phd);
}
void usage() {
int16_t ref = create(64);
void *mem = retain_auto(ref);
/* ... */
}
#include <stdlib.h>
#include <stdint.h>
struct pool_t{
void *ptr;
size_t size;
size_t used;
};
/* create memory pool*/
struct pool_t* create_pool(size_t size) {
struct pool_t* pool = calloc(1, sizeof(struct pool_t));
if(pool == NULL)
return NULL;
if (size) {
void *mem = calloc(1, size);
if (mem != NULL) {
pool->ptr = mem;
pool->size = size;
pool->used = 0;
return pool;
}
}
return NULL;
}
/* allocate memory from memory pool */
void* pool_alloc(struct pool_t* pool, size_t size) {
if(pool == NULL)
return NULL;
size_t avail_size = pool->size - pool->used;
if (size && size <= avail_size){
void *mem = pool->ptr + pool->used;
pool->used += size;
return mem;
}
return NULL;
}
/* release memory for whole pool */
void delete_pool(struct pool_t* pool) {
if (pool != NULL) {
free(pool->ptr);
free(pool);
}
}原文:http://blog.csdn.net/mlkiller/article/details/19363433