0
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int Init_Arr(int N,int X,int *arr);

void Add_elem(int N,int *arr);

void Del_elem(int N,int *arr);

void Reinit_arr(int N,int *arr);

int main(){
  int N,X,*arr;
  printf("How big array do you want?:");
  scanf("%d",&N);
  arr=(int *)malloc(sizeof(N));
  printf("\nHow much elements do you want in your array?:");
  scanf("%d",&X);
  Init_Arr(N,X,arr);
  Add_elem(N,arr);
  Del_elem(N,arr);
  Reinit_arr(N,arr);
  return 0;
}
int Init_Arr(int N,int X,int *arr){
  for (int i=0;i<N;i++){
    arr[i]=0;
  }
  for(int i=0;i<X;i++){
    arr[i]=rand()%10+1;
  }
  for(int i=0;i<N;i++){
    printf("%d",arr[i]);
  }
  printf("\nArray is initialised!\n");
  return 0;
}

void Add_elem(int N,int *arr){
  int add;
  int help;
  int i=0;
  printf("\nWhat element do you want to add?:");
  scanf("%d",&add);
  while(arr[i]!=0){
    i++;
  }
  arr[i]=add;
  for(int i=0;i<N;i++){
    printf("%d",arr[i]);
  }
  printf("\nElement was added to array!\n");
}
void Del_elem(int N,int *arr){
  int del;
  printf("\nWhat element do you want to delete from array?:");
  scanf("%d",&del);
  arr[del]=0;
  for(int i=0;i<N;i++){
    printf("%d",arr[i]);
  }
  printf("\nElement was deleted from array!\n");
}
void Reinit_arr(int N,int *arr){
  int *copyarr;
  int D;
  copyarr=(int *)realloc(sizeof(N));
  for(int i=0;i<N;i++){
    copyarr[i]=arr[i];
  }
  printf("How big array now do you want?:");
  scanf("%d",&D);
  arr=(int*)realloc(arr,sizeof(D));
  for(int i=0;i<N;i++){
    arr[i]=copyarr[i];
  }
  for(int i=N;i<D;i++){
    arr[i]=0;
  }
  for(int i=0;i<D;i++){
    printf("%d",arr[i]);
  }
  printf("\nArray was reinitialised\n");
}

Well,this program should be working in following way:initiate a dynamic string, fill it with X random elements, then add any element to the end of positive numbers string, delete any element(just setting it to 0) of array and the problem function:reallocate memory and make the string bigger, with the inside copied. the problem is that it just says "Aborted" at the function

Reinit_arr(N,arr);

GDB said that after trying this function the program returned the following:raise.c: no such file or directory. can you help me with it?

  • `realloc(sizeof(N));` This is missing a parameter and is unlikely to be compilable at all. Please provide real code that can be compiled and shows the problem. Don't make up some code that is only slightly simmilar to your code. Show the real thing instead. – Gerhardh May 17 '21 at 18:35
  • yes,this is the thing. i meant ```malloc``` but thanks for showing me my mistake –  May 17 '21 at 18:37
  • You got your error with that code? Then you forgot to inspect and fix all compiler warnings before running your program. That should be a mandatory first step whenever you compile any program. If you did not get a harsh warning from your compiler, you should clearly crank up the warning level. – Gerhardh May 17 '21 at 18:39
  • Gerhardh, there were no compiler warnings,unfortunately –  May 17 '21 at 18:44
  • For GCC you can increase warning level with options `-Wall -Wextra` – Gerhardh May 17 '21 at 18:45

1 Answers1

0

The lines

arr=(int *)malloc(sizeof(N));
copyarr=(int *)realloc(sizeof(N));
arr=(int*)realloc(arr,sizeof(D));

are bad. You are allocating only one element. It seems you should allocate N and D elements.

Also the 2nd line in above specifies only one argument to realloc() while it expects two arguments. It looks like you meant malloc().

Finally, casting results of malloc() family is considered as a bad practice.

In conclusion, the lines should be:

arr=malloc(sizeof(*arr)*N);
copyarr=malloc(sizeof(*copyarr)*N);
arr=realloc(arr,sizeof(*arr)*D);
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • thanks for the answer, using your suggestion really helped, because the program went to the reinitializing array. but a new one appeared: ```free():double free detected in tcache 2``` –  May 17 '21 at 18:45
  • The function `Reinit_arr` writes to the argument `arr`, but it is local to the function. You should have `Reinit_arr` tell the new value of `arr` by some mean. It may be by returning the new pointer or by taking a pointer to the pointer to update. – MikeCAT May 17 '21 at 18:54
  • i fixed this thing. i just changed all the ```arr``` inside ```Reinit_arr``` to ```array``` but still entered ```arr``` in the call of it. then it worked but the error with free didn't go. i decided to change ```free(arr)``` to ```free(*arr)``` and however compiler said this was a warning the program worked fully –  May 17 '21 at 19:12