effective programming C笔记

1.内存

stack memory (local variables, function arguments/calls, return address, etc)

heap memory  (malloc)

<1>stack

一般情况下, stack 先使用高地址,再使用低地址

<2>heap

I promise to always free each chunk of memory that I allocate.

staticlly allocated

int array[10];
int array2[] = {1,2,3,4,5};
char str[] = "i promise to always free every chunk of memory that I allocate";

dynamically allocated

int *arr = malloc(20 * sizeof(int));
arr[5] = 5;
// ...
// ...
free( arr );

2. struct 和 typedef

typedef struct IntPair_s {
  int first;
  int second;
} IntPair;

IntPair pair;
pair.first = 1;
pair.second = 2;
IntPair *pairPtr = &pair;
// pairPtr->first pairPtr->second

猜你喜欢

转载自www.cnblogs.com/dynmi/p/12594219.html