wp231957 发表于 2022-5-19 08:58:57

九九矩阵,练习二级指针,自用

#include <stdio.h>
#include <malloc.h>
#define N 9

int main()
{
    int **p=(int**)malloc(sizeof(int)*N);
    for(int i=0;i<N;i++)
    {
      *(p+i)=(int*)malloc(sizeof(int)*N);
    }
    int m=1;
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            *(*(p+i)+j)=m++;
      }
    }
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            printf("%4d",*(*(p+i)+j));
      }
      printf("\n");
    }
    for(int i=0;i<N;i++)
    {
      free(*(p+i));
    }
    free(p);
    return 0;
}

/*
PS D:\wp> ./ct4
   1   2   3   4   5   6   7   8   9
10    11    12    13    14    15    16    17    18
19    20    21    22    23    24    25    26    27
28    29    30    31    32    33    34    35    36
37    38    39    40    41    42    43    44    45
46    47    48    49    50    51    52    53    54
55    56    57    58    59    60    61    62    63
64    65    66    67    68    69    70    71    72
73    74    75    76    77    78    79    80    81
PS D:\wp>
*/

wp231957 发表于 2022-5-19 09:00:50

二版:
#include <stdio.h>
#include <malloc.h>
#define N 9

int main()
{
    int **p=(int**)malloc(sizeof(int)*N);
    for(int i=0;i<N;i++)
    {
      *(p++)=(int*)malloc(sizeof(int)*N);
    }
    p-=N;   //指针p 飞了,请回原点
    int m=1;
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            *(*(p+i)+j)=m++;
      }
    }
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            printf("%4d",*(*(p+i)+j));
      }
      printf("\n");
    }
    for(int i=0;i<N;i++)
    {
      free(*(p+i));
    }
    free(p);
    return 0;
}

wp231957 发表于 2022-5-19 09:02:06

wp231957 发表于 2022-5-19 09:00
二版:

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

int main()
{
    int **p=(int**)malloc(sizeof(int)*N);
    for(int i=0;i<N;i++)
    {
      p=(int*)malloc(sizeof(int)*N);
    }
    int m=1;
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            p=m++;
      }
    }
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            printf("%4d",p);
      }
      printf("\n");
    }
    for(int i=0;i<N;i++)
    {
      free(*(p+i));
    }
    free(p);
    return 0;
}

wp231957 发表于 2022-5-19 09:44:09

wp231957 发表于 2022-5-19 09:02
版本三:

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

int main()
{
    int **p=(int**)malloc(sizeof(int)*N);
    for(int i=0;i<N;i++)
    {
      p=(int*)malloc(sizeof(int)*N);
    }
    int m=1;
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            (*(p+i))=m++;
      }
    }
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            printf("%4d",(*(p+i)));
      }
      printf("\n");
    }
    for(int i=0;i<N;i++)
    {
      free(*(p+i));
    }
    free(p);
    return 0;
}

人造人 发表于 2022-5-19 12:15:08

#include <stdio.h>
#include <malloc.h>
#define N 9

int main()
{
    //int **p=(int**)malloc(sizeof(int)*N);   // bug

#if 1
    int **p = malloc(sizeof(*p) * N);       // ok
#else
    int **p = malloc(sizeof(int *) * N);    // ok
#endif

    for(int i=0;i<N;i++)
    {
      p=(int*)malloc(sizeof(int)*N);
    }
    int m=1;
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            p=m++;
      }
    }
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            printf("%4d",p);
      }
      printf("\n");
    }
    for(int i=0;i<N;i++)
    {
      free(*(p+i));
    }
    free(p);
    return 0;
}

wp231957 发表于 2022-5-19 12:55:53

人造人 发表于 2022-5-19 12:15


多谢指点,但我运行没啥问题啊

人造人 发表于 2022-5-19 13:12:17

wp231957 发表于 2022-5-19 12:55
多谢指点,但我运行没啥问题啊

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

$ cat main.c
#include <stdio.h>
#include <malloc.h>
#define N 9

int main()
{
    int **p=(int**)malloc(sizeof(int)*N);
    for(int i=0;i<N;i++)
    {
      p=(int*)malloc(sizeof(int)*N);
    }
    int m=1;
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            (*(p+i))=m++;
      }
    }
    for(int i=0;i<N;i++)
    {
      for(int j=0;j<N;j++)
      {
            printf("%4d",(*(p+i)));
      }
      printf("\n");
    }
    for(int i=0;i<N;i++)
    {
      free(*(p+i));
    }
    free(p);
    return 0;
}

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

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

SUMMARY: AddressSanitizer: heap-buffer-overflow /tmp/main.c:10 in main
Shadow bytes around the buggy address:
0x0c087fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c087fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c087fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c087fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c087fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c087fff8000: fa fa 00 00 00 00fa fa fa 00 00 00 00 04 fa
0x0c087fff8010: fa fa 00 00 00 00 04 fa fa fa 00 00 00 00 04 fa
0x0c087fff8020: fa fa 00 00 00 00 04 fa fa fa 00 00 00 00 04 fa
0x0c087fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c087fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c087fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable:         00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone:       fa
Freed heap region:       fd
Stack left redzone:      f1
Stack mid redzone:       f2
Stack right redzone:   f3
Stack after return:      f5
Stack use after scope:   f8
Global redzone:          f9
Global init order:       f6
Poisoned by user:      f7
Container overflow:      fc
Array cookie:            ac
Intra object redzone:    bb
ASan internal:         fe
Left alloca redzone:   ca
Right alloca redzone:    cb
Shadow gap:            cc
==2144349==ABORTING
$

wp231957 发表于 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>
这个不行吗

wp231957 发表于 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>

人造人 发表于 2022-5-19 13:44:53

wp231957 发表于 2022-5-19 13:21
这些都是什么意思啊

PS D:\wp\wp> gcc -debug -o ct4 ct4.c


$ alias gcc-debug
alias gcc-debug='gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer'
$

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

我记得在windows下是不行

人造人 发表于 2022-5-19 13:46:22

gcc 6.x 有点太老了,换一个新一点的编译器试试

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

$

人造人 发表于 2022-5-19 13:48:11

gcc 12.x也有了,6.x是真的有点老了

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

$

hornwong 发表于 2022-5-22 23:45:04

{:5_106:}
页: [1]
查看完整版本: 九九矩阵,练习二级指针,自用