#Creating an array of 1.7 million elements.

1 messages ยท Page 1 of 1 (latest)

severe umbra
#

Program reads the contents of a file. The very first number in the file is the amount of numbers the file has below.

I have to store all those numbers, but after creating an array of such size, the program terminates. Is there are any to work around this? Or am i doing it the wrong way?

#include <stdio.h>

#define BUFFER_SIZE 16

int main(void)
{
    FILE *file = fopen("sequence.txt", "r");
    char buffer[BUFFER_SIZE];

    fgets(buffer, BUFFER_SIZE, file);
    int number_amount;
    sscanf(buffer, "%d", &number_amount);

    printf("OK");  // <-- Only this OK gets printed out.
    int number_array[number_amount];
    printf("OK");

    int current_number;
    for (int index = 0; index < number_amount; index++)
    {
        fgets(buffer, BUFFER_SIZE, file);
        sscanf(buffer, "%d", &current_number);
        number_array[index] = current_number;
    }

    fclose(file);
    printf("OK");
    return 0;
}
fierce sunBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

uncut magnet
#

number_amount is 1.7 million?

severe umbra
#

yes

#

1788756

uncut magnet
#

That wont fit on the stack.

#

use malloc

#

int *number_array = malloc(number_amount);

severe umbra
uncut magnet
#

Yes

severe umbra
#

number_array[i]

#

oh okay

uncut magnet
#

Ideally you'd also error check it.

int *number_array = malloc(number_amount);
if (number_array == NULL) {
    printf("Could not allocate!\n");
    return 1;
}
severe umbra
#

it stopped at index=447472, current_number=97523

#

is it too much? ๐Ÿ˜ญ

uncut magnet
#

Stopped how?
segfault?

severe umbra
#

no

#

it just stopped

#

and even without printf if i do something right after the loop it doesnt get reached

uncut magnet
#

what is the output of echo $LASTEXITCODE

#

right after it stops

severe umbra
#
PS D:\System\Desktop\C\ege_problem_27> gcc main.c
PS D:\System\Desktop\C\ege_problem_27> .\a       
PS D:\System\Desktop\C\ege_problem_27> echo $LASTEXITCODE
-1073741819
#

๐Ÿ—ฃ๏ธ ๐Ÿ”ฅ

uncut magnet
#

That's just Windows' way of segfaulting

severe umbra
#

i see

uncut magnet
#

0xC0000005 is an "access violation"

severe umbra
#

so i guess storing 1 million numbers is a bad idea? ๐Ÿ’€

severe umbra
uncut magnet
#

Its probably not that that's causing the issue.
I'm not sure how well gdb works on Windows. Do you have it?

severe umbra
#
PS D:\System\Desktop\C\ege_problem_27> gdb
GNU gdb (GDB for MinGW-W64 x86_64, built by Brecht Sanders, r5) 14.1
Copyright (C) 2023 Free Software Foundation, Inc.

seems like i do i guess

uncut magnet
#

Ok, run gdb my.exe, then when in gdb, type run.
It should stop right when the segfault happens.

#

After that, type disassemble.

severe umbra
#

what to look for tho

#

or should i just sent the full dump in here

uncut magnet
#

Yea just send it here.
There should be an arrow at the instruction you are currently at.

severe umbra
uncut magnet
#

Hmm, why does everything look fine.
Give me a minute I'm definitely missing something.

severe umbra
#

should've written that in C# smh

uncut magnet
# severe umbra

The one thing I can tell you is that it crashes before the printf, so it doesn't actually stop at the
numbers shown here.

Try moving the printf before the assignment and see if anything stands out.

terse wharf
uncut magnet
#

Yea we fixed that, its segfaulting for a different reason now

terse wharf
severe umbra
#

oops!

scarlet vine
#

is it number_amount * sizeof(int)?

uncut magnet
scarlet vine
severe umbra
#

or should i always do sizeof(int)?

terse wharf
severe umbra
#

aight

uncut magnet
#

sizeof is more portable

severe umbra
#

thanks everyone!

#

!solved

fierce sunBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

drowsy whale
#

@severe umbra btw, you were using a VLA

#

!vla

fierce sunBOT
# drowsy whale !vla
What Is a VLA, and Why Is It "Bad"?

A Variable Length Array (VLA) is an array where the size is not constant and depends on a variable.

Example
int size = rand();
int vla[size]; // VLA of type int[size]
int not_vla[10]; // regular array of type int[10]
constexpr int size = 10;
int arr[size]; // also not a VLA, of type int[10]
Why Are VLAs "Bad"?

VLAs have poor compiler support and can lead to unsafe code. The core issue with VLAs is that the compiler doesn't know the size of the stack frame. Without warning flags like -Wvla, it can be easy to create a VLA by accident, even in C++ with some compilers.

Compiler Support

:white_check_mark: available since C99
:no_entry: not available in C++ at all
:no_entry: was never supported by MSVC
:warning: optional feature since C11
:warning: supported as non-standard extension by GCC, clang