Dynamically allocating multiple big arrays in C












0















I'm writing a program in C on windows that launches 30 threads, each of which needs an array of int16_t.



The size is calculated before the thread function is called and in the example I'm working with it's around 250 millions. This is around 15GB, which should not be a problem, because I have 128GB ram available.



I've tried using both malloc and calloc inside the thread function, but over half of the allocations return NULL with errno set to 12 (enomem).



With a small number of threads (up to 3) it works fine though, same if I just use 1 thread and allocating an unreasonably big array.



My next attempt to solve this issue was to create an array of pointers in the main, allocate the arrays there and pass them as argument to the thread, same thing happened.



So from these results my best guess would be it can't allocate contiguous blocks of memory of that size, so I also tried allocating many smaller arrays, which obviously didn't work either. Is this an expected behaviour or am I doing something wrong?










share|improve this question

























  • 2 * 250 * 10^6 * 30 = 15*10^9 bytes. Sanity check your spec before you start coding away maybe.

    – Lundin
    Nov 22 '18 at 15:33











  • guess I should've mentioned I have 128 gb of ram available

    – incantation
    Nov 22 '18 at 15:37






  • 2





    I guess what you are actually asking is this: stackoverflow.com/questions/11891593/…

    – Lundin
    Nov 22 '18 at 15:42











  • Also a Minimal, Complete, and Verifiable example might help. Just the part that does the failing creation of the array of pointers in main.

    – Jabberwocky
    Nov 22 '18 at 15:44






  • 1





    Are you compiling as 32 bit or 64 bit code? A 32 bit process cannot address more than 4 GB of memory, even if your computer has 128Gb or ram.

    – Jabberwocky
    Nov 22 '18 at 15:48


















0















I'm writing a program in C on windows that launches 30 threads, each of which needs an array of int16_t.



The size is calculated before the thread function is called and in the example I'm working with it's around 250 millions. This is around 15GB, which should not be a problem, because I have 128GB ram available.



I've tried using both malloc and calloc inside the thread function, but over half of the allocations return NULL with errno set to 12 (enomem).



With a small number of threads (up to 3) it works fine though, same if I just use 1 thread and allocating an unreasonably big array.



My next attempt to solve this issue was to create an array of pointers in the main, allocate the arrays there and pass them as argument to the thread, same thing happened.



So from these results my best guess would be it can't allocate contiguous blocks of memory of that size, so I also tried allocating many smaller arrays, which obviously didn't work either. Is this an expected behaviour or am I doing something wrong?










share|improve this question

























  • 2 * 250 * 10^6 * 30 = 15*10^9 bytes. Sanity check your spec before you start coding away maybe.

    – Lundin
    Nov 22 '18 at 15:33











  • guess I should've mentioned I have 128 gb of ram available

    – incantation
    Nov 22 '18 at 15:37






  • 2





    I guess what you are actually asking is this: stackoverflow.com/questions/11891593/…

    – Lundin
    Nov 22 '18 at 15:42











  • Also a Minimal, Complete, and Verifiable example might help. Just the part that does the failing creation of the array of pointers in main.

    – Jabberwocky
    Nov 22 '18 at 15:44






  • 1





    Are you compiling as 32 bit or 64 bit code? A 32 bit process cannot address more than 4 GB of memory, even if your computer has 128Gb or ram.

    – Jabberwocky
    Nov 22 '18 at 15:48
















0












0








0








I'm writing a program in C on windows that launches 30 threads, each of which needs an array of int16_t.



The size is calculated before the thread function is called and in the example I'm working with it's around 250 millions. This is around 15GB, which should not be a problem, because I have 128GB ram available.



I've tried using both malloc and calloc inside the thread function, but over half of the allocations return NULL with errno set to 12 (enomem).



With a small number of threads (up to 3) it works fine though, same if I just use 1 thread and allocating an unreasonably big array.



My next attempt to solve this issue was to create an array of pointers in the main, allocate the arrays there and pass them as argument to the thread, same thing happened.



So from these results my best guess would be it can't allocate contiguous blocks of memory of that size, so I also tried allocating many smaller arrays, which obviously didn't work either. Is this an expected behaviour or am I doing something wrong?










share|improve this question
















I'm writing a program in C on windows that launches 30 threads, each of which needs an array of int16_t.



