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

  • SEARCH
  • Home
  • 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 8164991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:26:13+00:00 2026-06-06T19:26:13+00:00

My program experiences a seg fault in the middle of the loop iteration. After

  • 0

My program experiences a seg fault in the middle of the loop iteration. After calling the function intermediate, the inter_value prints until inter_value[199][208], and then, I have a seg fault.
To make sure it is not out of bound access, I print the array inter_value first and the array prints without any problem.

Is this a symbol of running out of memory? The array ct and inter_value are created by malloc, key_byte is a static array.

D = 200;
K = 256;
for(j = 0; j < D; j++)
    for(i = 0; i < K; i++)
        printf("inter_value[%i][%i] = %i\n", j, i, inter_value[j][i]);

for(j = 0; j < D; j++) {
    for(i = 0; i < K; i++) {
        intermediate(ct[j][0], key_byte[i], &inter_value[j][i]);
        printf("inter_value[%i][%i] = %i\n", j, i, inter_value[j][i]);
        fflush(stdout);
    }
}
printf("rex\n");


for(j = 0; j < D; j++) {
    for(i = 0; i < K; i++) {
        hamming_dist(ct[j][0], inter_value[j][i], &h[j][i]);
    }
}

Function intermediate is here

void intermediate(unsigned char ct, unsigned char key_byte, unsigned char *inter_value){
    *inter_value = getSBoxInvert(ct^key_byte);
}

Edit 1: Declaration of arrays.

//initialize different intermediate values
inter_value = (unsigned char**)malloc(D * sizeof(unsigned char*));
if(inter_value == NULL){
    fprintf(stderr, "out of memory\n");
    return 0;
}
for(i = 0; i < D; i++){
    inter_value[i] = (unsigned char *)malloc(K * sizeof(unsigned char)); // this is fix to key size
    if(inter_value[i] == NULL){
        fprintf(stderr, "out of memory\n");
        return 0;
    }
}


//ct = malloc(row * sizeof(unsigned char*));
ct = (unsigned char**)malloc(D * sizeof(unsigned char*));
if(ct == NULL){
    fprintf(stderr, "out of memory\n");
    return 0;
}
for(i = 0; i < D; i++){
    //ct[i] = malloc(column * sizeof(unsigned char));
    ct[i] = (unsigned char *)malloc(column * sizeof(unsigned char));
    if(ct[i] == NULL){
        fprintf(stderr, "out of memory\n");
        return 0;
    }
}

unsigned char key_byte[256] = {0};

Edit 2: The print out before seg fault.

inter_value[199][233] = 214
inter_value[199][234] = 119
inter_value[199][2

Edit 3: gdb output (It seems it is pointing to another function)

Program received signal SIGSEGV, Segmentation fault.
0x0804a3f5 in hamming_dist (ct=31 ‘\037’, inter_value=203 ‘\313’, h=0x2) at cpa.cpp:53
53 *h = c;

Edit 4: After issue backtrace command from gdb…

#0 0x0804a3f5 in hamming_dist (ct=31 ‘\037’, inter_value=203 ‘\313’, h=0x2) at cpa.cpp:53

#1 0x0804aff5 in main (argc=3, argv=0xbffff2f4) at cpa.cpp:266

Edit 5: Add the hamming_dist function call and a printf call before it.

Edit 6: Initialization of h

int **h;
h = (unsigned int**)malloc(D * sizeof(unsigned int*));
if(h == NULL){
    fprintf(stderr, "out of memory\n");
    return 0;
}
for(i = 0; i < D; i++){
    h[i] = (unsigned int*)malloc(K * sizeof(unsigned int)); // this is fix to key size
    if(h[i] == NULL){
        fprintf(stderr, "out of memory\n");
        return 0;
    }
}

Edit 7: The hamming_dist function declaration.

void hamming_dist(unsigned char ct, unsigned char inter_value, int *h){
    int temp;
    temp = ct ^ inter_value;
    //then count No. of ones
    int c; // c accumulates the total bits set in v
    for (c = 0; temp; c++)
        temp &= temp - 1; // clear the least significant bit set

    *h = c;
}
  • 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-06T19:26:14+00:00Added an answer on June 6, 2026 at 7:26 pm

    Your seg fault is because &h[j][i] is 0x2 when passed as the value of the hamming_dist parameter h, which attempts to dereference it and store into it. This is apparently because of an earlier out-of-bounds store that overwrote h[j].

    Note that writing out of bounds in a malloced buffer can have effects that don’t show up until arbitrarily later in your program. They may not show up at all … until you release the code and some customer runs it with inputs that happen to trigger the bug.

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

Sidebar

Related Questions

I am building a program in c++ where the user can set a function
In my C# winforms program I want to make an export function that will
I am writing a C program. What I have seen from my earlier experiences
Here's the deal: I'm trying, as a learning experience, to convert a C program
Program that calculates and shows the value of (2 to the 10th power) This
program to output all 4 digit numbers where no prior digit is greater than
Program returns an error of, expected PWideChar instead of string procedure TForm1.Button1Click(Sender: TObject); var
Program runs once and it both throws data to the pipe and gets it
This program builds a dictionary out of a list based on what two index
My program is showing it is a leap year for every output. Please let

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.