Home > Software design >  I am getting error when i am giving input n more that 2 in this c program
I am getting error when i am giving input n more that 2 in this c program

Time:10-20

MY code :-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h> 

struct Geometry
{
    bool isrectangle, isquare, isline, iscircle;
    float x1, x2, y1, y2;
    float square_corner1, square_corner2, side;
    float center1, center2, radius;
    float rectangle_corner1, rectangle_corner2, width, height;
};


#define SCANF_FMT_CIRCLE    "%f %f %f"
#define SCANF_FMT_RECTANGLE "%f %f %f %f"
#define SCANF_FMT_SQUARE    "%f %f %f"
#define SCANF_FMT_LINE      "%f %f %f %f"


#define PRINTF_FMT_CIRCLE    "Circle with center at %f, %f and radius %f\n"
#define PRINTF_FMT_RECTANGLE "Rectangle with corner at (%f, %f) with width %f and height %f\n"
#define PRINTF_FMT_SQUARE    "Square with corner at (%f, %f) and side %f\n"
#define PRINTF_FMT_LINE      "Line from (%f, %f) to (%f, %f)\n"



void initRectangle(struct Geometry **object){
    scanf(SCANF_FMT_RECTANGLE, &((*object)->rectangle_corner1), &((*object)->rectangle_corner2), &((*object)->width), &((*object)->height));
    (*object)->isrectangle = true;
    (*object)->iscircle = false;
    (*object)->isquare = false;
    (*object)->isline = false;
}

void initSquare(struct Geometry **object){
    scanf(SCANF_FMT_SQUARE, &((*object)->square_corner1), &((*object)->square_corner2), &((*object)->side));
    (*object)->isrectangle = false;
    (*object)->iscircle = false;
    (*object)->isquare = true;
    (*object)->isline = false;
}
void initCircle(struct Geometry **object){
    scanf(SCANF_FMT_CIRCLE, &((*object)->center1), &((*object)->center2), &((*object)->radius));
    (*object)->isrectangle = false;
    (*object)->iscircle = true;
    (*object)->isquare = false;
    (*object)->isline = false;
}
void initLine(struct Geometry **object){
    scanf(SCANF_FMT_LINE, &((*object)->x1), &((*object)->x2), &((*object)->y1), &((*object)->y2));
    (*object)->isrectangle = false;
    (*object)->iscircle = false;
    (*object)->isquare = false;
    (*object)->isline = true;
}

void printGeometry(struct Geometry* object){
    if (object->iscircle == true){
        printf(PRINTF_FMT_CIRCLE, object->center1, object->center2, object->radius);
    }
    else if (object->isline == true){
        printf(PRINTF_FMT_LINE, object->x1, object->x2, object->y1, object->y2);
    }
    else if (object->isquare == true){
        printf(PRINTF_FMT_SQUARE, object->square_corner1, object->square_corner2, object->side);
    }
    else {
        printf(PRINTF_FMT_RECTANGLE, object->rectangle_corner1, object->rectangle_corner2, object->width, object->height);
    }
}

void freeGeometry(struct Geometry* object){
    free(object);
    object = NULL;
}


int main()
{
    int n;
    struct Geometry **object;
    scanf("%d", &n);
    printf("%d geometric items\n", n);

    object = malloc(sizeof(struct Geometry*)*n);

    for(int i = 0; i < n; i  )
    
    {
        object[i] = malloc(sizeof(struct Geometry *)*n);
        
        char objectType[40];
        scanf("%s", objectType);
        
        

        if(!strcmp(objectType, "Rectangle"))
        {
            initRectangle(&object[i]);
        }
        else if (!strcmp(objectType, "Square"))
        {
            initSquare(&object[i]);
        }
        else if(!strcmp(objectType, "Circle"))
        {
            initCircle(&object[i]);
        }
        else if(!strcmp(objectType, "Line"))
        {
            initLine(&object[i]);
        }
        else
        {
            printf("Unknown geometric type %s\n", objectType);
            exit(1);
        }

    }

    for(int i = 0; i < n; i  )
    {
        printGeometry(object[i]);
    }

    for(int i = 0; i < n; i  )
    {
        freeGeometry(object[i]);
    }

    free(object);
}

Input Format: 6 Line 0.739 0.053 0.380 0.383 Line 0.098 0.158 0.546 0.531 Square 0.120 0.707 0.346 Rectangle 0.769 0.041 0.995 0.859 Rectangle 0.671 0.520 0.246 0.226 Square 0.333 0.721 0.249

Output Format: 6 geometric items Line from (0.739000, 0.053000) to (0.380000, 0.383000) Line from (0.098000, 0.158000) to (0.546000, 0.531000) Square with corner at (0.120000, 0.707000) and side 0.346000 Rectangle with corner at (0.769000, 0.041000) with width 0.995000 and height 0.859000 Rectangle with corner at (0.671000, 0.520000) with width 0.246000 and height 0.226000 Square with corner at (0.333000, 0.721000) and side 0.249000

Error:- malloc(): corrupted top size Aborted (core dumped)

Please someone help me out.

CodePudding user response:

This memory allocation

object[i] = malloc(sizeof(struct Geometry *)*n);
                          ^^^^^^^^^^^^^^^^^

does not make a sense.

The expression object[i] has the type struct Geometry *. So it seems you need to write

object[i] = malloc(sizeof(struct Geometry)*n);
                          ^^^^^^^^^^^^^^^ 

That is you need to allocate an array of objects of the type struct Geometry.

It seems initially you should write

struct Geometry *object;

//...

object = malloc(sizeof(struct Geometry)*n);

That is you do not need to allocate an array of arrays but one array of objects of the type struct Geometry.

Also there is no great sense to pass pointers to objects by reference through pointers to them as you are doing

initRectangle(&object[i]);

The pointers themselves are not changed within the functions.

And within the function printGeometry you are trying to output uninitialized data members ,like

if (object->iscircle == true){
    printf(PRINTF_FMT_CIRCLE, object->center1, object->center2, object->radius);
}

CodePudding user response:

Here at Line 85

object = malloc(sizeof(struct Geometry*)*n);

You have to explicitly cast a pointer, in both main function as well as for loop. For example:

int main()
{ 
   
   object = (Geometry**)malloc(sizeof(struct Geometry*)*n);
   for(int i = 0; i < n; i  )
 
   { 
    //...
    object[i] = (Geometry*)malloc(n * sizeof *object[i]);
   }

Now, it won't throw any error in c Compiler.

  • Related