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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:58:03+00:00 2026-05-15T01:58:03+00:00

I’m making a program in C, and I’mm having some troubles with memory, I

  • 0

I’m making a program in C, and I’mm having some troubles with memory, I think.

So my problem is: I have 2 functions that return a struct. When I run only one function at a time I have no problem whatsoever. But when I run one after the other I always get an error when writting to the second struct.

Function struct item* ReadFileBIN(char *name) — reads a binary file.
struct tables* getMesasInfo(char* Filename) — reads a text file.

My code is this:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int numberOfTables=0;
int numberOfItems=0;

//struct tables* mesas;
//struct item* Menu;

typedef struct item{
    char nome[100];
    int id;
    float preco;
};
typedef struct tables{
    int id;
    int capacity;
    bool inUse;
};
struct tables* getMesasInfo(char* Filename){
    struct tables* mesas;
    char *c;
    int counter,numberOflines=0,temp=0;
    char *filename=Filename;
    FILE * G;
    G = fopen(filename,"r");
    if (G==NULL){
        printf("Cannot open file.\n");
    }
    else{
     while (!feof(G)){
     fscanf(G, "%s", &c);
     numberOflines++;
        }
    fclose(G);
    }  
    /* Memory allocate for input array */
    mesas = (struct tables *)malloc(numberOflines* sizeof(struct tables*));
    counter=0;
    G=fopen(filename,"r");
    while (!feof(G)){
        mesas[counter].id=counter;
        fscanf(G, "%d", &mesas[counter].capacity);
        mesas[counter].inUse= false;
        counter++;
    }
fclose(G);
numberOfTables = counter;
return mesas;
}

struct item* ReadFileBIN(char *name)
{
        int total=0;
        int counter;
        FILE *ptr_myfile;
        struct item my_record;
        struct item* Menu;
        ptr_myfile=fopen(name,"r");
        if (!ptr_myfile)
        {
            printf("Unable to open file!");
        }

        while (!feof(ptr_myfile)){
            fread(&my_record,sizeof(struct item),1,ptr_myfile);
            total=total+1;
        }
        numberOfItems=total-1;
        Menu = (struct item *)calloc(numberOfItems , sizeof(struct item));
        fseek(ptr_myfile, sizeof(struct item), SEEK_END);
        rewind(ptr_myfile);
        for ( counter=1; counter < total ; counter++)
        {
            fread(&my_record,sizeof(struct item),1,ptr_myfile);
            Menu[counter] = my_record;
            printf("Nome: %s\n",Menu[counter].nome);
            printf("ID: %d\n",Menu[counter].id);
            printf("Preco: %f\n",Menu[counter].preco);
        }
        fclose(ptr_myfile);
        return Menu;
}

int _tmain(int argc, _TCHAR* argv[])
{
    struct item* tt = ReadFileBIN("menu.dat");
    struct tables* t = getMesasInfo("Capacity.txt");
    getchar();
}**

the error that im getting is :

“Unhandled exception at 0x00411700 in test.exe: 0xC0000005: Access violation writing location 0x00000000.”

in “Menu[counter] = my_record;”

Thanks in advance.

  • 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-05-15T01:58:04+00:00Added an answer on May 15, 2026 at 1:58 am

    You have a problem in these lines:

        numberOfItems=total-1;
        Menu = (struct item *)calloc(numberOfItems , sizeof(struct item));
        fseek(ptr_myfile, sizeof(struct item), SEEK_END);
        rewind(ptr_myfile);
        for ( counter=1; counter < total ; counter++)
    

    First, you’re setting numberOfItems to one less than the total. This is incorrect. You don’t even need numberOfItems; since total has the number of lines in the file, the next line should really be Menu = (struct item*) calloc(total, sizeof(struct item));

    Second, you’re trying to use Menu as a one-based array in the for loop. C arrays are zero-based. You should have the for loop use for (counter = 0; counter < total; counter++).

    Finally, as Peter pointed out, in the first function you’re allocating the wrong size of object. You need to malloc numberOfLines*(sizeof(struct tables) (not sizeof(struct tables*).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.