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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:48:21+00:00 2026-06-15T22:48:21+00:00

I am getting a STATUS_ACCESS_VIOLATION at run time in this C program I am

  • 0

I am getting a STATUS_ACCESS_VIOLATION at run time in this C program I am creating, and I believe this is due to initialising structs all at once, and then using malloc() to increase their size, causing pointers to reference memory already in use. This is the function where this exception occurs:

void process_track(char filename[], struct track *tracks) {
    int i = 0;
    FILE* fp;
    int lines;
    lines = count_file_lines(filename);
    tracks = malloc(lines);
    fp = fopen(filename, "r");
    if (fp == NULL) {
        printf("\n\"%s\" File not found", filename);
        exit(1);
    } else {
        for (i = 0; i < lines; i++) {
            fscanf(fp, "%d %d %d %d\n", &tracks[i].number, &tracks[i].startnode, &tracks[i].endnode, &tracks[i].minutes);
        }
        for (i = 0; i < lines; i++) {
            printf("%d ", tracks[i].number);
            printf("%d ", tracks[i].startnode);
            printf("%d ", tracks[i].endnode);
            printf("%d\n", tracks[i].minutes);
        }
    }
}

Specifically, it happens at the following line:

fscanf(fp, "%d %d %d %d\n", &tracks[i].number, &tracks[i].startnode, &tracks[i].endnode, &tracks[i].minutes);

The function is called in the following function (case 3):

void get_file(char textfilename[], int optnumber) {
    struct node *nodes;
    struct track *tracks;
    struct course *courses;
    struct entrant *entrants;
    struct checkpointdata *checkpointdatas;
    char filename[20]; //Array of char for filename
    printf("Enter the name of the %s text file(with file extension) :", textfilename);
    scanf(" %[a-zA-Z._1-9]", &filename);
    switch (optnumber) {
        case 1:
            process_name(filename);
            break;
        case 2:
            process_node(filename, nodes);
            break;
        case 3:
            process_track(filename, tracks);
            break;
        case 4:
            process_course(filename, courses);
            break;
        case 5:
            process_entrant(filename, entrants);
            break;
        case 6:
            process_checkpointdata(filename, checkpointdatas);
            break;
    }

}

It reads the first line fine, but fails after that. Since the other “process_[data]” are very similar, I tried those too. Some would work fine, some would also come up with the exception, and some would retrieve garbage on output.

How would I work around this, or allocate memory so that it does not clash with pre-allocated memory (if this indeed is the problem)?

  • 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-15T22:48:23+00:00Added an answer on June 15, 2026 at 10:48 pm

    malloc() accepts the number of bytes to allocate, not the number of elements. The posted code is only allocating lines bytes:

    tracks = malloc(lines);
    

    which is not enough for an array of struct track and will result in the subsequent for loops writing to and reading from beyond the bounds of the allocated memory block. Pass the size of the element as well as the number of elements:

    tracks = malloc(lines * sizeof(*tracks));
    

    Note that any change made to tracks within the function is visible only to that function. As tracks is not actually used after the call to the function the argument could be replaced with a local variable.

    Remember for every malloc() there must be a free().

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

Sidebar

Related Questions

I am basically getting this error when trying to run a stored procedure from
I'm getting this exception from facebook SDK: Fatal error: Uncaught OAuthException: (#506) Duplicate status
I'm having an issue getting Appstats to work correctly. Using /appstats or /appstats/stats ends
I'm getting empty "data": [ ] from json facebook graph when i'm using read_stream
I'm following this guide and I am getting an error. Can anyone help me?
I am moving a site over to a different server to test. getting this
i am getting a serious issue Program received signal: EXC_BAD_ACCESS. Data Formatters temporarily unavailable,
I am getting this error in production.log on my new rails app at Hostgator:
I am getting this error after doing this code: <div id=fb-root></div> <script src=http://connect.facebook.net/en_US/all.js></script> <script>
I am creating a site using AngularJS that will allow users to create accounts

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.