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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:59:38+00:00 2026-05-17T01:59:38+00:00

in the following code when ran will produce a Segmentation Fault, due to a

  • 0

in the following code when ran will produce a Segmentation Fault, due to a FILE* being passed to fclose which contains no address (NULL).
I’m wondering why this is happening, the FILE* isn’t being used what so over.
The FILE* is named urandom and is passed to fclose in the main function.

Thanks

#include <stdio.h>
#include <stdlib.h>

struct property
{
    char *name;
    unsigned int value;
    unsigned int owner;
    unsigned int type;
};

struct player
{
    unsigned int id;
    unsigned int money;
    unsigned int position;
};

int rollDice(FILE *);
int amountOfLines(FILE *);
int createArrayOfPtrs(int ,void ***);
int makeArryOfPropertyPtrs(int ,struct property **);
int FillArryPropertyData(struct property **,int ,FILE *);
int splitBuffer(char *,unsigned int *,char **);
int bufferPropertyFile(FILE *,char **,int );
i    nt fillPropertyStruct(struct property *,unsigned int ,char *);

int main(void)
{   
    int linesInPropertyFile = 0;
    struct property **arrayForProperties = 0;

    //Open /dev/urandom for rollDice
    FILE *urandom = fopen("/dev/urandom","rb");
    FILE *propertyFile = fopen("/home/jordan/Documents/Programming/Monopoly Project/properties","rb");
    if(propertyFile == NULL || urandom == NULL)
    {
        puts("ERROR: error in opening file(s)");
        return 1;
    }
    linesInPropertyFile = amountOfLines(propertyFile);
    //DEBUG
    printf("%d is contained within \"linesInPropertyFile\"\n",linesInPropertyFile);

    if(createArrayOfPtrs(linesInPropertyFile,(void ***)&arrayForProperties))
    {
        puts("ERROR: error from createArrayOfPointers()");
        return 1;
    }
    //DEBUG
    printf("Outside Pointer: %p\n",arrayForProperties);

    if(makeArryOfPropertyPtrs(linesInPropertyFile,arrayForProperties))
    {
        puts("ERROR: error from createArrayOfPointersForProperties()");
        return 1;
    }
    if(FillArryPropertyData(arrayForProperties,linesInPropertyFile,propertyFile))
    {
        puts("ERROR: error from FillArryPropertyData()");
    }

    //Close FILE stream for /dev/urandom
    fclose(urandom);
    fclose(propertyFile);
    return 0;
}

int FillArryPropertyData(struct property **array,int amntOfProperties,FILE *fp)
{
    int bufferUsed = 100;
    int i = 0;
    int returnValue = 0;
    int returnValue2 = 0;
    unsigned int money = 0;
    char *name;
    char *buffer;

    rewind(fp);

    while(returnValue == 0)
    {
        buffer = malloc(bufferUsed);
        returnValue = bufferPropertyFile(fp,&buffer,bufferUsed);
        if(returnValue && returnValue != -1)
        {
            puts("ERROR: error from bufferPropertyFile()");
            return -1;
        }       
        if(returnValue == -1)
        {
            break;
        }
        if(buffer[0] != '\0')
        {
            returnValue2 = splitBuffer(buffer,&money,&name);
        }
        if(returnValue2)
        {
            puts("ERROR: error in splitBuffer()");
            return 1;
        }
        if(fillPropertyStruct(array[i],money,name))
        {
            puts("ERROR: error in fillPropertyStruct()");
            return 1;
        }
        money = 0;
        i++;
    }
    free(buffer);

    return 0;
}

