注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

ksnowlv的博客

积土成丘,集腋成裘,坚持再坚持!

 
 
 

日志

 
 

C/C++_多级指针&多维数组__new&delete  

2013-12-09 19:08:37|  分类: C/C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
      背景:最近几个月面试一些C/C++岗位面试者,关于指针方面我考查的问题之一,特意写下来,分享给有需要的人。限于时间关系,不深入研究,感兴趣的,可以好好研究下。  
    多维数组如何与多级指针相互转换呢?两者本质上都是代表地址,指针和数组可以看作是指向的一块数据的地址。当然,你可以说数组本身就拥有一块数据地址空间,比如说定长数组。和”指针和数组可以看作是指向的一块数据的地址“并不矛盾。
   转换的方式可能在不同的编译器下有不同的表现,下述CODE在xcode,vs下理论上都是OK的,但是,如果是直接把数组强制转换后同级指针,再访问,在xcode下直接崩溃,在VS下是OK的。注意区别,可能是xcode下对内存保护比较严格吧。

- (void)testMultiPointerMultiArray

{

    const NSInteger KArrayLength = 4;

    NSInteger array[KArrayLength][KArrayLength][KArrayLength];

    NSInteger count = 0;

    

    for (NSInteger i = 0; i < KArrayLength; ++i) {

        

        for (NSInteger j = 0; j < KArrayLength; ++j) {

            

            for (NSInteger k = 0; k < KArrayLength; ++k) {

                array[i][j][k] = count++;

                NSLog(@"array[%d][%d][%d]=%d",i,j,k,array[i][j][k]);

            }

        }

    }

    

    

    NSInteger*** p = new NSInteger**[KArrayLength];

    

    for (NSInteger i = 0; i < KArrayLength; ++i) {

        p[i] = new NSInteger *[KArrayLength];

        

        for (NSInteger j = 0; j < KArrayLength; ++j) {

            p[i][j] = new NSInteger[KArrayLength];

            

            for (NSInteger k = 0; k < KArrayLength; ++k) {

                *(*(*(p +i) +j) + k) = array[i][j][k];

                NSLog(@"p[%d][%d][%d]=%d",i,j,k,*(*(*(p +i) +j) + k));

            }

        }

    }

    

    for (NSInteger i = 0; i < KArrayLength; ++i) {

       

        for (NSInteger j = 0; j < KArrayLength; ++j) {

            delete [] p[i][j];

        }

        

        delete [] p[i];

    }

    

    delete []  p;

    

}


输出日志为:

2013-12-09 18:35:38.979 multiPointerMultiArrayTest[13092:70b] array[0][0][0]=0

2013-12-09 18:35:38.981 multiPointerMultiArrayTest[13092:70b] array[0][0][1]=1

2013-12-09 18:35:38.981 multiPointerMultiArrayTest[13092:70b] array[0][0][2]=2

2013-12-09 18:35:38.982 multiPointerMultiArrayTest[13092:70b] array[0][0][3]=3

2013-12-09 18:35:38.982 multiPointerMultiArrayTest[13092:70b] array[0][1][0]=4

2013-12-09 18:35:38.982 multiPointerMultiArrayTest[13092:70b] array[0][1][1]=5

2013-12-09 18:35:38.983 multiPointerMultiArrayTest[13092:70b] array[0][1][2]=6

2013-12-09 18:35:38.983 multiPointerMultiArrayTest[13092:70b] array[0][1][3]=7

2013-12-09 18:35:38.983 multiPointerMultiArrayTest[13092:70b] array[0][2][0]=8

2013-12-09 18:35:38.984 multiPointerMultiArrayTest[13092:70b] array[0][2][1]=9

2013-12-09 18:35:38.984 multiPointerMultiArrayTest[13092:70b] array[0][2][2]=10

2013-12-09 18:35:38.984 multiPointerMultiArrayTest[13092:70b] array[0][2][3]=11

2013-12-09 18:35:38.984 multiPointerMultiArrayTest[13092:70b] array[0][3][0]=12

2013-12-09 18:35:38.985 multiPointerMultiArrayTest[13092:70b] array[0][3][1]=13

2013-12-09 18:35:38.985 multiPointerMultiArrayTest[13092:70b] array[0][3][2]=14

2013-12-09 18:35:38.986 multiPointerMultiArrayTest[13092:70b] array[0][3][3]=15

2013-12-09 18:35:38.986 multiPointerMultiArrayTest[13092:70b] array[1][0][0]=16

