C代码中的分段错误

时间:2012-09-09 23:03:59

标签: c segmentation-fault

以下代码使用在此代码末尾看到的for循环和字符数组打印我需要它打印的消息,但是在循环完成后发生分段错误,我无法弄清楚原因是什么。

#include "bmp.h"

void extractMessage(BMPfile bmpfile) 
{
void extractMessage(BMPfile bmpfile) 
{
    short index = 0;
char word[16];
unsigned char letter = 0;
unsigned char count = 0;
unsigned char temp;
int width = getWidth(bmpfile);
int height = getHeight(bmpfile);
printf("The image has %d x %d pixels\n", width, height);
for (int y = 0 ; y < height ; y++) 
{
    for (int x = 0 ; x < width ; x++) 
    {
        pixel p = getPixel(bmpfile, x, y);  /* read pixel */        
        temp = p.green & 0x01;
        letter = letter << 1;
        letter = letter + temp;         
        count += 1;  
        if(count == 8){
            word[index] = letter;
            index += 1;
            count = 0;} 
        if  (letter == '0')
        {
            break;
        }   
    }
}
    for (int x=0; x < 16; x++)
    {
    printf("%c",word[x]);
    }
}

此外,Valgrind提供以下信息,我不知道如何解释。

Memcheck, a memory error detector
==13094== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==13094== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==13094== Command: ./p1 picture.bmp
==13094== 
The image has 450 x 683 pixels
==13094== Invalid write of size 1
==13094==    at 0x400845: extractMessage (in /home/hauger3/hauger3/cs398/Lab2/p1)
==13094==    by 0x400C72: main (in /home/hauger3/hauger3/cs398/Lab2/p1)
==13094==  Address 0x7ff001000 is not stack'd, malloc'd or (recently) free'd
==13094== 
==13094== 
==13094== Process terminating with default action of signal 11 (SIGSEGV)
==13094==  Access not within mapped region at address 0x7FF001000
==13094==    at 0x400845: extractMessage (in /home/hauger3/hauger3/cs398/Lab2/p1)
==13094==    by 0x400C72: main (in /home/hauger3/hauger3/cs398/Lab2/p1)
==13094==  If you believe this happened as a result of a stack
==13094==  overflow in your program's main thread (unlikely but
==13094==  possible), you can try to increase the size of the
==13094==  main thread stack using the --main-stacksize= flag.
==13094==  The main thread stack size used in this run was 10485760.
==13094== 
==13094== HEAP SUMMARY:
==13094==     in use at exit: 568 bytes in 1 blocks
==13094==   total heap usage: 1 allocs, 0 frees, 568 bytes allocated
==13094== 
==13094== LEAK SUMMARY:
==13094==    definitely lost: 0 bytes in 0 blocks
==13094==    indirectly lost: 0 bytes in 0 blocks
==13094==      possibly lost: 0 bytes in 0 blocks
==13094==    still reachable: 568 bytes in 1 blocks
==13094==         suppressed: 0 bytes in 0 blocks
==13094== Rerun with --leak-check=full to see details of leaked memory
==13094== 
==13094== For counts of detected and suppressed errors, rerun with: -v
==13094== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
Segmentation fault
[hauger3@linux6 Lab2]$ valgrind --leak-check=full ./p1 picture.bmp 
==19793== Memcheck, a memory error detector
==19793== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==19793== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==19793== Command: ./p1 picture.bmp
==19793== 
The image has 450 x 683 pixels
==19793== Invalid write of size 1
==19793==    at 0x400845: extractMessage (in /home/hauger3/hauger3/cs398/Lab2/p1)
==19793==    by 0x400C72: main (in /home/hauger3/hauger3/cs398/Lab2/p1)
==19793==  Address 0x7ff001000 is not stack'd, malloc'd or (recently) free'd
==19793== 
==19793== 
==19793== Process terminating with default action of signal 11 (SIGSEGV)
==19793==  Access not within mapped region at address 0x7FF001000
==19793==    at 0x400845: extractMessage (in /home/hauger3/hauger3/cs398/Lab2/p1)
==19793==    by 0x400C72: main (in /home/hauger3/hauger3/cs398/Lab2/p1)
==19793==  If you believe this happened as a result of a stack
==19793==  overflow in your program's main thread (unlikely but
==19793==  possible), you can try to increase the size of the
==19793==  main thread stack using the --main-stacksize= flag.
==19793==  The main thread stack size used in this run was 10485760.
==19793== 
==19793== HEAP SUMMARY:
==19793==     in use at exit: 568 bytes in 1 blocks
==19793==   total heap usage: 1 allocs, 0 frees, 568 bytes allocated
==19793== 
==19793== LEAK SUMMARY:
==19793==    definitely lost: 0 bytes in 0 blocks
==19793==    indirectly lost: 0 bytes in 0 blocks
==19793==      possibly lost: 0 bytes in 0 blocks
==19793==    still reachable: 568 bytes in 1 blocks
==19793==         suppressed: 0 bytes in 0 blocks
==19793== Reachable blocks (those to which a pointer was found) are not shown.
==19793== To see them, rerun with: --leak-check=full --show-reachable=yes
==19793== 
==19793== For counts of detected and suppressed errors, rerun with: -v
==19793== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
Segmentation fault

2 个答案:

答案 0 :(得分:1)

当您尝试访问不属于您的程序的内存时,会发生分段错误。这通常通过以下方式实现:

  1. 通过取消引用指向不属于您的程序的内存位置的指针。这可以是NULL指针,已释放相关内存的指针,或未初始化的指针。
  2. 通过访问超出数组边界的数组元素。
  3. 因为你没有使用任何指针,我建议专注于第二项。检查索引是否始终为>= 0< 16。每次访问阵列前assert()都会对您有所帮助。

答案 1 :(得分:0)

这可能是因为在你的:

if(count == 8){
            word[index] = letter;
            index += 1;
            count = 0;
}

您的索引可能超出给定范围(&gt; = 0&amp;&amp;&lt; 16)