int fillPropertyStruct(struct property *array,unsigned int money,char *name)
{
    int nameSize = 100;
    int i = 0;
    array->name = malloc(nameSize);
    array->value = money;
    while(1)
    {
        if(i >= nameSize)
        {
            void *tmp = realloc(array->name,nameSize * 2);
            nameSize *= 2;
            if(tmp)
            {
                array->name = tmp;
            }
            else
            {
                return -1;
            }
        }
        if(name[i] == '\0')
        {
            break;
        }
        array->name[i] = name[i];
        i++;
    }
    array->name[i] = '\0';

    return 0;
}
int splitBuffer(char *buffer,unsigned int *money,char **name)
{
    int i = 0;
    int j = 1;
    int nameSize = 100;

    *name = malloc(nameSize);
    while(1)
    {
        if(buffer[j] != '"')
        {
            (*name)[j-1] = buffer[j];
        }
        else
        {
            i++;
        }
        j++;
        if(i)
        {
            break;
        }
        if(j >= nameSize)
        {
            void *tmp = 0;
            tmp = realloc(*name,nameSize * 2);
            nameSize = nameSize * 2;
            if(tmp != NULL)
            {
                *name = tmp;
            }
            else
            {
                puts("ERROR: error in splitBuffer");
                return -1;
            }
        }
    }
    name[j-1] = '\0';

    while(buffer[j] != '$')
    {
        if(buffer[j] == '\0')
        {
                puts("ERROR: error in splitBuffer()");
            return -2;
        }
        j++;
    }
    j++;
    while(buffer[j] != '\0')
    {
        *money += (buffer[j] - '0');
        if(buffer[j+1] != '\0')
        {
            *money *= 10;
        }
        j++;
    }
    printf("BUFFER: %s\n",buffer);
    printf("NAME: %s\n",*name);
    printf("MONEY: %d\n",*money);
    return 0;
}

int bufferPropertyFile(FILE *fp,char **buffer,int i)
{   
    int j = (i - i);

    if(feof(fp))
    {
        //-1 Returned if EOF detected
        return -1;
    }
    char retr = 0;
    while(1)
    {
        if(j + 1 >= i)
        {
            void *tmp = realloc(*buffer,i * 2);
            if(tmp != NULL)
            {
                *buffer = tmp;
                i = i * 2;
            }
            else
            {
                puts("ERROR: error in bufferPropertyFile()");
                return -2;
            }
        }
        retr = fgetc(fp);
        if(retr == '\n' || feof(fp))
        {
            break;
        }
        (*buffer)[j] = retr;
        j++;
    }
    (*buffer)[j] = '\0';
    if(**buffer == '\0')
    {
        return -1;
    }
    return 0;
}




int rollDice(FILE *fp)
{
    int seed = fgetc(fp);
    srand(seed);
    return (rand() % 6) + 1;
}

int amountOfLines(FILE *file)
{
    int i = 0;
    int retr = 0;

    while(1)
    {
        retr = fgetc(file);
        if(retr == EOF)
        {
            break;
        }
        if(retr == '\n' )
        {
            i++;
        }
    }
    return i;
}
int createArrayOfPtrs(int numberOfPointers,void ***pointer)
{
    void *tmp = malloc(numberOfPointers * sizeof (tmp));
    if(tmp != NULL)
    {
        *pointer = tmp;
        //DEBUG
        printf("Pointer: %p\n",*pointer);
    }
    else
    {
        return 1;
    }
    return 0;
}

int makeArryOfPropertyPtrs(int numberOfPointers,struct property **pointer)
{
    int i = 0;
    void *tmp;
    for(i = 0;i < numberOfPointers;i++)
    {
        tmp = malloc(sizeof(struct property));
        if(tmp == NULL)
        {
            return 1;
        }
        pointer[i] = (struct property *)tmp;
    }
    return 0;
}
  • 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-17T01:59:39+00:00Added an answer on May 17, 2026 at 1:59 am

    here it givest an access violation in splitBuffer on this line:

    name[j-1]='\0';
    

    which probably should be

    (*name)[j-1]='\0';
    

    indeed that memory is not allocated anywhere, in other words, undefined behaviour, which indeed in your case might overwrite the urandom variable: both urandom and name are allocated on stack so depending on value of j it might write over urandom..

    apart from that, there might be more errors, the number and use of pointers/mallocs/reallocs and lack of frees is a bit scary

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

Sidebar

Related Questions

I ran the following code, but nothing came up on the console.... #include <stdio.h>
I ran across the following code in one of our in-house dlls and I
The following code will give a hard fail when run under Windows 7 32bit:
I downloaded the selenium-java-2.0a5.zip http://code.google.com/p/selenium/downloads/list and ran the following code: package org.openqa.selenium.example; import org.openqa.selenium.By;
I have the following snippet code: my $hostname = `host \`hostname\``; which this yields
I ran the instruments for my app (which contains a UITableView) and got the
Please consider the following code and comments: Console.WriteLine(1 / 0); // will not compile,
The following code will not run correctly in IE7 with the latest service packs
I ran the following two pieces of code on Windows XP (Code:Block, MinGW), and
Following code iterates through many data-rows, calcs some score per row and then sorts

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.