2013-12-09 18:35:38.986 multiPointerMultiArrayTest[13092:70b] array[1][0][1]=17

2013-12-09 18:35:38.987 multiPointerMultiArrayTest[13092:70b] array[1][0][2]=18

2013-12-09 18:35:38.987 multiPointerMultiArrayTest[13092:70b] array[1][0][3]=19

2013-12-09 18:35:38.987 multiPointerMultiArrayTest[13092:70b] array[1][1][0]=20

2013-12-09 18:35:38.988 multiPointerMultiArrayTest[13092:70b] array[1][1][1]=21

2013-12-09 18:35:38.988 multiPointerMultiArrayTest[13092:70b] array[1][1][2]=22

2013-12-09 18:35:38.988 multiPointerMultiArrayTest[13092:70b] array[1][1][3]=23

2013-12-09 18:35:38.989 multiPointerMultiArrayTest[13092:70b] array[1][2][0]=24

2013-12-09 18:35:38.989 multiPointerMultiArrayTest[13092:70b] array[1][2][1]=25

2013-12-09 18:35:38.989 multiPointerMultiArrayTest[13092:70b] array[1][2][2]=26

2013-12-09 18:35:38.990 multiPointerMultiArrayTest[13092:70b] array[1][2][3]=27

2013-12-09 18:35:38.990 multiPointerMultiArrayTest[13092:70b] array[1][3][0]=28

2013-12-09 18:35:38.990 multiPointerMultiArrayTest[13092:70b] array[1][3][1]=29

2013-12-09 18:35:38.991 multiPointerMultiArrayTest[13092:70b] array[1][3][2]=30

2013-12-09 18:35:38.991 multiPointerMultiArrayTest[13092:70b] array[1][3][3]=31

2013-12-09 18:35:38.991 multiPointerMultiArrayTest[13092:70b] array[2][0][0]=32

2013-12-09 18:35:38.992 multiPointerMultiArrayTest[13092:70b] array[2][0][1]=33

2013-12-09 18:35:38.992 multiPointerMultiArrayTest[13092:70b] array[2][0][2]=34

2013-12-09 18:35:38.992 multiPointerMultiArrayTest[13092:70b] array[2][0][3]=35

2013-12-09 18:35:38.993 multiPointerMultiArrayTest[13092:70b] array[2][1][0]=36

2013-12-09 18:35:38.993 multiPointerMultiArrayTest[13092:70b] array[2][1][1]=37

2013-12-09 18:35:38.993 multiPointerMultiArrayTest[13092:70b] array[2][1][2]=38

2013-12-09 18:35:38.994 multiPointerMultiArrayTest[13092:70b] array[2][1][3]=39

2013-12-09 18:35:38.994 multiPointerMultiArrayTest[13092:70b] array[2][2][0]=40

2013-12-09 18:35:38.994 multiPointerMultiArrayTest[13092:70b] array[2][2][1]=41

2013-12-09 18:35:38.995 multiPointerMultiArrayTest[13092:70b] array[2][2][2]=42

2013-12-09 18:35:38.995 multiPointerMultiArrayTest[13092:70b] array[2][2][3]=43

2013-12-09 18:35:38.995 multiPointerMultiArrayTest[13092:70b] array[2][3][0]=44

2013-12-09 18:35:38.996 multiPointerMultiArrayTest[13092:70b] array[2][3][1]=45

2013-12-09 18:35:38.996 multiPointerMultiArrayTest[13092:70b] array[2][3][2]=46

2013-12-09 18:35:38.996 multiPointerMultiArrayTest[13092:70b] array[2][3][3]=47

2013-12-09 18:35:38.997 multiPointerMultiArrayTest[13092:70b] array[3][0][0]=48

2013-12-09 18:35:38.997 multiPointerMultiArrayTest[13092:70b] array[3][0][1]=49

2013-12-09 18:35:38.997 multiPointerMultiArrayTest[13092:70b] array[3][0][2]=50

2013-12-09 18:35:38.998 multiPointerMultiArrayTest[13092:70b] array[3][0][3]=51

2013-12-09 18:35:38.998 multiPointerMultiArrayTest[13092:70b] array[3][1][0]=52

2013-12-09 18:35:38.998 multiPointerMultiArrayTest[13092:70b] array[3][1][1]=53

2013-12-09 18:35:38.999 multiPointerMultiArrayTest[13092:70b] array[3][1][2]=54

