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

typedef struct my_struct {
    int *x;
    float *y;
    long int *z;
} my_struct;

void destroy(my_struct *s);

int init(my_struct **ms)
{
	my_struct *s = NULL;
	do{
	    s= (my_struct *) malloc(sizeof(my_struct));
	    if (s == NULL) {
	        printf("alloc my_struct fail\n");
	        break;
	    }
	
	    s->x = (int *) malloc(sizeof(int));
	    if (s->x == NULL) {
	        printf("alloc x fail\n");
	        break;
	    }
	
	    s->y = (float *) malloc(sizeof(float));
	    if (s->y == NULL) {
	        printf("alloc y fail\n");
	        break;
	    }
	
	    s->z = (long int*) malloc(sizeof(long int));
	    if (s->z == NULL) {
	        printf("alloc z fail\n");
	        break;
	    }
	    *ms = s;
		return 0;// success init		
	}while(0);
	

	//something wrong
	destroy(s);
	*ms = NULL;
    return -ENOMEM; 

}

void destroy(my_struct *s)
{
	if(s == NULL){
		return;
	}
	
    if (s->x != NULL) {
        free(s->x);
    }

    if (s->y != NULL) {
        free(s->y);
    }

    if (s->z != NULL) {
        free(s->z);
    }

    free(s);
}

int main(int argc, const char *argv[])
{
    my_struct *s = NULL;
    if (init(&s)) {
        printf("Init my_struct failed\n");
        return -ENOMEM;
    }

    // use my_struct here ...

    destroy(s);

    return 0;
}