【合集】【更新中】C++ pointer 之指针大乱指

Problem 1: 

What's the result of the program:

class base {
public:
  ~base() {
    printf("base disconcrete\n");
  }
};
class child:public base {
public:
  ~child() {
    printf("child disconcrete\n");
  }
};
int main() {
  base* b = new child();
  delete b;
  return 0;
}

Take notice that base* points to derive, so :

Only 

"base disconcrete" is printed.

But if 

  child* c = new child();
  delete c;

Problem 2:

char* getMem(void) {
  char p[] = "hello world";
  p[5] = 0x0;
  return p;
}
void test(void) {
  char *s = 0x0;
  s = getMem();
  printf(s);
}

int main() {
  test();
  return 0;
}

The result is Not Sure

Problem 3:  Quite Dangerous

void f(char **p) {
  *p += 2;
}

int main() {
  char *a[] = {"123","abc","456"}, **r = a;
  f(r);
  printf("%s\r\n",*r);
  return 0;
}

The result is 3

But if 

void f(char **p) {
  *p += 2;
}
void g(char **p) {
  p += 2;
}

int main() {
  char *a[] = {"123","abc","456"}, **r = a;
  //f(r);
  g(r);
  printf("%s\r\n",*r);
  return 0;
}

The result is still 123

Only 

printf("%s\r\n",*(r+2)); output 456

Problem 4:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class str{  
  2. public:  
  3.   int x;  
  4.   char s[0];  
  5. };  
  6.   
  7. class foo{  
  8. public:  
  9.   str * p;  
  10. };  
  11. int main() {  
  12.   foo f = {0};  
  13.   str* p1 = NULL;  
  14.   if (f.p->s){  
  15.     printf("%d\n", f.p->s);  
  16.   }  
  17.   return 0;  
  18. }  

This sentence sets p to NULL:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. foo f = {0};  

f.p is a pointer of str, so f.p is an offset. I.e., f.p->s = base address + 4

So the result is 4

Problem 5: Dangerous:

#include <stdio.h>
#include <assert.h>

int main()
{
    const int i = 10; 
    int* p = (int*)&i;
    *p += 10; 
    printf("%d\t%d\n", i, *p);
    printf("%x\t%x\n", &i, p); 
}

The value of i is 10, *p = 20. The address of i and p is same. 



But for struct. 

#include <iostream>
#include <assert.h>
using namespace std;

struct mbLJ{
    int mb; 
    int LJ; 
    mbLJ(){
        mb=0;
        LJ=1;
    }   
};



int main()
{
    const struct mbLJ hehe;
    int* p = (int*)(&hehe.mb);
    *p += 10; 
    printf("%d\t%d\n", hehe.mb, *p);
}
输出 10 10

对全局变量取地址不会出错,对全局变量的const修改直接程序崩溃:

const int j = 10;
int main()
{
    const int i = 10; 
    int* p = (int*)&i;
    int* q = (int*)&j;
    
    *q += 10;
    //*p += 10; 
    printf("%d\t%d\n", i, *p);
    printf("%x\t%x\n", &i, p);

    //printf("%d\t%d\n", j, *q);
    //printf("%x\t%x\n", &j, q);
    
    return 0;

}


#include <iostream>
#include <assert.h>
using namespace std;

struct mbLJ{
    static int mb ; 
    int LJ; 
    mbLJ(){
        mb=0;
        LJ=1;
    }   
};
int mbLJ::mb  = 0;//没有这句编译不会过,因为静态成员变量只是声明,没有实现定义。。。

int main()
{
    const struct mbLJ hehe;
    
    int* p = (int*)(&hehe.mb);
    *p += 10; 
    printf("%d\t%d\n", hehe.mb, *p);
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值