Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8506213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:31:57+00:00 2026-06-11T02:31:57+00:00

The following code prints the message that I need it to print using the

  • 0

The following code prints the message that I need it to print using the for loop and character array seen at the end of this code but a segmentation fault occurs after the loop is done and I cannot figure out what the cause is.

#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]);
    }
}

Additionally Valgrind gives the following information which I have no idea how to interpret.

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:31:59+00:00Added an answer on June 11, 2026 at 2:31 am

    A segmentation fault occurs when you try to access memory that does not belong to your program. This typically happens by the following means:

    1. By dereferencing a pointer that points to a memory location that does not belong to your program. This can be a NULL pointer, a pointer whose associated memory has already been freed, or a pointer that is not initialized.
    2. By accessing an array element beyond the boundaries of the array.

    Because you do not use any pointers, I suggest concentrating on the second item. Check whether the index is always >= 0 and < 16. An assert() before each access to the array will help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code, prints out Derived Base Base But I need every Derived object
I'm wondering why this code section prints out the following: print request.user.has_perm('bug_tracking.is_developer'): + str(request.user.has_perm('bug_tracking.is_developer'))
The following code prints [B@40545a60,[B@40545a60abc exp But I want to print abc , so
The following code prints out 10 . How can I make it print out
Following are two pieces of jquery code. First one prints a text message when
received help with. Basically, this code is written in Java but I need it
The following code prints (when invoking MyMethod): 0 0 0 1 I would expect
The following code prints null once. class MyClass { private static MyClass myClass =
For example the following code prints { key: 'b' } function myFunc(key, value) {
Why is the following code prints xxY? Shouldn't local variables live in the scope

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.