6 Replies - 714 Views - Last Post: 09 September 2012 - 08:30 PM Rate Topic: -----

#1 camcamcam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-August 12

Running out of memory when creating an array

Posted 09 September 2012 - 03:15 PM

hi,

I am creating an array of 1000*1000*3 = 3000000 bytes but when i execute the program it crashes, but if i create the same array but in small dimensions it works perfectly, this is the code:


#include <stdio.h>
#include <stdlib.h>

typedef char color[3];

struct image {
       color picture[1000][1000];
};

int main()
    struct image image1;
    
    system("pause");
    return 0;
}



The struct has not really a big size, but it crashes anyways.

Any idea of what is going on?.

ps: i have 2GB of ram.

Is This A Good Question/Topic? 0
  • +

Replies To: Running out of memory when creating an array

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2982
  • View blog
  • Posts: 10,299
  • Joined: 08-August 08

Re: Running out of memory when creating an array

Posted 09 September 2012 - 03:43 PM

You're using the stack, which many not have that much memory available. You can either increase your stack size or create it on the heap.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4063
  • View blog
  • Posts: 12,545
  • Joined: 25-December 09

Re: Running out of memory when creating an array

Posted 09 September 2012 - 03:45 PM

You are trying to allocate this array on the stack which usually has much less memory available. If you want an array of this size I recommend you dynamic memory (malloc/free) to allocate this large array.

Also I recommend that you don't do this:
typedef char color[3];

You are hiding the fact that color is an array. Don't do that, you will just confuse yourself and anybody reading your code.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 sungchoiok  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 140
  • Joined: 18-July 10

Re: Running out of memory when creating an array

Posted 09 September 2012 - 07:05 PM

Here's some more info on Heap and Stack. Very interesting.

www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Running out of memory when creating an array

Posted 09 September 2012 - 07:41 PM

Rather than type defining a char array as a color, I recommend type defining a structure. This will avoid using multiple array subscript operators.

#include <stdio.h>

#define NUM_COLORS 2
typedef struct {
   char red, green, blue;
} Color;

int main() {
   Color colors[NUM_COLORS];
   
   for(int i=0; i<NUM_COLORS; i++) {
      colors[i].red = 0;
      colors[i].green = 0;
      colors[i].blue = 0;
   }
   
   return 0;
}


This post has been edited by jjl: 09 September 2012 - 07:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2982
  • View blog
  • Posts: 10,299
  • Joined: 08-August 08

Re: Running out of memory when creating an array

Posted 09 September 2012 - 08:15 PM

How many images will you have? If the number is fixed you might make them a global array. I'd also avoid making color an array, and instead use another struct. Maybe something like this:
#include <stdio.h>
#include <stdlib.h>

struct color {
	int8_t red, green, blue;
};

struct image {
	struct color picture[1000][1000];
};

void setpixel(int x, int y, struct image *img, struct color *clr) {
	img->picture[x][y].red = clr->red;
}

struct image images[5];

int main()
{
	struct color apixel;
	apixel.red = 0;
	apixel.green = 10;
	apixel.blue = 255;
	setpixel(0, 0, &images[0], &apixel);


Remember though that globals are bad because they're easy to get confused with local variables and altering the global variable directly can make debugging difficult, so I'd always pass the global as a reference.
Was This Post Helpful? 0
  • +
  • -

#7 camcamcam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-August 12

Re: Running out of memory when creating an array

Posted 09 September 2012 - 08:30 PM

Hi,

thanks for all the responses.

I was always using the Stack memory and have no idea...

This is the first time that i am tying to make a large array, to store a bitmap read from a BMP file. That way i load the bitmap in a large array. But i will have to load it into some array created dinamically (in the heap).

thanks a lot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1