鱼C论坛

 找回密码
 立即注册
查看: 1694|回复: 12

[技术交流] 九九矩阵,练习二级指针,自用

[复制链接]
发表于 2022-5-19 08:58:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define N 9

  4. int main()
  5. {
  6.     int **p=(int**)malloc(sizeof(int)*N);
  7.     for(int i=0;i<N;i++)
  8.     {
  9.         *(p+i)=(int*)malloc(sizeof(int)*N);
  10.     }
  11.     int m=1;
  12.     for(int i=0;i<N;i++)
  13.     {
  14.         for(int j=0;j<N;j++)
  15.         {
  16.             *(*(p+i)+j)=m++;
  17.         }
  18.     }
  19.     for(int i=0;i<N;i++)
  20.     {
  21.         for(int j=0;j<N;j++)
  22.         {
  23.             printf("%4d  ",*(*(p+i)+j));
  24.         }
  25.         printf("\n");
  26.     }
  27.     for(int i=0;i<N;i++)
  28.     {
  29.         free(*(p+i));
  30.     }
  31.     free(p);
  32.     return 0;
  33. }

  34. /*
  35. PS D:\wp> ./ct4
  36.    1     2     3     4     5     6     7     8     9  
  37.   10    11    12    13    14    15    16    17    18
  38.   19    20    21    22    23    24    25    26    27
  39.   28    29    30    31    32    33    34    35    36
  40.   37    38    39    40    41    42    43    44    45
  41.   46    47    48    49    50    51    52    53    54
  42.   55    56    57    58    59    60    61    62    63
  43.   64    65    66    67    68    69    70    71    72
  44.   73    74    75    76    77    78    79    80    81
  45. PS D:\wp>
  46. */
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-19 09:00:50 | 显示全部楼层
二版:
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define N 9

  4. int main()
  5. {
  6.     int **p=(int**)malloc(sizeof(int)*N);
  7.     for(int i=0;i<N;i++)
  8.     {
  9.         *(p++)=(int*)malloc(sizeof(int)*N);
  10.     }
  11.     p-=N;   //指针p 飞了,请回原点
  12.     int m=1;
  13.     for(int i=0;i<N;i++)
  14.     {
  15.         for(int j=0;j<N;j++)
  16.         {
  17.             *(*(p+i)+j)=m++;
  18.         }
  19.     }
  20.     for(int i=0;i<N;i++)
  21.     {
  22.         for(int j=0;j<N;j++)
  23.         {
  24.             printf("%4d  ",*(*(p+i)+j));
  25.         }
  26.         printf("\n");
  27.     }
  28.     for(int i=0;i<N;i++)
  29.     {
  30.         free(*(p+i));
  31.     }
  32.     free(p);
  33.     return 0;
  34. }

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-19 09:02:06 | 显示全部楼层

版本三:
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define N 9

  4. int main()
  5. {
  6.     int **p=(int**)malloc(sizeof(int)*N);
  7.     for(int i=0;i<N;i++)
  8.     {
  9.         p[i]=(int*)malloc(sizeof(int)*N);
  10.     }
  11.     int m=1;
  12.     for(int i=0;i<N;i++)
  13.     {
  14.         for(int j=0;j<N;j++)
  15.         {
  16.             p[i][j]=m++;
  17.         }
  18.     }
  19.     for(int i=0;i<N;i++)
  20.     {
  21.         for(int j=0;j<N;j++)
  22.         {
  23.             printf("%4d  ",p[i][j]);
  24.         }
  25.         printf("\n");
  26.     }
  27.     for(int i=0;i<N;i++)
  28.     {
  29.         free(*(p+i));
  30.     }
  31.     free(p);
  32.     return 0;
  33. }

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-19 09:44:09 | 显示全部楼层

