2013-06-03 10 views
1

Я некоторое время боролся с этой ошибкой, и я не могу понять, что случилось. Вот код:открытие файла дает сбой утверждения sYSMALLOc

//the code for the function that is being called 
//charset is a const char[] consisting of 91 characters 
//charset_size is 91 
void set_sequence(char keyword[], int keyword_size){ 

    sequence = malloc(keyword_size); 

    int i = 0, j = 0; 

    for(i = 0; i < keyword_size; i++){ 

     for(j = 0; j < charset_size; j++){ 

      if(keyword[i] == charset[j]){ 

       sequence[i] = j; 

      } 

     } 

    } 

    sequence_size = keyword_size; 

} 

//the function call in main 
set_sequence("foo bar\n", 8); 

//there's supposed to be stuff done here with sequence that I haven't implemented yet 
free(sequence); //sequence is a global variable that I use the function to set 

FILE* dest = fopen("cipher", "w"); 

Причина я включил файл бит, потому что, когда он там, я получаю ошибки подтверждения, но когда закомментирована код работает абсолютно нормально (я не имею ничего после этой линии, потому что я был пытаясь определить проблему).

Я запустил код через memcheck valgrind --tool=memcheck ../bin/cipher valgrind, чтобы узнать, в чем проблема, но я не могу понять это. Вот вывод:

==10608== Memcheck, a memory error detector 
==10608== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. 
==10608== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info 
==10608== Command: ../bin/cipher -e testfile 
Program running in encrypt mode 
Open source file: success 
Allocate memory for raw_input: success 
Read source file: success 
Allocate memory for input: success 
input set-> freeing raw_input 
==10608== Invalid write of size 4 
==10608== at 0x80486DC: set_sequence (in /home/hugo/Programming/C++/Cipher 
==10608== by 0x8048A86: main (in /home/hugo/Programming/C++/Cipher/bin/cipher) 
==10608== Address 0x41f6688 is 0 bytes inside a block of size 8 alloc'd 
==10608== at 0x402BB7A: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) 
==10608== by 0x804868C: set_sequence (in /home/hugo/Programming/C++/Cipher/bin/cipher 
==10608== by 0x8048A86: main (in /home/hugo/Programming/C++/Cipher/bin/cipher) 
==10608== 
==10608== HEAP SUMMARY: 
==10608==  in use at exit: 704 bytes in 2 blocks 
==10608== total heap usage: 6 allocs, 4 frees, 1,793 bytes allocated 
==10608== 
==10608== LEAK SUMMARY: 
==10608== definitely lost: 0 bytes in 0 blocks 
==10608== indirectly lost: 0 bytes in 0 blocks 
==10608==  possibly lost: 0 bytes in 0 blocks 
==10608== still reachable: 704 bytes in 2 blocks 
==10608==   suppressed: 0 bytes in 0 blocks 
==10608== Rerun with --leak-check=full to see details of leaked memory 
==10608== 
==10608== For counts of detected and suppressed errors, rerun with: -v 
==10608== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 0 from 0) 

ответ

3

Вы не указываете последовательность. Является ли это массивом символов или массивом int? Если это массив int, ваш malloc ошибочен, ему нужно будет выделить keyword_size * sizeof(int) байтов

+0

Извините, я потратил некоторое время, чтобы ответить на ваш вопрос. Я действительно занят. Ваше решение сработало. Я полностью забыл, что символ «char» равен 1 байту, а «int» - 4 байта, поэтому, конечно, если это была плохая практика, он остался незамеченным, пока я не попытался сделать то же самое для 'int'. – Hugo

Смежные вопросы