2013-12-09 18:35:38.999 multiPointerMultiArrayTest[13092:70b] array[3][1][3]=55

2013-12-09 18:35:38.999 multiPointerMultiArrayTest[13092:70b] array[3][2][0]=56

2013-12-09 18:35:39.000 multiPointerMultiArrayTest[13092:70b] array[3][2][1]=57

2013-12-09 18:35:39.000 multiPointerMultiArrayTest[13092:70b] array[3][2][2]=58

2013-12-09 18:35:39.000 multiPointerMultiArrayTest[13092:70b] array[3][2][3]=59

2013-12-09 18:35:39.000 multiPointerMultiArrayTest[13092:70b] array[3][3][0]=60

2013-12-09 18:35:39.001 multiPointerMultiArrayTest[13092:70b] array[3][3][1]=61

2013-12-09 18:35:39.001 multiPointerMultiArrayTest[13092:70b] array[3][3][2]=62

2013-12-09 18:35:39.001 multiPointerMultiArrayTest[13092:70b] array[3][3][3]=63

2013-12-09 18:35:44.276 multiPointerMultiArrayTest[13092:70b] p[0][0][0]=0

2013-12-09 18:35:44.276 multiPointerMultiArrayTest[13092:70b] p[0][0][1]=1

2013-12-09 18:35:44.277 multiPointerMultiArrayTest[13092:70b] p[0][0][2]=2

2013-12-09 18:35:44.277 multiPointerMultiArrayTest[13092:70b] p[0][0][3]=3

2013-12-09 18:35:44.277 multiPointerMultiArrayTest[13092:70b] p[0][1][0]=4

2013-12-09 18:35:44.278 multiPointerMultiArrayTest[13092:70b] p[0][1][1]=5

2013-12-09 18:35:44.278 multiPointerMultiArrayTest[13092:70b] p[0][1][2]=6

2013-12-09 18:35:44.278 multiPointerMultiArrayTest[13092:70b] p[0][1][3]=7

2013-12-09 18:35:44.279 multiPointerMultiArrayTest[13092:70b] p[0][2][0]=8

2013-12-09 18:35:44.279 multiPointerMultiArrayTest[13092:70b] p[0][2][1]=9

2013-12-09 18:35:44.279 multiPointerMultiArrayTest[13092:70b] p[0][2][2]=10

2013-12-09 18:35:44.280 multiPointerMultiArrayTest[13092:70b] p[0][2][3]=11

2013-12-09 18:35:44.280 multiPointerMultiArrayTest[13092:70b] p[0][3][0]=12

2013-12-09 18:35:44.281 multiPointerMultiArrayTest[13092:70b] p[0][3][1]=13

2013-12-09 18:35:44.281 multiPointerMultiArrayTest[13092:70b] p[0][3][2]=14

2013-12-09 18:35:44.281 multiPointerMultiArrayTest[13092:70b] p[0][3][3]=15

2013-12-09 18:35:44.281 multiPointerMultiArrayTest[13092:70b] p[1][0][0]=16

2013-12-09 18:35:44.282 multiPointerMultiArrayTest[13092:70b] p[1][0][1]=17

2013-12-09 18:35:44.282 multiPointerMultiArrayTest[13092:70b] p[1][0][2]=18

2013-12-09 18:35:44.282 multiPointerMultiArrayTest[13092:70b] p[1][0][3]=19

2013-12-09 18:35:44.283 multiPointerMultiArrayTest[13092:70b] p[1][1][0]=20

2013-12-09 18:35:44.283 multiPointerMultiArrayTest[13092:70b] p[1][1][1]=21

2013-12-09 18:35:44.283 multiPointerMultiArrayTest[13092:70b] p[1][1][2]=22

2013-12-09 18:35:44.284 multiPointerMultiArrayTest[13092:70b] p[1][1][3]=23

2013-12-09 18:35:44.284 multiPointerMultiArrayTest[13092:70b] p[1][2][0]=24

2013-12-09 18:35:44.284 multiPointerMultiArrayTest[13092:70b] p[1][2][1]=25

2013-12-09 18:35:44.285 multiPointerMultiArrayTest[13092:70b] p[1][2][2]=26

2013-12-09 18:35:44.285 multiPointerMultiArrayTest[13092:70b] p[1][2][3]=27

2013-12-09 18:35:44.285 multiPointerMultiArrayTest[13092:70b] p[1][3][0]=28

2013-12-09 18:35:44.286 multiPointerMultiArrayTest[13092:70b] p[1][3][1]=29

