0

I am trying to allocate memory to a struct function. However, I am not sure how to do it in 2D arrays. The question asks me to allocate memory to initialized rows and columns and if successful, it should return a pointer to a newly-allocated pointer, otherwise return NULL.

typedef struct 
{ 
uint8_t** pixels;
unsigned int rows;
unsigned int cols;
} img_t;

/* A type for returning status codes */
typedef enum 
{
   IMG_OK,
   IMG_BADINPUT,
   IMG_BADARRAY,
   IMG_BADCOL,
   IMG_BADROW,
   IMG_NOTFOUND
 } img_result_t;



   /* task 01 */

   // Create a new img_t with initial size rows and cols. If successful
  // (i.e. memory allocation succeeds), returns a pointer to a
 // newly-allocated img_t.  If unsuccessful, returns a null pointer.
 img_t* img_create(unsigned int rows, unsigned int cols)
 {
  img_t.rows = 640;
  img_t.cols = 480;
  double **A = malloc(img_t.rows * sizeof(double*));
  for(int i = 0; i < img_t.rows; i++)
  {
    A[i] = malloc(img_t.cols * sizeof(double));
  }
  // How do I check whether or not the memory has been assigned or not 
  //as the question demands?
  
 }

INPUT: the number of rows and cols of the desired array. OUTPUT: img_create() returns the POINTER to a new instance of data structure img_t. You may initialize the values in your pixels to 0. BEHAVIOUR: if malloc() fails (i.e. returns NULL), img_create() returns NULL

Can someone help me with it?

  • Sigh. That template shows the instructor/author is asking for a two-dimensional array formed using pointers to pointers. That is a bad method. – Eric Postpischil Jul 11 '21 at 22:58
  • and why is that? – Harsimar Kaur Jul 11 '21 at 23:02
  • If the duplicate post does not answer your question then you need to be more specific in describing what you have difficulty with. – kaylum Jul 11 '21 at 23:03
  • Because using pointers-to-pointers is wasteful of memory and inefficient for the hardware. Modern processors typically can do address calculations into one memory block more easily than they can do the pointer loads for the pointer-to-pointer method, and the pointer-to-pointer method also interferes with compiler optimization because the compiler cannot know where any of the pointers point, whereas it does understand two-dimensional array layouts. – Eric Postpischil Jul 11 '21 at 23:10
  • The way the author expects you to do this is: (1) Allocate memory for an `img_t` and assign its address to a pointer. (2) Fill in the `rows` and `cols` members of that `img_t`. (3) Allocate memory for `rows` pointers to `uint8_t` (e.g., if `rows` is 3, allocate memory for 3 `uint8_t *`) and assign its address to the `pixels` member. (4) For each value of `i` from 0 to `row-1`, allocate memory for `cols` `uint8_t` and assign its address to `pixels[i]`. (5) If any error occurs during this (such as memory not being available), free all the previous memory and return `NULL`… – Eric Postpischil Jul 11 '21 at 23:25
  • … Otherwise, if there is no error, return that first pointer. This should be enough to get you going on the assignment. If not, you have not learned the preliminary things you need to know. – Eric Postpischil Jul 11 '21 at 23:26

0 Answers0