四版:
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define N 9

  4. int main()
  5. {
  6.     int **p=(int**)malloc(sizeof(int)*N);
  7.     for(int i=0;i<N;i++)
  8.     {
  9.         p[i]=(int*)malloc(sizeof(int)*N);
  10.     }
  11.     int m=1;
  12.     for(int i=0;i<N;i++)
  13.     {
  14.         for(int j=0;j<N;j++)
  15.         {
  16.             (*(p+i))[j]=m++;
  17.         }
  18.     }
  19.     for(int i=0;i<N;i++)
  20.     {
  21.         for(int j=0;j<N;j++)
  22.         {
  23.             printf("%4d  ",(*(p+i))[j]);
  24.         }
  25.         printf("\n");
  26.     }
  27.     for(int i=0;i<N;i++)
  28.     {
  29.         free(*(p+i));
  30.     }
  31.     free(p);
  32.     return 0;
  33. }

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-19 12:15:08 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define N 9

  4. int main()
  5. {
  6.     //int **p=(int**)malloc(sizeof(int)*N);     // bug

  7. #if 1
  8.     int **p = malloc(sizeof(*p) * N);       // ok
  9. #else
  10.     int **p = malloc(sizeof(int *) * N);    // ok
  11. #endif

  12.     for(int i=0;i<N;i++)
  13.     {
  14.         p[i]=(int*)malloc(sizeof(int)*N);
  15.     }
  16.     int m=1;
  17.     for(int i=0;i<N;i++)
  18.     {
  19.         for(int j=0;j<N;j++)
  20.         {
  21.             p[i][j]=m++;
  22.         }
  23.     }
  24.     for(int i=0;i<N;i++)
  25.     {
  26.         for(int j=0;j<N;j++)
  27.         {
  28.             printf("%4d  ",p[i][j]);
  29.         }
  30.         printf("\n");
  31.     }
  32.     for(int i=0;i<N;i++)
  33.     {
  34.         free(*(p+i));
  35.     }
  36.     free(p);
  37.     return 0;
  38. }

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-19 12:55:53 From FishC Mobile | 显示全部楼层
人造人 发表于 2022-5-19 12:15

多谢指点,但我运行没啥问题啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-19 13:12:17 | 显示全部楼层
wp231957 发表于 2022-5-19 12:55
多谢指点,但我运行没啥问题啊

