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

  • Home
  • SEARCH
  • 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 7624181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:53:46+00:00 2026-05-31T04:53:46+00:00

Well, I always think that if I call malloc function, I assign specific amount

  • 0

Well, I always think that if I call malloc function, I assign specific amount of memory, but, I’ve just realised that if I write:

int* a = (int*)malloc(sizeof(int) * 2);

I can assign a value to a[4] or any another index, I though that, in this case, I could only assign to a[0] or a[1]. What’s the concept error I have?

  • 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-31T04:53:47+00:00Added an answer on May 31, 2026 at 4:53 am

    When you write a[4], it is the same as writing *(a + 4). Since the compiler doesn’t know how much memory is allocated at the address that a points to, it will happily let you address the memory.

    However, the memory located there could be anything – it could be another variable used by your program, part of the stack, or just out of the bounds of your program. Accessing outside the allocated space in this way is likely to (at best) produce a segmentation fault or (at worst) introduce a security hole by overwriting other parts of your program.

    You are correct in that you can only assign to a[0] or a[1] safely, but the C compiler will let you assign outside that bounds (because it doesn’t know any different).

    It is not safe to do a[4] in your example.


    Also, it is better not to cast the result of malloc – see this answer

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

Sidebar

Related Questions

Well, the Endianness theme was always a little bit confusing to me, but i
Well here is the thing, I have MainForm that call OrderForm.Show(). Now I want
I am just a bit curious about the new function std::move() that has just
Better yet, how can I make My Computer always open in Explorer as well?
Well... simple question, right? But with no so simple answers. In firefox i use
Well, it seems simple enough, but I can't find a way to add a
Well this is incredibly frustrating. After being nagged by Rails that I need to
Is the return value of this function well defined by the C standard ?
I mostly develop using C#, but I think this question might be suitable for
So I just got back for the ACM Programing competition and did pretty well

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.