The size is calculated before the thread function is called and in the example I'm working with it's around 250 millions. This is around 15GB, which should not be a problem, because I have 128GB ram available.



I've tried using both malloc and calloc inside the thread function, but over half of the allocations return NULL with errno set to 12 (enomem).



With a small number of threads (up to 3) it works fine though, same if I just use 1 thread and allocating an unreasonably big array.



My next attempt to solve this issue was to create an array of pointers in the main, allocate the arrays there and pass them as argument to the thread, same thing happened.



So from these results my best guess would be it can't allocate contiguous blocks of memory of that size, so I also tried allocating many smaller arrays, which obviously didn't work either. Is this an expected behaviour or am I doing something wrong?







c arrays windows malloc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 16:06









Ring Ø

21.7k45283




21.7k45283










asked Nov 22 '18 at 15:29









incantationincantation

815




815













  • 2 * 250 * 10^6 * 30 = 15*10^9 bytes. Sanity check your spec before you start coding away maybe.

    – Lundin
    Nov 22 '18 at 15:33











  • guess I should've mentioned I have 128 gb of ram available

    – incantation
    Nov 22 '18 at 15:37






  • 2





    I guess what you are actually asking is this: stackoverflow.com/questions/11891593/…

    – Lundin
    Nov 22 '18 at 15:42











  • Also a Minimal, Complete, and Verifiable example might help. Just the part that does the failing creation of the array of pointers in main.

    – Jabberwocky
    Nov 22 '18 at 15:44






  • 1





    Are you compiling as 32 bit or 64 bit code? A 32 bit process cannot address more than 4 GB of memory, even if your computer has 128Gb or ram.

    – Jabberwocky
    Nov 22 '18 at 15:48





















  • 2 * 250 * 10^6 * 30 = 15*10^9 bytes. Sanity check your spec before you start coding away maybe.

    – Lundin
    Nov 22 '18 at 15:33











  • guess I should've mentioned I have 128 gb of ram available

    – incantation
    Nov 22 '18 at 15:37






  • 2





    I guess what you are actually asking is this: stackoverflow.com/questions/11891593/…

    – Lundin
    Nov 22 '18 at 15:42











  • Also a Minimal, Complete, and Verifiable example might help. Just the part that does the failing creation of the array of pointers in main.

    – Jabberwocky
    Nov 22 '18 at 15:44






  • 1





    Are you compiling as 32 bit or 64 bit code? A 32 bit process cannot address more than 4 GB of memory, even if your computer has 128Gb or ram.

    – Jabberwocky
    Nov 22 '18 at 15:48



















2 * 250 * 10^6 * 30 = 15*10^9 bytes. Sanity check your spec before you start coding away maybe.

– Lundin
Nov 22 '18 at 15:33





2 * 250 * 10^6 * 30 = 15*10^9 bytes. Sanity check your spec before you start coding away maybe.

– Lundin
Nov 22 '18 at 15:33













guess I should've mentioned I have 128 gb of ram available

– incantation
Nov 22 '18 at 15:37





guess I should've mentioned I have 128 gb of ram available

– incantation
Nov 22 '18 at 15:37




2




2





I guess what you are actually asking is this: stackoverflow.com/questions/11891593/…

– Lundin
Nov 22 '18 at 15:42





I guess what you are actually asking is this: stackoverflow.com/questions/11891593/…

– Lundin
Nov 22 '18 at 15:42













Also a Minimal, Complete, and Verifiable example might help. Just the part that does the failing creation of the array of pointers in main.

– Jabberwocky
Nov 22 '18 at 15:44





Also a Minimal, Complete, and Verifiable example might help. Just the part that does the failing creation of the array of pointers in main.

– Jabberwocky
Nov 22 '18 at 15:44




1




1





Are you compiling as 32 bit or 64 bit code? A 32 bit process cannot address more than 4 GB of memory, even if your computer has 128Gb or ram.

– Jabberwocky
Nov 22 '18 at 15:48







Are you compiling as 32 bit or 64 bit code? A 32 bit process cannot address more than 4 GB of memory, even if your computer has 128Gb or ram.

– Jabberwocky
Nov 22 '18 at 15:48














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434140%2fdynamically-allocating-multiple-big-arrays-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434140%2fdynamically-allocating-multiple-big-arrays-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()