c - finding memory access violation for a code -


i noob on valgrind. gcc compiler compiler gave me go flag online compiler gave me hard time need submit code. please me finding memory access violation below code

#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";      ( i=0 ; < strln ; i++)          if (!isupper(plaintext[i]))              return "-1";      arr = (char **) malloc ( rail*sizeof(char) );      ( i=0 ; < rail ; i++)          arr[i] = (char *) malloc ( strln*sizeof(char) );      k = 0;     ( = 0 ; < strln ; i++ )         ( 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);     ( = 0 ; < rail ; i++ ) {         ( 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 log :

==31753== memcheck, memory error detector ==31753== copyright (c) 2002-2013, , gnu gpl'd, julian seward et al. ==31753== using valgrind-3.10.0.svn , libvex; rerun -h copyright info ==31753== command: ./main ==31753==   testcase 1 ==31753== invalid write of size 8 ==31753==    @ 0x40079f: getcipher (railfence.c:30) ==31753==    0x400977: main (main.c:14) ==31753==  address 0x51fd040 0 bytes inside block of size 2 alloc'd ==31753==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31753==    0x40076f: getcipher (railfence.c:26) ==31753==    0x400977: main (main.c:14) ==31753==  ==31753== invalid read of size 8 ==31753==    @ 0x40080f: getcipher (railfence.c:38) ==31753==    0x400977: main (main.c:14) ==31753==  address 0x51fd040 0 bytes inside block of size 2 alloc'd ==31753==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31753==    0x40076f: getcipher (railfence.c:26) ==31753==    0x400977: main (main.c:14) ==31753==  ==31753== invalid read of size 8 ==31753==    @ 0x4007eb: getcipher (railfence.c:36) ==31753==    0x400977: main (main.c:14) ==31753==  address 0x51fd048 6 bytes after block of size 2 alloc'd ==31753==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31753==    0x40076f: getcipher (railfence.c:26) ==31753==    0x400977: main (main.c:14) ==31753==  ==31753== invalid read of size 8 ==31753==    @ 0x400869: getcipher (railfence.c:43) ==31753==    0x400977: main (main.c:14) ==31753==  address 0x51fd040 0 bytes inside block of size 2 alloc'd ==31753==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31753==    0x40076f: getcipher (railfence.c:26) ==31753==    0x400977: main (main.c:14) ==31753==  ==31753== invalid read of size 1 ==31753==    @ 0x4c2e0f4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31753==    0x400873: getcipher (railfence.c:43) ==31753==    0x400977: main (main.c:14) ==31753==  address 0x51fd098 0 bytes after block of size 8 alloc'd ==31753==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31753==    0x40079e: getcipher (railfence.c:30) ==31753==    0x400977: main (main.c:14) ==31753==  ==31753== invalid read of size 8 ==31753==    @ 0x4008a0: getcipher (railfence.c:46) ==31753==    0x400977: main (main.c:14) ==31753==  address 0x51fd040 0 bytes inside block of size 2 alloc'd ==31753==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31753==    0x40076f: getcipher (railfence.c:26) ==31753==    0x400977: main (main.c:14) ==31753==  ==31753== invalid read of size 8 ==31753==    @ 0x4008d9: getcipher (railfence.c:47) ==31753==    0x400977: main (main.c:14) ==31753==  address 0x51fd040 0 bytes inside block of size 2 alloc'd ==31753==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31753==    0x40076f: getcipher (railfence.c:26) ==31753==    0x400977: main (main.c:14) ==31753==  ==31753== invalid read of size 8 ==31753==    @ 0x400909: getcipher (railfence.c:49) ==31753==    0x400977: main (main.c:14) ==31753==  address 0x51fd040 0 bytes inside block of size 2 alloc'd ==31753==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31753==    0x40076f: getcipher (railfence.c:26) ==31753==    0x400977: main (main.c:14) ==31753==  ==31753== invalid read of size 1 ==31753==    @ 0x4c2e0f4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31753==    0x4ea7c7b: puts (ioputs.c:36) ==31753==    0x40098e: main (main.c:16) ==31753==  address 0x51fd138 0 bytes after block of size 8 alloc'd ==31753==    @ 0x4c2ab80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==31753==    0x400859: getcipher (railfence.c:41) ==31753==    0x400977: main (main.c:14) ==31753==  acegbdfh ==31753==  ==31753== heap summary: ==31753==     in use @ exit: 8 bytes in 1 blocks ==31753==   total heap usage: 4 allocs, 3 frees, 26 bytes allocated ==31753==  ==31753== leak summary: ==31753==    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 --leak-check=full see details of leaked memory ==31753==  ==31753== counts of detected , suppressed errors, rerun with: -v ==31753== error summary: 47 errors 9 contexts (suppressed: 0 0) 

try change line of code

arr = (char **) malloc ( rail*sizeof(char) ); 

with this

arr = (char **) malloc ( rail*sizeof(char*) ); 

Comments

Popular posts from this blog

android - Automated my builds -

how to proxy from https to http with lighttpd -

python - Flask migration error -