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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:53:57+00:00 2026-06-12T05:53:57+00:00

In my C code I am allocating memory for 2d array double E[2000][2000]; but

  • 0

In my C code I am allocating memory for 2d array double E[2000][2000]; but when I run it gets a runtime error Segmentation fault(core dumped) and when I reduce the array size to somewhere around 900 then the code runs fine.

Why it is showing runtime error since double take 64 bits memory (IEEE standard) so the code should take approximately 32MB which is not much compared to the ram size.And if it is not supported in C then how should I proceed if my maximum number of data that I have to store is 4000000 each are floating point numbers.

  • 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-12T05:53:58+00:00Added an answer on June 12, 2026 at 5:53 am

    Are you declaring E as a local variable ? If so, you’re running out of stack memory.

    void func()
    {
        double E[2000][2000]; /// definitely an overflow
    }
    

    Use dynamic allocation:

    double* E = malloc(2000 * 2000 * sizeof(double));
    /// don't forget to "free(E);"  later
    

    Or if you need the 2D array, use a zig-zag:

    double** E = malloc(2000 * sizeof(double*));
    
    /* check that the memory is allocated */
    if(!E)
    {
        /* do something like exit(some_error_code);  to terminate your program*/
    }
    
    for(i = 0 ; i < 2000 ; i)
    {
          E[i] = malloc(2000 * sizeof(double));
    
         /* check that the memory for this row is allocated */
         if(!E[i])
         {
            /* do something like exit(some_error_code);  to terminate your program*/
         }
    }
    

    Then the deallocation is a little more complicated:

    for(i = 0 ; i < 2000 ; i)
    {
          free(E[i]);
    }
    
    free(E);
    

    P.S. If you want to keep all of you data in a continuous way, there’s a trick (code from Takuya Ooura’s FFT Package)

    double **alloc_2d(int n1, int n2)
    {
        double **ii, *i;
        int j;
    
        /* pointers to rows */
        ii = (double **) malloc(sizeof(double *) * n1);
    
        /* some error checking */
        alloc_error_check(ii);
    
        /* one big memory block */
        i = (double *) malloc(sizeof(double) * n1 * n2);
    
        /* some error checking */
        alloc_error_check(i);
    
        ii[0] = i;
        for (j = 1; j < n1; j++) {
            ii[j] = ii[j - 1] + n2;
        }
        return ii;
    }
    
    void free_2d(double **ii)
    {
        free(ii[0]);
        free(ii);
    }
    

    The you just call

    double** E = alloc2d(2000, 2000);
    

    and

    free_2d(E);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When allocating memory for the 2-dimensional array using malloc() ,segmentation fault occurs when the
I am allocating memory for array of pointers to structure through malloc and want
I'm having some issues in allocating memory for an array dynamically in C++ within
I wanted to wrap a small C++ code allocating an array with ctypes and
Im allocating memory to a double pointer in another function, therefore I need to
everyone. I'm having a memory allocation error using ctypes and a C code. I'm
Here I'm gettimg memory allocation problem at activity indicator and my code is: -
I want to measure time memory allocation using this code: long AForMemory = DateTime.Now.Ticks;
I' having a problem allocating a structure in a function. Here is the code(I'm
Code: String message = MessageFormat.format(error {0},e); E.g. message: java.text.ParseException: Unparseable date: sdf sf sa

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.