3-dimensional array dynamic allocation

This is a discussion on 3-dimensional array dynamic allocation within the C Programming forums, part of the General Programming Boards category; Hello everybody! I'm trying to create a RGB picture of variable size using dynamic memory alloction. What I need to ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    3-dimensional array dynamic allocation

    Hello everybody!

    I'm trying to create a RGB picture of variable size using dynamic memory alloction. What I need to achieve is image[y][x][3] , where y is number of vertical pixels, x is number of horizontal pixels, 3 is number of colours: Red, Green, Blue.

    Here is my code:

    Code:
    int ***image;
    image = (int***)malloc(Ycount*sizeof(int**));
    	for (Ycount=0; Ycount<YSIZE; Ycount++)
    		{
    		image[Ycount] = (int**)malloc(Xcount*sizeof(int*));
    		for (Xcount=0; Xcount<XSIZE; Xcount++)
    			image[Ycount][Xcount] = (int*)malloc(3*sizeof(int));
    		}
    
    for(y = 0; y < Ycount; y++)     //putting data into the array
    			for(x = 0; x < Xcount; x++)
    			{
    				image[y][x][RED] = 255; // Value of red colour in the image
    				image[y][x][GREEN] = 0; // Value of green colour in the image
    				image[y][x][BLUE] = 0; // Value of blue colour in the image
    			}
    I think that memory allocation is successful, but something is wrong when putting data into the array is in process.

    I'd appreciate any help. Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Presuming Xcount and Ycount were initialized to correct values initially, your allocation should be fine (personally, I think it's better to use YSIZE and XSIZE in the malloc). What does "something is wrong when putting data in" mean?

    Couple of observations:
    1) You don't have to cast the return value of malloc() in C.
    2) if these are 0-255 RGB values, maybe you should use unsigned char instead of int. This will save you 75% of the memory usage (0-255 RGB values are one byte each).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Post the definitions of RED, BLUE, and GREEN.
    Are they placeholders for the 0-2 index range?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Code:
    image = (int***)malloc(Ycount*sizeof(int**));
    	for (Ycount=0; Ycount<YSIZE; Ycount++)
    		{
    		image[Ycount] = (int**)malloc(Xcount*sizeof(int*));
    		for (Xcount=0; Xcount<XSIZE; Xcount++)
    Since the red things are your loop variables (presumably garbage at this point), perhaps use the YSIZE and XSIZE respectively.

    Ditto for the loops which follow, although those seem to be accidentally correct for the moment.

    Also, see the FAQ on casting malloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2
    Post the definitions of RED, BLUE, and GREEN.
    Are they placeholders for the 0-2 index range?
    In RGB picture each pixel consists of 3 colours: Red, Green and Blue, and the value is between 0 and 255 for each colour. Say, I want a pixel with coordinate (50, 50) to be Red, so I will have

    Code:
    image[50][50][RED] = 255;
    image[50][50][GREEN] = 0;
    image[50][50][BLUE] = 0;
    What does "something is wrong when putting data in" mean?
    Thanks for the reply, I've modernized my code
    Code:
              int XSIZE, YSIZE;
              int ***image;
              int x,y;
              int Xcount, Ycount;
    
              printf("Image width:");
    	  scanf("%d", &Xcount);
    	  printf("Array height: ");
    	  scanf("%d",&Ycount);
    
    
            image = malloc(YSIZE*sizeof(int**));
    	for (YSIZE=0; YSIZE<Ycount; YSIZE++)
    		{
    		image[YSIZE] = malloc(XSIZE*sizeof(int*));
    		for (XSIZE=0; XSIZE<Xcount; XSIZE++)
    			image[YSIZE][XSIZE] = malloc(3*sizeof(int));
    		} 
    
    for(y = 0; y < YSIZE; y++)     //putting data into the array
    			for(x = 0; x < XSIZE; x++)
    			{
    				image[y][x][RED] = 255; // Value of red colour in the image
    				image[y][x][GREEN]= 100; // Value of green colour in the image
    				image[y][x][BLUE]= 100;// Value of blue colour
                            }
    The main problem is when I start the programm. It builds with no error, but when I try to debug it gives an error and points an arrow to the row image[Ycount][Xcount][RED] = 255;
    Last edited by alexiy; 05-03-2010 at 03:59 PM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay. A few basic things.

    Generally, all caps NAMES are used for defines, eg:
    Code:
    #define XSIZE 200
    which is what I was presuming those were before. Anyway, so when I said use that with malloc, I mean that you should not use the value which changes in a for loop:
    Code:
    	for (YSIZE=0; YSIZE<Ycount; YSIZE++)
    		{
    		image[YSIZE] = malloc(XSIZE*sizeof(int*));
    YSIZE and XSIZE are now going up and down, whereas Ycount and Xcount remain constant.

    Altho, because of how the for() is, for every loop after the first one, it will be the correct value. However, right now it looks like XSIZE has not been defined yet for the first iteration of the Y loop! In any case, it's better to use a constant value here, which would now be Xcount. Xcount does not change after it has been set. Make sense? You should do the same thing in the second loop here:
    Code:
    for(x = 0; x < XSIZE; x++)
    But that is not the exact cause of your problem. What is the "error" you are given (cut n' paste it here)?

    Also: have you defined RED, GREEN, and BLUE?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > image = malloc(YSIZE*sizeof(int**));
    > for (YSIZE=0; YSIZE<Ycount; YSIZE++)
    Fine, just blow a hole in your other foot then.

    It's the same problem rearranged - you're not allocating what you're trying to use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use a 2D array of structures? Use a 2D array of integers, and mask RGB (and hey, Alpha if you really want)? You don't really need 3Ds here unless you are just really bored.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 03:04 AM
  3. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 10:56 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21