2013-12-09 18:35:44.286 multiPointerMultiArrayTest[13092:70b] p[1][3][2]=30

2013-12-09 18:35:44.286 multiPointerMultiArrayTest[13092:70b] p[1][3][3]=31

2013-12-09 18:35:44.287 multiPointerMultiArrayTest[13092:70b] p[2][0][0]=32

2013-12-09 18:35:44.287 multiPointerMultiArrayTest[13092:70b] p[2][0][1]=33

2013-12-09 18:35:44.287 multiPointerMultiArrayTest[13092:70b] p[2][0][2]=34

2013-12-09 18:35:44.288 multiPointerMultiArrayTest[13092:70b] p[2][0][3]=35

2013-12-09 18:35:44.288 multiPointerMultiArrayTest[13092:70b] p[2][1][0]=36

2013-12-09 18:35:44.288 multiPointerMultiArrayTest[13092:70b] p[2][1][1]=37

2013-12-09 18:35:44.289 multiPointerMultiArrayTest[13092:70b] p[2][1][2]=38

2013-12-09 18:35:44.289 multiPointerMultiArrayTest[13092:70b] p[2][1][3]=39

2013-12-09 18:35:44.289 multiPointerMultiArrayTest[13092:70b] p[2][2][0]=40

2013-12-09 18:35:44.290 multiPointerMultiArrayTest[13092:70b] p[2][2][1]=41

2013-12-09 18:35:44.290 multiPointerMultiArrayTest[13092:70b] p[2][2][2]=42

2013-12-09 18:35:44.290 multiPointerMultiArrayTest[13092:70b] p[2][2][3]=43

2013-12-09 18:35:44.291 multiPointerMultiArrayTest[13092:70b] p[2][3][0]=44

2013-12-09 18:35:44.291 multiPointerMultiArrayTest[13092:70b] p[2][3][1]=45

2013-12-09 18:35:44.291 multiPointerMultiArrayTest[13092:70b] p[2][3][2]=46

2013-12-09 18:35:44.292 multiPointerMultiArrayTest[13092:70b] p[2][3][3]=47

2013-12-09 18:35:44.292 multiPointerMultiArrayTest[13092:70b] p[3][0][0]=48

2013-12-09 18:35:44.292 multiPointerMultiArrayTest[13092:70b] p[3][0][1]=49

2013-12-09 18:35:44.292 multiPointerMultiArrayTest[13092:70b] p[3][0][2]=50

2013-12-09 18:35:44.303 multiPointerMultiArrayTest[13092:70b] p[3][0][3]=51

2013-12-09 18:35:44.303 multiPointerMultiArrayTest[13092:70b] p[3][1][0]=52

2013-12-09 18:35:44.304 multiPointerMultiArrayTest[13092:70b] p[3][1][1]=53

2013-12-09 18:35:44.304 multiPointerMultiArrayTest[13092:70b] p[3][1][2]=54

2013-12-09 18:35:44.304 multiPointerMultiArrayTest[13092:70b] p[3][1][3]=55

2013-12-09 18:35:44.305 multiPointerMultiArrayTest[13092:70b] p[3][2][0]=56

2013-12-09 18:35:44.305 multiPointerMultiArrayTest[13092:70b] p[3][2][1]=57

2013-12-09 18:35:44.305 multiPointerMultiArrayTest[13092:70b] p[3][2][2]=58

2013-12-09 18:35:44.306 multiPointerMultiArrayTest[13092:70b] p[3][2][3]=59

2013-12-09 18:35:44.306 multiPointerMultiArrayTest[13092:70b] p[3][3][0]=60

2013-12-09 18:35:44.307 multiPointerMultiArrayTest[13092:70b] p[3][3][1]=61

2013-12-09 18:35:44.307 multiPointerMultiArrayTest[13092:70b] p[3][3][2]=62

2013-12-09 18:35:44.307 multiPointerMultiArrayTest[13092:70b] p[3][3][3]=63

(lldb) print p[0]

(NSInteger **) $0 = 0x08f71020

(lldb) print p[0][0]

(NSInteger *) $1 = 0x08f6f890

(lldb) print p[0][0][0]

(NSInteger) $2 = 0

(lldb) print p[1][0][0]

(NSInteger) $3 = 16

(lldb) 


从日志输出情况,可以看下p是三级指针,p[0]是二级指针,p[0][0]是一级指针和数组的对应关系一目了然了吧。

  评论这张
 
阅读(218)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018