Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Dynamically allocate memory inside a loop.

memory c++ c dynamically memory

Best Answer dargueta, 25 October 2014 - 07:42 PM

You can allocate it outside the loop. This will create array on the heap, which is capable of handling much larger array sizes (among other things). The only problem is that you have to remember to free it once you're done, otherwise the heap gets cluttered and your program might crash.

#include <stdlib.h>

float *array = malloc(2500000 * sizeof(float));

/* Do anything you need to in here, and make sure
you free `array` as soon as you're done with it. */

free(array);

/* `array` is gone, you can't use it again unless you reallocate
it again using malloc. */
Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts

Posted 25 October 2014 - 07:26 PM

Hello,

I need to have a float array inside a C code, which is like : 

float array[2500000];

This gives segmentation fault error, as you can guess.

 

I have a loop which writes to some value to 3 cells of this array, which is like : 

for(int i = 0; i < 2500000 ; i= i+3)
{
array[i] = 1;
array[i+1] = 2;
array[i + 2] = 3;
}

How can I allocate memory for my array inside that loop so that I avoid segmentation fault error?

Thanks.

 

 



#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 25 October 2014 - 07:42 PM   Best Answer

You can allocate it outside the loop. This will create array on the heap, which is capable of handling much larger array sizes (among other things). The only problem is that you have to remember to free it once you're done, otherwise the heap gets cluttered and your program might crash.

#include <stdlib.h>

float *array = malloc(2500000 * sizeof(float));

/* Do anything you need to in here, and make sure
you free `array` as soon as you're done with it. */

free(array);

/* `array` is gone, you can't use it again unless you reallocate
it again using malloc. */

Edited by dargueta, 25 October 2014 - 07:43 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts

Posted 25 October 2014 - 07:43 PM

Thanks, that works for me :)



#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 25 October 2014 - 07:44 PM

You saw my edit to the post, right? You must've replied while I was editing it.


Edited by dargueta, 25 October 2014 - 07:45 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#5 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts

Posted 25 October 2014 - 07:48 PM

Yeah, I just saw it, thanks for the explanation :)



#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 25 October 2014 - 07:53 PM

Okay, cool. :)


sudo rm -rf / && echo $'Sanitize your inputs!'





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download