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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:48:28+00:00 2026-06-12T19:48:28+00:00

I am reading integers from a file, and when I try to grow my

  • 0

I am reading integers from a file, and when I try to grow my array, I am getting a segmentation fault on the second call to growMyArray(struct myArray), specifically at int *grownArray = malloc(arrayToGrow.maxCount * sizeof(int));:

struct myArray growMyArray(struct myArray arrayToGrow) {

    arrayToGrow.maxCount *= 2;

    int *grownArray = malloc(arrayToGrow.maxCount * sizeof(int));

    int i;
    for (i = 0; i < arrayToGrow.count; i++)
        grownArray[i] = arrayToGrow.numbers[i];

    free(arrayToGrow.numbers);

    arrayToGrow.numbers = grownArray;

    return arrayToGrow;
}

My structure:

typedef struct myArray {
    int count;
    int maxCount;
    int *numbers;
} myArray;

Reading from input redirection:

struct myArray getRandomNumbers() {

    struct myArray randomNumbers;
    randomNumbers.count = 0;
    randomNumbers.maxCount = DEFAULT_SIZE;
    randomNumbers.numbers = malloc(randomNumbers.maxCount * sizeof(int));

    while (scanf("%d", &randomNumbers.numbers[randomNumbers.count]) == 1) {

        randomNumbers.count++;

        if (randomNumbers.count > randomNumbers.maxCount)
            randomNumbers = growMyArray(randomNumbers);
    }

    return randomNumbers;
}

I find this particularly odd because growing the array always works the first time but never works the second time. I have used multiple values for DEFAULT_SIZE, ranging from 2 to 20000 on a set of test data of size 200000.

Is there an apparent reason why I am getting a segmentation fault on the second call to growMyArray, specifically at int *grownArray = malloc(arrayToGrow.maxCount * sizeof(int));?

  • 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-12T19:48:29+00:00Added an answer on June 12, 2026 at 7:48 pm

    You wrote past the end of the array.

    while (scanf("%d", &randomNumbers.numbers[randomNumbers.count]) == 1) {
    
        randomNumbers.count++;
    
        if (randomNumbers.count > randomNumbers.maxCount)
            randomNumbers = growMyArray(randomNumbers);
    }
    

    Because you use > in the test, the if only fires once randomNumbers.count = randomNumbers.maxCount + 1, i.e. the scanf writes to randomNumbers.numbers[randomNumbers.maxCount] which is past the end of the array.

    Therefore, change > to >= in the if statement there.

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

Sidebar

Related Questions

When I try to read integers from a file, the code ends up reading
Possible Duplicate: Java: Reading integers from a file into an array I want to
I am working on a program that requires reading in integers from a file
I am reading a huge number of Integers from a file, and at the
I am reading integers from a text file, giving them as input to a
Possible Duplicate: reading integers from binary file in python I've read the solution to
I am reading a file of integers. I want to save integers from each
The problem I have is reading in multiple lines of integers from a file
I'm reading 16-bit integers from a piece of hardware over the serial port. Using
I am having difficulties w/ writing and reading an array of objects from a

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.