使用指针打印图案时出现问题

时间:2019-03-05 22:33:16

标签: c algorithm pointers

我的代码有问题,我无法弄清楚我尝试了所有事情和可能的修改的问题在哪里

以以下示例为例:

输入为2:

正常输出:

2 2 2 
2 1 2 
2 2 2 

我的代码输出

2 2 2 
2 1 2 
2 2 2 

没关系

输入为5:

正常输出:

5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5 
5 4 3 3 3 3 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 2 1 2 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 3 3 3 3 4 5 
5 4 4 4 4 4 4 4 5 
5 5 5 5 5 5 5 5 5

我的代码输出:

5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 540352565
Segmentation fault (core dumped)

这不行:s

我无法弄清楚为什么此540352565出现在我的数组的8x8字段中 Segmentation fault (core dumped)也是由自由引起的,我认为它与神秘数540352565

有关

如果有人可以帮助我解决这个问题,我将不胜感激:p

这是我的代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{

    int n,fs,fe,i,j,nt;
    int** pattern = NULL;
    scanf("%d", &n);

    nt = n+(n-1);//number of line

    // Allocating memory
    pattern=malloc( nt * sizeof(int*));
    for(i=0;i<nt;i++) pattern[i] = malloc(sizeof(int));
    printf("Start\n");
    // filling pointer with data
    for(fs=0,fe=nt-1;fs != fe;fs++,fe--){//where to write
        for(i=fs;i<=fe;i++){
          printf("n=%d,nt=%d,i=%d,fs=%d,fe=%d\n",n,nt,i,fs,fe);
          pattern[fs][i] = n; // line 1
          pattern[fe][i] = n; // line 2
          pattern[i][fs] = n; // line 1
          pattern[i][fe] = n; // line 2
        }
        n--;
    }
    pattern[nt/2][nt/2] = 1; // the central element
    //printing pattern
    for(i=0;i<nt;i++){
      for (j=0; j<nt; j++)
        printf("%d ",pattern[i][j]);
      printf("\n");
    }
    //desallocating memory
    for(i=0;i<nt;i++) free(pattern[i]);
    free(pattern);
    return 0;
}

这是 valgrind 的输出,它执行内存检查

