Hart22
Hart22

Reputation: 133

struct/constructor/pointer - C programming language

I have an issue on a problem involving creating a data structure that encapsulates an input buffer. My problem is currently with malloc but I feel it is probably actually some pointer defining that I am getting wrong

I have to create a constructor that takes an 10x10 8-bit two dimensional array and creates a copy of it for processing.

It must use the following function declaration and struct definition.

file - SP.h
typedef struct SP_struct    {
    uint8_t *   base_of_buffer;
    uint16_t    width;
    uint16_t    height;
    uint16_t    x0;
    uint16_t    y0;
    uint8_t     pixel_size;
} SP_struct;

SP_struct*             SP_create           (const unsigned char* s, int width, int height); // constructor

#include "pixel_sum.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main()
{
    uint16_t i = 0;
    unsigned char dummy_array[4][4] = {0,1,0,1,
                                        1,2,1,0,
                                        0,1,4,1,
                                        1,0,1,0};

    printf( "\nMain loop\n" );
    printf( "*dummy_array = %d\n",*dummy_array);

    struct SP_struct *SP_struct_ptr = SP_create(* dummy_array, 4, 4);
    struct SP_struct *SP_struct_ptr2 = SP_create(* dummy_array, 4, 4); // if this line is un commented it works fine
    printf("FINISHED MAIN LOOP");

}

SP_struct*     SP_create(const unsigned char* s, int width, int height){
    uint16_t i = 0;
    printf( "--------------SP_create\n" );
    printf( "*s = %d\n",*s); // This gets value
    printf( "s = %d\n",s); // This gets value

    // printf( "%d\n",width);
    // printf( "%d\n",height);

    SP_struct *p = malloc(width*height*sizeof(p->pixel_size));;
    printf( "p = %d\n",p); // This gets value
    printf( "&p = %d\n",&p); // This gets value
     p->x0 = 12;
     p->y0 = 12;
    //p 
    printf( "p->base_of_buffer = %d\n",p->base_of_buffer);
    printf( "&p->x0 = %d\n",&p->x0);
    printf( "p->x0 = %d\n",p->x0);
    printf( "p->y0 = %d\n",p->y0);
    for(i = 0; i< width*height; i++) {
        p->base_of_buffer[0+i] = s[i];
        //printf( "%d . ",i);
        printf( "%d\n",p->base_of_buffer[0+i]);
    }

    return p    ;
}

When I run it with the line for SP_struct_ptr2 commented out it runs fine and gives me a pointer and correct addresses..enter image description here

but when I comment back in the SP_struct_ptr2 line I get.. enter image description here

Has any got any ideas? I am using gcc 6.3.0 on windows 64-bit and these are the only files in compilation.

Upvotes: 2

Views: 67

Answers (1)

Afshin
Afshin

Reputation: 9173

You need to allocate SP_struct and base_of_buffer separately. Your code probably should be something like this:

SP_struct*     SP_create(const unsigned char* s, int width, int height){
    ...
    SP_struct *p = malloc(sizeof(*p));
    if (p) {
        p->pixel_size = PIXEL_SIZE;
        p->base_of_buffer = malloc(width*height*sizeof(p->pixel_size)*sizeof(*p->base_of_buffer));
        ...
    }
    return p;
}

Upvotes: 2

Related Questions