我是valgrind的诺布。 GCC编译器编译器给了我标志,但是在线编译器让我很难在需要提交此代码的地方。请帮我查找以下代码的内存访问冲突
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include"railfence.h"
#define RAIL 2
char* getCipher(char plainText[])
{
int strln;
int i;
int j;
int k = 0;
char *result;
char **arr;
strln = strlen(plainText);
if (strln == 0 || strln > 20)
return "-1";
for ( i=0 ; i < strln ; i++)
if (!isupper(plainText[i]))
return "-1";
arr = (char **) malloc ( RAIL*sizeof(char) );
for ( i=0 ; i < RAIL ; i++)
arr[i] = (char *) malloc ( strln*sizeof(char) );
k = 0;
for ( i = 0 ; i < strln ; i++ )
for ( j = 0 ; j < 2 ; j++ ) {
if ( (i+j)&1 )
arr[j][i] = '.';
else
arr[j][i] = plainText[k++];
}
result = (char *) malloc (sizeof(char) * strln);
k=0;
strln = strlen(*arr);
for ( i = 0 ; i < RAIL ; i++ ) {
for ( j = 0 ; j < strln ; j++ )
if ( arr[i][j] != '.' )
result[k++] = arr[i][j];
free(arr[i]);
}
free(arr);
return result;
}
的main.c
#include <stdio.h>
#include"railfence.h"
int main()
{
//TestCase 1
{
printf("\nTestCase 1\n");
char plainText[]="ABCDEFGH";
char * cipherText= getCipher(plainText);
if(cipherText!=NULL)
printf("%s\n",cipherText);
}
}
Valgrind日志:
==31753== Memcheck, a memory error detector
==31753== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==31753== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==31753== Command: ./main
==31753==
TestCase 1
==31753== Invalid write of size 8
==31753== at 0x40079F: getCipher (railfence.c:30)
==31753== by 0x400977: main (main.c:14)
==31753== Address 0x51fd040 is 0 bytes inside a block of size 2 alloc'd
==31753== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31753== by 0x40076F: getCipher (railfence.c:26)
==31753== by 0x400977: main (main.c:14)
==31753==
==31753== Invalid read of size 8
==31753== at 0x40080F: getCipher (railfence.c:38)
==31753== by 0x400977: main (main.c:14)
==31753== Address 0x51fd040 is 0 bytes inside a block of size 2 alloc'd
==31753== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31753== by 0x40076F: getCipher (railfence.c:26)
==31753== by 0x400977: main (main.c:14)
==31753==
==31753== Invalid read of size 8
==31753== at 0x4007EB: getCipher (railfence.c:36)
==31753== by 0x400977: main (main.c:14)
==31753== Address 0x51fd048 is 6 bytes after a block of size 2 alloc'd
==31753== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31753== by 0x40076F: getCipher (railfence.c:26)
==31753== by 0x400977: main (main.c:14)
==31753==
==31753== Invalid read of size 8
==31753== at 0x400869: getCipher (railfence.c:43)
==31753== by 0x400977: main (main.c:14)
==31753== Address 0x51fd040 is 0 bytes inside a block of size 2 alloc'd
==31753== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31753== by 0x40076F: getCipher (railfence.c:26)
==31753== by 0x400977: main (main.c:14)
==31753==
==31753== Invalid read of size 1
==31753== at 0x4C2E0F4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31753== by 0x400873: getCipher (railfence.c:43)
==31753== by 0x400977: main (main.c:14)
==31753== Address 0x51fd098 is 0 bytes after a block of size 8 alloc'd
==31753== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31753== by 0x40079E: getCipher (railfence.c:30)
==31753== by 0x400977: main (main.c:14)
==31753==
==31753== Invalid read of size 8
==31753== at 0x4008A0: getCipher (railfence.c:46)
==31753== by 0x400977: main (main.c:14)
==31753== Address 0x51fd040 is 0 bytes inside a block of size 2 alloc'd
==31753== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31753== by 0x40076F: getCipher (railfence.c:26)
==31753== by 0x400977: main (main.c:14)
==31753==
==31753== Invalid read of size 8
==31753== at 0x4008D9: getCipher (railfence.c:47)
==31753== by 0x400977: main (main.c:14)
==31753== Address 0x51fd040 is 0 bytes inside a block of size 2 alloc'd
==31753== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31753== by 0x40076F: getCipher (railfence.c:26)
==31753== by 0x400977: main (main.c:14)
==31753==
==31753== Invalid read of size 8
==31753== at 0x400909: getCipher (railfence.c:49)
==31753== by 0x400977: main (main.c:14)
==31753== Address 0x51fd040 is 0 bytes inside a block of size 2 alloc'd
==31753== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31753== by 0x40076F: getCipher (railfence.c:26)
==31753== by 0x400977: main (main.c:14)
==31753==
==31753== Invalid read of size 1
==31753== at 0x4C2E0F4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31753== by 0x4EA7C7B: puts (ioputs.c:36)
==31753== by 0x40098E: main (main.c:16)
==31753== Address 0x51fd138 is 0 bytes after a block of size 8 alloc'd
==31753== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31753== by 0x400859: getCipher (railfence.c:41)
==31753== by 0x400977: main (main.c:14)
==31753==
ACEGBDFH
==31753==
==31753== HEAP SUMMARY:
==31753== in use at exit: 8 bytes in 1 blocks
==31753== total heap usage: 4 allocs, 3 frees, 26 bytes allocated
==31753==
==31753== LEAK SUMMARY:
==31753== definitely lost: 8 bytes in 1 blocks
==31753== indirectly lost: 0 bytes in 0 blocks
==31753== possibly lost: 0 bytes in 0 blocks
==31753== still reachable: 0 bytes in 0 blocks
==31753== suppressed: 0 bytes in 0 blocks
==31753== Rerun with --leak-check=full to see details of leaked memory
==31753==
==31753== For counts of detected and suppressed errors, rerun with: -v
==31753== ERROR SUMMARY: 47 errors from 9 contexts (suppressed: 0 from 0)
答案 0 :(得分:0)
尝试更改此行代码
arr = (char **) malloc ( RAIL*sizeof(char) );
用这个
arr = (char **) malloc ( RAIL*sizeof(char*) );