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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:05:07+00:00 2026-06-11T20:05:07+00:00

I was working on an assignment (details in another question ). As part of

  • 0

I was working on an assignment (details in another question). As part of it, I was increasing the size of the array. And found that when I try to initialize an array:

int arr[2097152]; // 8MB

I got segmentation fault … I think its because I am trying to declare an array that too large? Then I found the way to get around this is to use malloc. But being new to C (mainly use JavaScript/Python/Java …). I get very confused with pointers and stuff …

I’ve declared an array 8MB:

int *arr = malloc (MBs * 1024 * 1024 / sizeof(int)); // MBs = 8

But now … how do I access it, or write to it? When I use it like arr am I getting the address, and if I use *arr I get the 1st element?

  • 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-11T20:05:08+00:00Added an answer on June 11, 2026 at 8:05 pm

    Use it like arr[index], just as if it were declared as an array. In C, the notation x[y] is exactly equivalent to *(x + y). This works in the case of an array because the array name is converted to a pointer to its first element.

    int *arr = malloc (MBs * 1024 * 1024 / sizeof(int));
    

    This is not a good approach (and doesn’t make it the size you want), because you don’t have the number of elements handy. You should declare it based on the number of elements, e.g.,

    #define ARR_LENGTH 2097152
    int *arr = malloc (ARR_LENGTH * sizeof *arr);
    

    You need to multiply by the element size because malloc’s argument is a byte count.

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

Sidebar

Related Questions

I'm working on an assignment of mine that a part of it requires me
I'm working on my assignment of PHP course. The problem that I don't understand
I am working on an assignment that deals with reading data from a text
I am working on a short java assignment that I have been set. The
I am working on a C++ assignment for class that wants me to overload
I am working on an assignment that is giving me grief. I am supposed
I am working on an assignment with polymorphism and have followed online tutorials that
I am working on an assignment that only allows use of low-level I/O (read(),
Currently I'm working on an assignment that requires that I create a program in
I have an assignment that I am working on, and I can't get 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.