0

I'm quite new (and quite awful) at c. Im trying to write a recursion that puts binary tree data into a dynamic table. In the given data, natural numbers > 0 represent nodes, zeroes represent NULL nodes and each node is followed by \n; (prefix left to right notation). So a guy without children is given like: 1\n0\n0

Some fragments of programm might seem unnecessarily (like oindeks) - they will be used in the future.

when i input data im given free(): double free detected in tcache 2 and process is terminated. I suppose its rather a problem with realloc, cause free() is present only in main, but I truly dont know. Thank you in advance for any kind of help!

void Sczytaj(int Drzewiec[], int* a, int* b, int* c) {
   
   int Obecny, oindeks;
   scanf("%d", &Obecny);
   oindeks = *a;
   Drzewiec[oindeks] = Obecny;
   if (Obecny == 0) {
      *a += 1;
   }
   if (Obecny > 0) {
      *a += 1;
      *b += 1;
      *c += 2;
      Drzewiec = realloc(Drzewiec, sizeof(int) * (size_t) *c);  //should it be sizeof(int*)?
      Sczytaj(Drzewiec, &*a, &*b, &*c);
      Sczytaj(Drzewiec, &*a, &*b, &*c);
   }
}

int main(void) {
   printf("Wpisz dane:");
   int wielkość = 1, indeks = 0, indeks_bez_zera = 0;
   int* Drzewiec;
   Drzewiec = (int*)malloc(sizeof(int) * (size_t) wielkość);
   Sczytaj(Drzewiec, &indeks, &indeks_bez_zera, &wielkość);
   for (int i = 0; i < wielkość; i++) {
      printf("%d hej\n", Drzewiec[i]);
   }
   printf("\nOstatni indeks to: %d\nOstatni indeks_bez_zera to: %d\nWielkość to: %d", indeks, indeks_bez_zera, wielkość);
   free(Drzewiec);
}

I tried changing the function type to int, but i sorta need to call it twice for every non-empty node (2 possible children), and Im not sure whether i can do it with an inclusion of return.

Kiro99
  • 1
  • 1
  • 2
    Your reallocation of `Drzewiec` is a local copy of the pointer originally allocated. Change the function type from `void` so that you can return the reallocated pointer. – Weather Vane Feb 10 '23 at 16:34
  • Use this form to avoid questions about whether you are allocating the correct amount of space: `my_ptr = malloc(num_items * sizeof(*my_ptr));`. Analogously for `realloc()` and `calloc()`. Note in particular that the `*my_ptr` on the right corresponds to the `my_ptr` on the left (and do not overlook the difference in level of indirection). This gets you the correct item size, and that item size stays correct for the pointer to which you are assigning the result even if you change the type of that pointer. – John Bollinger Feb 10 '23 at 16:34
  • @WeatherVane Arent arrays always passed by reference? also,, is there a way to use return twice in a function? because i need to call Sczytaj twice (inside Sczytaj) in case of a full node – Kiro99 Feb 10 '23 at 16:50
  • The array argument is actually a pointer. Another way would to be use a double-pointer as `int **Drzewiec`. – Weather Vane Feb 10 '23 at 16:53
  • @JohnBollinger if i understand your comment correctly I should use: `Drzewiec = realloc(Drzewiec, sizeof(*Drzewiec) * (size_t) *c);` `Drzewiec = (int*)malloc(sizeof(*Drzewiec) * (size_t) wielkość);` right? this doesnt solve the issue unfortunetely – Kiro99 Feb 10 '23 at 16:53
  • Agreed, it does not resolve the issue you asked about. Refer to Weather Vane's comment in that regard. Rather, it addresses the comment in your code that expresses uncertainty about how to compute the correct size for your allocation. – John Bollinger Feb 10 '23 at 17:00
  • @JohnBollinger oh! forgot about it, it was a note to myself! Still, thank you very much! It's seems like a great practice that i didnt know bout – Kiro99 Feb 10 '23 at 17:04
  • @WeatherVane so should I define Drzewiec like this (int **Drzewiec) in function definition or in main? Pointers are just sometimes too much for my brain, despite idea seeming simple; – Kiro99 Feb 10 '23 at 17:09
  • Then stay with what you have but the function should be `int *Sczytaj(int Drzewiec[], int* a, int* b, int* c)` and you should `return Drzewiec;` and call the function like `Drzewiec = Sczytaj(Drzewiec, &indeks, &indeks_bez_zera, &wielkość);` – Weather Vane Feb 10 '23 at 17:15

0 Answers0