0

I have the following code, I am trying to store a market_order struct into my products.buys array

Here are what my structs look like

   typedef struct market_order {
    char type[5];
    int price;
    int quantity;
    char item[20];
    int traderId;
    int orderId;
    int number_of_orders;
    int orderCount;
} MarketOrder;

typedef struct products{
    int buy_levels;
    int sell_levels;
    struct market_order *buys[20];
    struct market_order *sells[20];
} Products;

struct products *products;

In my main method, in initalize memory space for my global products ptr. Like this

products = malloc(sizeof(struct products));

I then have another method that adds new elements to my products.buys keeping a counter for how many elements I have added so iteration is easier.

   void populate_buy_orders(int traderId, int orderId, char* item, int quantity, int price){
    MarketOrder* new_order = malloc(sizeof(struct market_order));
    if (new_order == NULL) {
        printf("Failed to allocate memory for new order\n");
        return;
    }

    new_order->traderId = traderId;
    new_order->orderId = orderId;
    new_order->quantity = quantity;
    new_order->price = price;
    new_order->number_of_orders = 1;
    new_order->orderCount = products->buy_levels;
    products->buys[products->buy_levels] = new_order;
    ++products->buy_levels;
}

I get a segmentation fault at this line

products->buys[products->buy_levels] = new_order;

What could be the issue?

  • Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives. This allows other people to easily test your program, by simply using copy&paste. – Andreas Wenzel May 11 '23 at 02:09
  • It always gives a segmentation fault – HyperCoderSuperion May 11 '23 at 02:19
  • I have deleted my previous comment, because it contained an error. I suggest that you [edit] the question to add the information that the stated line causes the segmentation fault the first time it is executed. Afterwards, you can delete your comment in which you reply to my now deleted comment. This will keep the comments section clean. – Andreas Wenzel May 11 '23 at 02:27
  • What is the value of `products->buy_levels` when the segmentation fault occurs? It should be between `0` and `19`, otherwise you will be accessing the array `products->buys` out of bounds. You should be able to answer this question by running your program in a [debugger](https://stackoverflow.com/q/25385173/12149471) and setting a breakpoint on the line on which the segmentation fault occurs. – Andreas Wenzel May 11 '23 at 02:39
  • OT: `MarketOrder* new_order = malloc(sizeof(struct market_order))` is poor practice. Write `MarketOrder* new_order = malloc(sizeof(MarketOrder))` or even better `MarketOrder* new_order = malloc(sizeof(*new_order))` – Jabberwocky May 11 '23 at 06:53

1 Answers1

2

malloc function only allocates specified amount of memory, it does not initialize it. So after allocating, your products->buy_levels member contains some random value. When you use this value as array index, it goes over the bounds and you get a segfault. You should either initialize your struct with default values after allocating, or use calloc instead for the default zero-initialization of the allocated buffer.

Ari0nhh
  • 5,720
  • 3
  • 28
  • 33