
C/C++
文章平均质量分 64
wowRicky
其实我是代码滴搬用工;
C4 Picasso, my favorite car.
展开
-
写一个内存越界访问的程序
内存越界 address sanitizer原创 2024-07-12 14:57:21 · 940 阅读 · 0 评论 -
一个不完全编译导致的奇怪问题
分析到这里,你应该明白问题所在了吧,就是a.h修改后,a.c没有重新编译生成a.o, a.out可执行文件link的仍然是初始代码的a.o.gdb 调试,发现a_t结构体的大小再main.c 和 a.c中的size 不一致,一个是12bytes, 一个是8bytes.编译运行,生成a.o, a.out, 运行a.out 输出: 1-2。输入如下:1-2-0,是不是感觉很奇怪, 我们期望的是1-2-3。删除a.o, 重新编译,问题解决。原创 2024-07-12 17:58:57 · 211 阅读 · 0 评论 -
ELF Analysis
#include <stdio.h> #include <stdlib.h>int g_uninit; // .bssint g_init = 0x12; // .datastatic int g_sta_var = 0x34; // .dataconst int c = 0x34; // .rodatachar *s = "zeku"; /* s: .data ".原创 2021-08-02 16:00:58 · 240 阅读 · 0 评论 -
怎么编写段错误(Segmentation fault)的程序
On Unix-like operating systems, a process that accesses invalid memory receives the SIGSEGV signal. On Microsoft Windows, a process that accesses invalid memory receives the STATUS_ACCESS_VIOLATION exception.1.访问0地址#include <stdio.h>int mai.原创 2021-07-31 09:47:26 · 716 阅读 · 0 评论 -
C运算符优先级笔记
1.指针数组int *p[5];[] 大于 *2. 强制类型() 与 成员选择(./->)#include <stdlib.h>typedef struct{ int data; int time;} data_t;int main(){ data_t *p = (data_t *)malloc(sizeof(data_t)); int t = (data_t *)p->time; /*focus: -> ...原创 2021-07-27 12:09:23 · 789 阅读 · 0 评论 -
C 变量的作用域
下面的程序输出什么?思考一下#include <stdio.h> // Driver Codeint main(){ { int x = 10, y = 20; { // The outer block contains // declaration of x and // y, so following statement // is valid原创 2021-07-21 18:06:02 · 214 阅读 · 0 评论 -
堆分析:heap profiler tool: massif
How to use:1. valgrind --tool=massif progprog运行完后,输出:massif.out.<pid>//<pid>:process ID2. ms_print massif.out.12345Ms_print将生成显示(prog)程序执行期间内存消耗的图表,详细说明有关方案中各点的负责分配地点的信息,包括峰值点内存分配。...原创 2021-07-15 17:47:18 · 605 阅读 · 0 评论 -
C面向对象之透明指针的运用
不透明指针(opaque pointer)可以用来在C中实现封装。什么是不透明指针(opaque pointer)从字面意思来看,“不透明”意味着看不到内部,因此“不透明指针”即看不到内部定义的指针。这样说有些抽象,我们来看个例子:#include <stdio.h>typedef void *opque_data;typedef struct AAA *opque_stru;int main(){ opque_data data = 0; opque_s原创 2021-07-12 09:41:15 · 949 阅读 · 0 评论 -
malloc(0)-malloc 0 字节
C17中有如下描述:7.22.3 Memory management functions1 The order and contiguity of storage allocated by successive calls to the aligned_alloc, calloc,malloc, and realloc functions is unspecified. The pointer returned if the allocation succeeds issuitably align原创 2021-07-08 14:44:09 · 504 阅读 · 0 评论 -
二维指针操作链表
二维指针操作链表#include <stdio.h>#include <stdlib.h>typedef struct Node { struct Node *next; int data;} node;void initList(node **head, unsigned int size){ node **cur = head; for (int i=0; i<size; i++) { *cur = (node *)malloc(sizeo原创 2021-06-27 17:34:16 · 508 阅读 · 0 评论 -
连续地址数据(数组或者malloc的内存)作为函数参数
If you want to pass a single-dimension array as an argument in a function, you would have to declare a formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer原创 2021-06-27 17:33:06 · 425 阅读 · 0 评论 -
双重指针作为函数参数的妙用
指针加加#include <stdio.h>#include <stdlib.h>#include <string.h>void setDataCond(int *p, unsigned int condi){ if (condi % 2) { *p = condi * 10; p++; }}void setDataCond_1(int **pp, unsigned int condi){ if (condi % 2) { *原创 2021-06-27 11:40:54 · 2213 阅读 · 0 评论 -
c++私有继承有什么用
Private InheritanceC++ has a second means of implementing the has-a relationship: private inheritance.Withprivate inheritance, public and protected members of the base class become private members原创 2014-04-20 22:20:34 · 3861 阅读 · 1 评论 -
虚函数与虚继承寻踪
封装、继承、多态是面向对象语言的三大特性,熟悉C++的人对此应该不会有太多异议。C语言提供的struct,顶多算得上对数据的简单封装,而C++的引入把struct“升级”为class,使得面向对象的概念更加强大。继承机制解决了对象复用的问题,然而多重继承又会产生成员冲突的问题,虚继承在我看来更像是一种“不得已”的解决方案。多态让对象具有了运行时特性,并且它是软件设计复用的本质,虚函数的出现为多态性转载 2014-04-18 13:18:01 · 601 阅读 · 0 评论 -
C++派生类与基类构造函数调用次序
本文用来测试C++基类和派生类构造函数,析构函数,和拷贝构造函数的调用次序。运行环境:SUSE Linux Enterprise Server 11 SP2 (x86_64) #include using namespace std;class Base{public: Base() { cout }原创 2014-01-13 12:40:12 · 1544 阅读 · 0 评论 -
字符串面试题(一)字符串逆序
字符串逆序可以说是最经常考的题目。这是一道入门级的题目,相信80%的程序员经历过这道题。给定一个字符串s,将s中的字符顺序颠倒过来,比如s="abcd",逆序后变成s="dcba"。逆序基本上没有这么考的,放在这里主要是为了和后面的原地逆序做个对比。很简单,直接分配一个与原字符串等长的字符数组,然后反向拷贝一下即可。char* Reverse(char* s){转载 2013-12-18 16:34:23 · 808 阅读 · 0 评论 -
When should static_cast, dynamic_cast and reinterpret_cast be used?
这是我偶然在 http://stackoverflow.com/questions/ 网页上发现的一个问题(类似博客园的博问),问题主要是关于询问应该怎样使用,以及何时使用C++里面的这几种类型转换操作符:static_case, dynamic_cast,以及 reinterpret_cast 。我想这是一个非常典型的问题,因此我就想把这篇帖子转载到我的博客上,也是我第一篇转载的文章。转载 2013-06-27 10:55:48 · 930 阅读 · 0 评论