在linux下,沒用到的記憶體空間會被拿來當作buffer/cache使用,
以加速I/O存取
所以會感覺記憶體占用很多,
其實實際上如果程式需要記憶體,
就會釋放buffer/cache的空間,所以通常記憶體幾乎都是保持在很高的使用量。
使用free指令,
"-/+ buffers/cache"那一行的free就是實際上系統剩下來沒被其他程式吃掉的記憶體大小
可以用以下的指令取得真實的剩餘空間
echo -n free memory:;free -m | grep buffers/cache | awk '{print $4}'
2009年7月7日 星期二
2009年7月6日 星期一
C陣列與指標
宣告陣列時,
無論是幾維的陣列,
C語言都以分配一塊連續的記憶體空間來處理。
所以這種時候,可以把它當一維陣列處理
C以row為主,假設是二維陣列 arr[2][2]
當一維時的順序就是:
arr[0][0] arr[0][1] arr[1][0] arr[1][1]
雖然指標和陣列用起來差不多,
不過實際上意義是不太一樣的,
可以參考
http://dascan.pixnet.net/blog/post/15600458
http://squall.cs.ntou.edu.tw/cprog/Materials/AdvancedArray.html
要宣告一個指標代表陣列,要用以下的方法:
int data[5][5][5][5];
int (*p)[5][5][5];
可以參考
http://blog.udn.com/cchahacaptain/2197712
無論是幾維的陣列,
C語言都以分配一塊連續的記憶體空間來處理。
所以這種時候,可以把它當一維陣列處理
C以row為主,假設是二維陣列 arr[2][2]
當一維時的順序就是:
arr[0][0] arr[0][1] arr[1][0] arr[1][1]
雖然指標和陣列用起來差不多,
不過實際上意義是不太一樣的,
可以參考
http://dascan.pixnet.net/blog/post/15600458
http://squall.cs.ntou.edu.tw/cprog/Materials/AdvancedArray.html
要宣告一個指標代表陣列,要用以下的方法:
int data[5][5][5][5];
int (*p)[5][5][5];
可以參考
http://blog.udn.com/cchahacaptain/2197712
標籤:
C programming
2009年7月2日 星期四
混合C與C++的程式
參考這個網站的:
http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html在c++的程式內,使用C++標準函式庫,需要標明namespace:
#include <>; //cstdioint main(){...std::printf(...);...}
在c++的程式內,使用C標準函式庫,用法跟c一樣:
#include <> //stdio.hint main(){...printf(...);...}
在c++的程式內,使用C函式庫:
extern "C" {#include "" //my-header.h}int main(){...my_func(...);...}
------------------------------//my-header.h#ifdef __cplusplusextern "C" {#endifmy-function(...);#ifdef __cplusplus}#endif------------------------------//main.cpp#include "" //my-header.hint main(){...my_func(...);...}------------------------------
若不想要include my-header.h,只要使用某幾個c function,可以直接宣告完整的prototype
//main.cpp
extern "C" my_func(int a, int b, int c);
or
//main.cppextern "C" {my_func1(int a, int b, int c);my_func2(int a, int b, int c);my_func3(int a, int b, int c);}
用到時直接用
//main.cpp
my_func1(...);
若要讓c程式call到c的某個function, 使用extern "C"可以使complier將此function給linker的資訊使用c的格式
//my.cppextern "C" my_cpp_func(int a, int b);my_cpp_func(int a, int b){...}
標籤:
C programming
2009年6月28日 星期日
取得浮點數(float)的最大值
其他資料型態應該也可以這樣做...
因為不同平台的資料型態的位元數可能不太一樣,用這個方法比較保險
因為不同平台的資料型態的位元數可能不太一樣,用這個方法比較保險
- include float.h
- use FLT_MAX
標籤:
C programming
2009年6月27日 星期六
2009年6月25日 星期四
C陣列長度取得(用malloc的方法)
當使用動態記憶體配置時
無法使用sizeof的技巧來取得陣列長度
不過可以寫一個自己的malloc和free來計算並維護各個動態分配的長度!!
無法使用sizeof的技巧來取得陣列長度
不過可以寫一個自己的malloc和free來計算並維護各個動態分配的長度!!
- 寫mymalloc在malloc時記錄下這塊記憶體的陣列元素個數
- 寫myfree在free時把記錄下來的對應刪掉
#include
#include
#include
typedef struct arr_sz_t{
void * pointer;
int length;
struct arr_sz_t *next;
}arr_sz;
static arr_sz *sz_mapping;
void* mymalloc(int length, int size);
void myfree(void *p);
void add_sz_entry(void* p, int length);
int arr_length(void* p);
//input 1.array length 2.unit size
void* mymalloc(int length, int size){
void* ret = malloc(length*size);
add_sz_entry(ret, length);
return ret;
}
//free with mapping table
void myfree(void *p){
free(p);
if(sz_mapping != NULL){
arr_sz * t;
t = sz_mapping;
if(t->pointer == p){
sz_mapping = t->next;
free(t);
return;
}
while(t->next != NULL){
arr_sz *next;
next = t->next;
if(next->pointer == p){
t->next = next->next;
free(next);
return;
}
t = t->next;
}
}
}
void add_sz_entry(void* p, int length){
arr_sz* t = (arr_sz *)malloc(sizeof(arr_sz));
t->next = NULL;
t->pointer = p;
t->length = length;
//no sz_mapping
if(sz_mapping == NULL){
sz_mapping = t;
}
//insert
else{
t->next = sz_mapping;
sz_mapping = t;
}
}
//if no mach in mapping table, size is 0
int arr_length(void* p){
//no mapping data
if(sz_mapping == NULL){
return 0;
}
arr_sz* t;
t = sz_mapping;
while(t != NULL){
if(t->pointer == p){
return t->length;
}
t = t->next;
}
return 0;
}
int main(void){
srand(time(0));
int *data[50];
int i;
for(i=0;i<50;i++){
data[i] = (int *)mymalloc(random()%50, sizeof(int));
printf("size of data[%d]: %d\n", i, arr_length(data[i]));
myfree(data[i]);
}
return 0;
}
2009年5月19日 星期二
C的typedef
typedef的功用就是把某些東西變成一個預設的型態(像是int、double、char等型態)來使用
這些東西可以是enum、struct...
char name[20];
int height;
int weight;
};
struct people a;
char name[20];
int height;
int weight;
}People;
People a;
enum week today = Sun;
Week today = Sun
這些東西可以是enum、struct...
- 原本:
char name[20];
int height;
int weight;
};
struct people a;
- 可以寫成
char name[20];
int height;
int weight;
}People;
People a;
- 原本
enum week today = Sun;
- 可以寫成
Week today = Sun
標籤:
C programming
訂閱:
文章 (Atom)
