0

I've recently started learning C and currently I'm working on a project which involves implementing a struct with two variables and I don't really know how to apparoach this.

The gist of it is I need to implement a struct which contains two variables, a pointer to an int array AND an int value which indicates the number of elements conatained within the array. The size of the array is declared upon the invocation of the constructor and is dependent on the input.

For the constructor I'm using a different function which recieves a string as input which is encoded into a decimal code. Also this function recieves another input which is a pointer to an int array (the pointer defined in the struct) and the problem is I'm using the malloc() function to allocate memory for it but I dont really understand how and when to use the free() function properly.

So, the questions are:

  1. When am I supposed to free the allocated memory? (assuming I need this struct for later use throughout the program's running time)
  2. What are the best ways to avoid memory leaks? What should you look out for?
RONOLULU
  • 727
  • 1
  • 4
  • 12
  • 1
    1. You free it when the data is no longer needed. 2. To avoid memory leaks free the memory. For a more precise answer you need to explain more specifically what the exact issue is. Usually by showing a code example and asking about that code. – kaylum Jun 15 '21 at 22:33
  • Although I strongly recommend that every `malloc` should have a corresponding `free` somewhere in your code, this might still be worth reading: [What REALLY happens when you don't free after malloc?](https://stackoverflow.com/q/654754/12149471) – Andreas Wenzel Jun 15 '21 at 23:00
  • If you may need the memory block until the end of your program, then you should only free it at the end of the program. Of course, you should not do this too often with too much memory, or you may run out of memory. If you are only dealing with a few megabytes of memory, it should not be a problem, but it may be a problem when dealing with several gigabytes of memory. – Andreas Wenzel Jun 15 '21 at 23:08

1 Answers1

0

It's unclear whether you're expected to manage the memory of the array inside, but this is functionally the setup you need for allocating the containing structure.

#include <malloc.h>
#include <stdio.h>

struct my_struct {
   size_t num_entries;
   int *array;
};

int main() {
    struct my_struct *storage = malloc(sizeof(struct my_struct));
    storage->num_entries = 4;

    storage->array = malloc(sizeof(int) * storage->num_entries);
    storage->array[0] = 1;
    storage->array[3] = 2;

    printf("allocated %ld entries\n", storage->num_entries);
    printf("entry #4 (index=3): %d\n", storage->array[3]);

    free(storage->array); /* MUST be first! */
    free(storage);
    storage = 0; /* safety to ensure you can't read the freed memory later */
}

if you're responsible for freeing the internal storage array, then you must free it first before freeing the containing memory.

The biggest key to memory management: only one part of the code at any time "owns" the memory in the pointer and is responsible for freeing it or passing it to something else that will.

Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69
  • There is no reason for the struct to be on the heap in this example. I don't know what OP needs, I just want to remind people that in c (and c++) structs can be stored wherever primitive types can be stored. – HAL9000 Jun 16 '21 at 00:00
  • @Wes Hardaker I'm not really sure what you mean by "manage the memory of the array inside". I am responsible for allocating space for the array and I'm responsible to take the input from the constructor and encode it to integer valus which are then stored in the array. The example code you wrote is pretty much what I'm aiming for (I think...). Again, the whole problem is I'm new to C and I'm still unsure of the best way to acheive my goal. – RONOLULU Jun 16 '21 at 06:21