换一个好点的编译器,可以帮你检查出更多的问题

  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #define N 9

  5. int main()
  6. {
  7.     int **p=(int**)malloc(sizeof(int)*N);
  8.     for(int i=0;i<N;i++)
  9.     {
  10.         p[i]=(int*)malloc(sizeof(int)*N);
  11.     }
  12.     int m=1;
  13.     for(int i=0;i<N;i++)
  14.     {
  15.         for(int j=0;j<N;j++)
  16.         {
  17.             (*(p+i))[j]=m++;
  18.         }
  19.     }
  20.     for(int i=0;i<N;i++)
  21.     {
  22.         for(int j=0;j<N;j++)
  23.         {
  24.             printf("%4d  ",(*(p+i))[j]);
  25.         }
  26.         printf("\n");
  27.     }
  28.     for(int i=0;i<N;i++)
  29.     {
  30.         free(*(p+i));
  31.     }
  32.     free(p);
  33.     return 0;
  34. }

  35. $ gcc-debug -o main main.c
  36. $ ./main
  37. =================================================================
  38. ==2144349==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x604000000030 at pc 0x561b0dd312e1 bp 0x7ffe4dc688f0 sp 0x7ffe4dc688e0
  39. WRITE of size 8 at 0x604000000030 thread T0
  40.     #0 0x561b0dd312e0 in main /tmp/main.c:10
  41.     #1 0x7f34ce3b130f in __libc_start_call_main (/usr/lib/libc.so.6+0x2d30f)
  42.     #2 0x7f34ce3b13c0 in __libc_start_main@GLIBC_2.2.5 (/usr/lib/libc.so.6+0x2d3c0)
  43.     #3 0x561b0dd31144 in _start (/tmp/main+0x2144)

  44. 0x604000000034 is located 0 bytes to the right of 36-byte region [0x604000000010,0x604000000034)
  45. allocated by thread T0 here:
  46.     #0 0x7f34cefbadd9 in __interceptor_malloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:145
  47.     #1 0x561b0dd3122f in main /tmp/main.c:7
  48.     #2 0x7f34ce3b130f in __libc_start_call_main (/usr/lib/libc.so.6+0x2d30f)

  49. SUMMARY: AddressSanitizer: heap-buffer-overflow /tmp/main.c:10 in main
  50. Shadow bytes around the buggy address:
  51.   0x0c087fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  52.   0x0c087fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  53.   0x0c087fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  54.   0x0c087fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  55.   0x0c087fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  56. =>0x0c087fff8000: fa fa 00 00 00 00[04]fa fa fa 00 00 00 00 04 fa
  57.   0x0c087fff8010: fa fa 00 00 00 00 04 fa fa fa 00 00 00 00 04 fa
  58.   0x0c087fff8020: fa fa 00 00 00 00 04 fa fa fa 00 00 00 00 04 fa
  59.   0x0c087fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  60.   0x0c087fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  61.   0x0c087fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  62. Shadow byte legend (one shadow byte represents 8 application bytes):
  63.   Addressable:           00
  64.   Partially addressable: 01 02 03 04 05 06 07
  65.   Heap left redzone:       fa
  66.   Freed heap region:       fd
  67.   Stack left redzone:      f1
  68.   Stack mid redzone:       f2
  69.   Stack right redzone:     f3
  70.   Stack after return:      f5
  71.   Stack use after scope:   f8
  72.   Global redzone:          f9
  73.   Global init order:       f6
  74.   Poisoned by user:        f7
  75.   Container overflow:      fc
  76.   Array cookie:            ac
  77.   Intra object redzone:    bb
  78.   ASan internal:           fe
  79.   Left alloca redzone:     ca
  80.   Right alloca redzone:    cb
  81.   Shadow gap:              cc
  82. ==2144349==ABORTING
  83. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-19 13:15:38 | 显示全部楼层
人造人 发表于 2022-5-19 13:12
换一个好点的编译器,可以帮你检查出更多的问题

PS D:\wp> gcc -v
Using built-in specs.
COLLECT_GCC=D:\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=d:/mingw64/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
PS D:\wp>
这个不行吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-19 13:21:32 | 显示全部楼层
人造人 发表于 2022-5-19 13:12
换一个好点的编译器,可以帮你检查出更多的问题

这些都是什么意思啊

PS D:\wp\wp> gcc -debug -o ct4 ct4.c
cc1.exe: warning: unrecognized gcc debugging option: e
cc1.exe: warning: unrecognized gcc debugging option: b
cc1.exe: warning: unrecognized gcc debugging option: u
cc1.exe: warning: unrecognized gcc debugging option: g

PS D:\wp\wp>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-19 13:44:53 | 显示全部楼层
wp231957 发表于 2022-5-19 13:21
这些都是什么意思啊

PS D:\wp\wp> gcc -debug -o ct4 ct4.c
  1. $ alias gcc-debug
  2. alias gcc-debug='gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer'
  3. $
复制代码


gcc-debug 是一个 alias
你试试
  1. gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer -o main main.c
复制代码


我记得在windows下是不行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-19 13:46:22 | 显示全部楼层
gcc 6.x 有点太老了,换一个新一点的编译器试试

  1. $ gcc --version
  2. gcc (GCC) 11.2.0
  3. Copyright (C) 2021 Free Software Foundation, Inc.
  4. This is free software; see the source for copying conditions.  There is NO
  5. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  6. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-19 13:48:11 | 显示全部楼层
gcc 12.x也有了,6.x是真的有点老了

  1. $ gcc --version
  2. gcc (GCC) 12.1.0
  3. Copyright (C) 2022 Free Software Foundation, Inc.
  4. This is free software; see the source for copying conditions.  There is NO
  5. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  6. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-22 23:45:04 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 23:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表