呆's Blog
  • Home
Subscribe
Tagged

c language

A collection of 3 posts

c language

C 語言 Pointer 操作 - 3

幾篇先前幫新玩 C 語言的一個朋友整理的 pointer 操作說明 例 1 #define nelement(a) (sizeof(a) / sizeof(a[0])) static void hello() { printf("hello\n"); } static void world() { printf("world\n"); } typedef void (*callback)(); int main() { callback cbs[] = { hello, world }; int i; for(i = 0; i < nelement(cbs); i

  • Derek 呆
Derek 呆 May 7, 2020 • 1 min read
c language

C 語言 Pointer 操作 - 2

幾篇先前幫新玩 C 語言的一個朋友整理的 pointer 操作說明 例 1 #include <stdio.h> #include <stdint.h> int main() { uint32_t v1 = 0x11223344; uint8_t *p = (uint8_t *) &v1; printf("%x, %x, %x, %x\n", *p, *(p + 1), p[2], p[3]); } 執行結果 (視平台的 endianess 不同結果會不一樣) $ gcc

  • Derek 呆
Derek 呆 May 7, 2020 • 8 min read
c language

C 語言 Pointer 操作

幾篇先前幫新玩 C 語言的一個朋友整理的 pointer 操作說明 例 1 int v = 0; int *p = &v; C 的資源管理通常會是滿需要花心思的, 這部份的管理策略不一致或不清楚時, 非常容易發生問題 (double free, memory leak, stack/heap corruption, dangling pointer...). 首先要清楚 pointer 指向的內存位置, 是在 stack 或是 heap 上. 此例 p 指向 v. 這裡 v 及 p 都在 stack 上 兩者都不需要手動釋放. 兩者的 life cycle

  • Derek 呆
Derek 呆 Sep 5, 2019 • 15 min read
呆's Blog © 2021
Powered by Ghost