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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:56:52+00:00 2026-05-30T17:56:52+00:00

I want to allocate a 2.9GB char array with database = (char*) malloc((2900 *

  • 0

I want to allocate a 2.9GB char array with

  database = (char*) malloc((2900 * 1000000 * sizeof(char)));

This gives an integer overflow warning and the malloc returns NULL. The
malloc parameter is of type size_t which according to documentation is of type
unsigned int.

So the max should be UINT_MAX which is at least 2.9GB. However, if
I try to allocate more than MAX_INT the malloc fails. Does this mean
size_t on my system is of type int? How do I check this? I looked through

/usr/include/stdlib.h 

and

./lib/gcc/x86_64-redhat-linux/4.1.1/include/stddef.h 

but
can’t find the definition of size_t. Thanks very much

  • 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-30T17:56:53+00:00Added an answer on May 30, 2026 at 5:56 pm

    There are two issues here.

    First, the overflow warning: both 2900 and 1000000 are of type int, so the result of multiplying them is also of type int. The result cannot be represented by a 32-bit signed integer, so it overflows. You need to cast one (or both) arguments to size_t to use unsigned arithmetic.

    (Or, you could move the sizeof(char) to be one of the first two terms, since its type is size_t, though you can also just remove the sizeof(char) since it is always 1.)

    Second, the maximum size that malloc can allocate depends both on the platform on which you are running and on the current state of the program. If there is insufficient contiguous address space left to satisfy the request, obviously the malloc will fail.

    Further, the platform on which you are running may have an upper limit on how large an object it can dynamically allocate. You’ll need to consult your platform’s documentation to find out what that upper limit is.

    size_t is certainly not int, because int is always signed and size_t is always unsigned.

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

Sidebar

Related Questions

I want to statically allocate the array. Look at the following code, this code
If I want to allocate a char array (in C) that is guaranteed to
I am allocating memory for array of pointers to structure through malloc and want
I want to allocate shared memory as a 2D array using IPC. I tried
I want to allocate dynamic an array of chars. So i ve the above
I am trying to allocate a array of char*'s in C. I know the
I want to allocate memory using malloc and check that it succeeded. something like:
i want to allocate a matrix. is this the only option: int** mat =
Please be nice.. I am a noob with this stuff. I want to allocate
I want to allocate rankings to users, based on a points field. Easy enough

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.