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 9170725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:03:57+00:00 2026-06-17T16:03:57+00:00

Probably just another silly pointer issue from a C newbie. Couldn’t figure this one

  • 0

Probably just another silly pointer issue from a C newbie. Couldn’t figure this one out though. It seems that somehow my stack frame is corrupted. The assignment seems mostly irrelevant, but it’s a fairly basic I/O exercises. Attempting to read in an array of structures with a single read (cannot use advanced I/O functions such as fread()).

#include "A2_Phase2.h"

void read_directory(Cdir directory[], int cnt) 
{
    int fd;
    char filename[] = "RandomStructDir.bin";

    fd = open(filename, O_RDONLY, S_IRWXU);
    if (fd < 0)
        perror(strcat(filename, " failed to open."));

    if (read(fd, &(directory[0].code[0]), sizeof(Cdir) * cnt) < 0) {
        perror(strcat(filename, " could not be accessed."));
    }

    close(fd);
}

int binary_search(Cdir directory[], char *key, int l, int r) {

    int mid = (int) r / 2;

    if (strncmp(key, directory[mid].code, 3) < 0)
        return binary_search(directory, key, l, mid - 1);
    else if (strncmp(key, directory[mid].code, 3) > 0)
        return binary_search(directory, key, mid + 1, r);
    else
        return mid;
}

int main(int argc, char *argv[]) 
{
    int COUNTRY_COUNT = atoi(argv[1]);
    printf("%d", COUNTRY_COUNT);

    Cdir *directory = (Cdir *) malloc(sizeof(Cdir) * COUNTRY_COUNT);
    read_directory(directory, COUNTRY_COUNT);
    binary_search(directory, "ZWE", 0, 238);
    free(directory);
}

I receive this error via GDB:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400940 in binary_search (
    directory=<error reading variable: Cannot access memory at address 0x7fffff7feff8>, 
    key=<error reading variable: Cannot access memory at address 0x7fffff7feff0>, l=<error reading variable: Cannot access memory at address 0x7fffff7fefec>, 
    r=<error reading variable: Cannot access memory at address 0x7fffff7fefe8>)
    at A2_Phase2.c:19
19  int binary_search(Cdir directory[], char *key, int l, int r) { 

Thanks!

  • 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-17T16:04:02+00:00Added an answer on June 17, 2026 at 4:04 pm
    int COUNTRY_COUNT = atoi(argv[1]);
    

    reads the number of countries as an argument to the program but you later hard-code the assumption that this is >= 238 when you call

    binary_search(directory, "ZWE", 0, 238);
    

    Can you try

    binary_search(directory, "ZWE", 0, COUNTRY_COUNT-1);
    

    instead? There are also a few errors in your binary_search function which could be re-written as

    int binary_search(Cdir directory[], const char *key, int l, int r)
    {
        int mid = (r + l) / 2;
        int cmp = strncmp(key, directory[mid].code, 3);
        if (l >= r) {
            if (cmp == 0)
                return l;
            return -1;
        }
        if (cmp < 0) 
            return binary_search(directory, key, l, mid - 1);
        else if (cmp > 0)
            return binary_search(directory, key, mid + 1, r);
        else
            return mid;
    }
    

    The main changes are

    • calculation of mid takes account of l as well as r
    • (as noted by Kirilenko) recognise that its possible to find no match. return -1 in this case
    • reduce number of calls to strcmp. Very minor but it makes the code clearer to me and will improve performance of searches

    Less importantly, there are some stylistic issues that make your code hard to read

    • Masses of unnecessary whitespace inside functions
    • Use of upper case (e.g. COUNTRY_COUNT) for variables is unusual. All upper case is often informally reserved for defines with variables using lower or camelCase
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This will probably be just another unsolved thread but i'll fill in some info
I couldn't find anything on this, but probably its just because I don't know
What would make one table substantially slower than another? Probably easiest to just illustrate:
I probably just haven't thought this through, or perhaps I'm simply unaware of an
This is probably just wishful thinking... Is there any way to check to see
This is probably just me being stupid somehow or the other, but I am
Im having a weird issue here. Its probably just something stupid but I dont
This is probably me just remembering things completely backwards, but I'd like to know
Probably simple question and I'm just missing something, but I'm stuck out of ideas.
Ok this is probably just me not knowing enough about php but here it

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.