==13198== Memcheck, a memory error detector
==13198== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13198== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==13198== Command: ./main 2
==13198== 
--13198-- Valgrind options:
--13198--    -v
--13198-- Contents of /proc/version:
--13198--   Linux version 4.15.0-45-generic (buildd@lgw01-amd64-031) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019
--13198-- 
--13198-- Arch and hwcaps: AMD64, LittleEndian, amd64-cx16-rdtscp-sse3-avx
--13198-- Page sizes: currently 4096, max supported 4096
--13198-- Valgrind library directory: /usr/lib/valgrind
--13198-- Reading syms from /home/aminejow/exercism_files/c/pattern/main
--13198-- Reading syms from /lib/x86_64-linux-gnu/ld-2.27.so
--13198--   Considering /lib/x86_64-linux-gnu/ld-2.27.so ..
--13198--   .. CRC mismatch (computed 1b7c895e wanted 2943108a)
--13198--   Considering /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.27.so ..
--13198--   .. CRC is valid
--13198-- Reading syms from /usr/lib/valgrind/memcheck-amd64-linux
--13198--   Considering /usr/lib/valgrind/memcheck-amd64-linux ..
--13198--   .. CRC mismatch (computed c25f395c wanted 0a9602a8)
--13198--    object doesn't have a symbol table
--13198--    object doesn't have a dynamic symbol table
--13198-- Scheduler: using generic scheduler lock implementation.
--13198-- Reading suppressions file: /usr/lib/valgrind/default.supp
==13198== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-13198-by-aminejow-on-???
==13198== embedded gdbserver: writing to   /tmp/vgdb-pipe-to-vgdb-from-13198-by-aminejow-on-???
==13198== embedded gdbserver: shared mem   /tmp/vgdb-pipe-shared-mem-vgdb-13198-by-aminejow-on-???
==13198== 
==13198== TO CONTROL THIS PROCESS USING vgdb (which you probably
==13198== don't want to do, unless you know exactly what you're doing,
==13198== or are doing some strange experiment):
==13198==   /usr/lib/valgrind/../../bin/vgdb --pid=13198 ...command...
==13198== 
==13198== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==13198==   /path/to/gdb ./main
==13198== and then give GDB the following command
==13198==   target remote | /usr/lib/valgrind/../../bin/vgdb --pid=13198
==13198== --pid is optional if only one valgrind process is running
==13198== 
--13198-- REDIR: 0x401f2f0 (ld-linux-x86-64.so.2:strlen) redirected to 0x58060901 (???)
--13198-- REDIR: 0x401f0d0 (ld-linux-x86-64.so.2:index) redirected to 0x5806091b (???)
--13198-- Reading syms from /usr/lib/valgrind/vgpreload_core-amd64-linux.so
--13198--   Considering /usr/lib/valgrind/vgpreload_core-amd64-linux.so ..
--13198--   .. CRC mismatch (computed 4b63d83e wanted 670599e6)
--13198--    object doesn't have a symbol table
--13198-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
--13198--   Considering /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so ..
--13198--   .. CRC mismatch (computed a4b37bee wanted 8ad4dc94)
--13198--    object doesn't have a symbol table
==13198== WARNING: new redirection conflicts with existing -- ignoring it
--13198--     old: 0x0401f2f0 (strlen              ) R-> (0000.0) 0x58060901 ???
--13198--     new: 0x0401f2f0 (strlen              ) R-> (2007.0) 0x04c32db0 strlen
--13198-- REDIR: 0x401d360 (ld-linux-x86-64.so.2:strcmp) redirected to 0x4c33ee0 (strcmp)
--13198-- REDIR: 0x401f830 (ld-linux-x86-64.so.2:mempcpy) redirected to 0x4c374f0 (mempcpy)
--13198-- Reading syms from /lib/x86_64-linux-gnu/libc-2.27.so
--13198--   Considering /lib/x86_64-linux-gnu/libc-2.27.so ..
--13198--   .. CRC mismatch (computed b1c74187 wanted 042cc048)
--13198--   Considering /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.27.so ..
--13198--   .. CRC is valid
--13198-- REDIR: 0x4edac70 (libc.so.6:memmove) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ed9d40 (libc.so.6:strncpy) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edaf50 (libc.so.6:strcasecmp) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ed9790 (libc.so.6:strcat) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ed9d70 (libc.so.6:rindex) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edc7c0 (libc.so.6:rawmemchr) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edade0 (libc.so.6:mempcpy) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edac10 (libc.so.6:bcmp) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ed9d00 (libc.so.6:strncmp) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ed9800 (libc.so.6:strcmp) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edad40 (libc.so.6:memset) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ef80f0 (libc.so.6:wcschr) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ed9ca0 (libc.so.6:strnlen) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ed9870 (libc.so.6:strcspn) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edafa0 (libc.so.6:strncasecmp) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ed9840 (libc.so.6:strcpy) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edb0e0 (libc.so.6:memcpy@@GLIBC_2.14) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ed9da0 (libc.so.6:strpbrk) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ed97c0 (libc.so.6:index) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ed9c70 (libc.so.6:strlen) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ee46c0 (libc.so.6:memrchr) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edaff0 (libc.so.6:strcasecmp_l) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edabe0 (libc.so.6:memchr) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4ef8eb0 (libc.so.6:wcslen) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4eda050 (libc.so.6:strspn) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edaf20 (libc.so.6:stpncpy) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edaef0 (libc.so.6:stpcpy) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edc7f0 (libc.so.6:strchrnul) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4edb040 (libc.so.6:strncasecmp_l) redirected to 0x4a2a6e0 (_vgnU_ifunc_wrapper)
--13198-- REDIR: 0x4eed330 (libc.so.6:__strrchr_sse2) redirected to 0x4c32790 (__strrchr_sse2)
--13198-- REDIR: 0x4ed3070 (libc.so.6:malloc) redirected to 0x4c2faa0 (malloc)
--13198-- REDIR: 0x4eed620 (libc.so.6:__strlen_sse2) redirected to 0x4c32d30 (__strlen_sse2)
--13198-- REDIR: 0x4eed120 (libc.so.6:__strchrnul_sse2) redirected to 0x4c37020 (strchrnul)
--13198-- REDIR: 0x4ef7440 (libc.so.6:__mempcpy_sse2_unaligned) redirected to 0x4c37130 (mempcpy)
==13198== Invalid write of size 4
==13198==    at 0x1089C2: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d530 is 16 bytes after a block of size 16 in arena "client"
==13198== 
==13198== Invalid write of size 4
==13198==    at 0x108947: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d514 is 0 bytes after a block of size 4 alloc'd
==13198==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13198==    by 0x1088BC: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198== 
==13198== Invalid write of size 4
==13198==    at 0x108970: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d794 is 0 bytes after a block of size 4 alloc'd
==13198==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13198==    by 0x1088BC: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198== 
==13198== Invalid write of size 4
==13198==    at 0x108999: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d564 is 0 bytes after a block of size 4 alloc'd
==13198==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13198==    by 0x1088BC: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198== 
==13198== Invalid write of size 4
==13198==    at 0x108A26: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d660 is 12 bytes after a block of size 4 alloc'd
==13198==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13198==    by 0x1088BC: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198== 
==13198== Invalid read of size 4
==13198==    at 0x108A62: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d514 is 0 bytes after a block of size 4 alloc'd
==13198==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13198==    by 0x1088BC: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198== 
--13198-- REDIR: 0x4ed3950 (libc.so.6:free) redirected to 0x4c30cd0 (free)
Start
n=5,nt=9,i=0,fs=0,fe=8
n=5,nt=9,i=1,fs=0,fe=8
n=5,nt=9,i=2,fs=0,fe=8
n=5,nt=9,i=3,fs=0,fe=8
n=5,nt=9,i=4,fs=0,fe=8
n=5,nt=9,i=5,fs=0,fe=8
n=5,nt=9,i=6,fs=0,fe=8
n=5,nt=9,i=7,fs=0,fe=8
n=5,nt=9,i=8,fs=0,fe=8
n=4,nt=9,i=1,fs=1,fe=7
n=4,nt=9,i=2,fs=1,fe=7
n=4,nt=9,i=3,fs=1,fe=7
n=4,nt=9,i=4,fs=1,fe=7
n=4,nt=9,i=5,fs=1,fe=7
n=4,nt=9,i=6,fs=1,fe=7
n=4,nt=9,i=7,fs=1,fe=7
n=3,nt=9,i=2,fs=2,fe=6
n=3,nt=9,i=3,fs=2,fe=6
n=3,nt=9,i=4,fs=2,fe=6
n=3,nt=9,i=5,fs=2,fe=6
n=3,nt=9,i=6,fs=2,fe=6
n=2,nt=9,i=3,fs=3,fe=5
n=2,nt=9,i=4,fs=3,fe=5
n=2,nt=9,i=5,fs=3,fe=5
5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5 
5 4 3 3 3 3 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 2 1 2 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 3 3 3 3 4 5 
5 4 4 4 4 4 4 4 5 
5 5 5 5 5 5 5 5 5 
==13198== 
==13198== HEAP SUMMARY:
==13198==     in use at exit: 0 bytes in 0 blocks
==13198==   total heap usage: 12 allocs, 12 frees, 5,228 bytes allocated
==13198== 
==13198== All heap blocks were freed -- no leaks are possible
==13198== 
==13198== ERROR SUMMARY: 158 errors from 6 contexts (suppressed: 0 from 0)
==13198== 
==13198== 1 errors in context 1 of 6:
==13198== Invalid write of size 4
==13198==    at 0x108A26: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d660 is 12 bytes after a block of size 4 alloc'd
==13198==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13198==    by 0x1088BC: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198== 
==13198== 
==13198== 15 errors in context 2 of 6:
==13198== Invalid write of size 4
==13198==    at 0x108999: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d564 is 0 bytes after a block of size 4 alloc'd
==13198==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13198==    by 0x1088BC: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198== 
==13198== 
==13198== 23 errors in context 3 of 6:
==13198== Invalid write of size 4
==13198==    at 0x108970: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d794 is 0 bytes after a block of size 4 alloc'd
==13198==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13198==    by 0x1088BC: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198== 
==13198== 
==13198== 23 errors in context 4 of 6:
==13198== Invalid write of size 4
==13198==    at 0x108947: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d514 is 0 bytes after a block of size 4 alloc'd
==13198==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13198==    by 0x1088BC: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198== 
==13198== 
==13198== 24 errors in context 5 of 6:
==13198== Invalid write of size 4
==13198==    at 0x1089C2: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d530 is 16 bytes after a block of size 16 in arena "client"
==13198== 
==13198== 
==13198== 72 errors in context 6 of 6:
==13198== Invalid read of size 4
==13198==    at 0x108A62: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198==  Address 0x522d514 is 0 bytes after a block of size 4 alloc'd
==13198==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13198==    by 0x1088BC: main (in /home/aminejow/exercism_files/c/pattern/main)
==13198== 
==13198== ERROR SUMMARY: 158 errors from 6 contexts (suppressed: 0 from 0)

1 个答案:

答案 0 :(得分:1)

您没有为每一行分配足够的内存:

for(i=0;i<nt;i++) pattern[i] = malloc(sizeof(int));

在这里,您仅为一个int分配空间。您需要其中nt

for(i=0;i<nt;i++) pattern[i] = malloc(nt * sizeof(int));