1

The program takes some txt input from "numbers.txt" file. It first counts the amount of all the numbers (freqCount)in that text file, than reads the file again and with the use of malloc it creates two arrays A and B , of which both have size equal to the amount of all the numbers in the text file. So far so good.

Now i want to increase the size of the array A, so that I can put "freqCount" more arguments in it. In the freqRepeat function I created, there is a function increaseSize which takes that same array A, and uses realloc to add 2*freqCount more arguments in it.

After calling the mentioned function increaseSize there is a problem, because only part of the arguments remain unchanged, and there are few arguments that become some huge number. This is a major issue. Can anyone please provide me with some help ? thanks

ps. I include the expemplary text file input at the end of the code.

#include <stdio.h>
#include <stdlib.h>
int read_ints(const char *file_name, int *result);
int *scanFreq(const char *file_name, int *A, int *B, int *resultTab);
int freqRepeat(int *A, int *B, int freqCount);
int *increaseSize(int *A, int freqCount);
void calcmalc(int freqCount);
int *nextArray(int *A, int *B, int freqCount, int freqStart);

int main()
{
  int result = 0;
  int resultTab = 0;
  int freqCount;
  freqCount = read_ints("numbers.txt", &result);
  printf("freqCount is %d", freqCount);
  int *A = (int *)malloc(freqCount * sizeof(int));
  int *B = (int *)malloc(freqCount * sizeof(int));
  scanFreq("numbers.txt", A, B, &resultTab);
  freqRepeat(A, B, freqCount);

}
int read_ints(const char *file_name, int *result)
{
  FILE *file = fopen("numbers.txt", "r");
  int i = 0;
  int n = 0; //row number//

  if (file == NULL)
  {
    printf("unable to open file %s", file_name);
  }

  while (fscanf(file, "%d", &i) == 1)
  {
    n++;
    printf("%d\n ", i);
    *result += i;
    printf("\n we are at row nr. %d sum of this number and all numbers before is: %d\n", n, *result);
  }
  fclose(file);
  return n;
}
int *scanFreq(const char *file_name, int *A, int *B, int *resultTab)
{
  FILE *file = fopen("numbers.txt", "r");
  int i = 0;
  int n = 0; //row number//

  if (file == NULL)
  {
    printf("unable to open file %s", file_name);
  }

  while (fscanf(file, "%d", &i) == 1)
  {
    n++;
    *resultTab += i;
    B[n] = i;
    A[n] = *resultTab;
  }
  fclose(file);
  return 0;
}

int freqRepeat(int *A, int *B, int freqCount)
{
  int lastFrequency;

  lastFrequency = freqCount;
   freqCount = freqCount + freqCount;
  A = increaseSize(A, freqCount);

  printf("\n\nwcis enter\n\n");
  getchar();

  for (int i = 1; i < 15; i++)
  {
    printf("array argument after increasing array size %d \n", A[i]);

    // why some of the arguments have been changed ????????
    }
return 0;
}
int *increaseSize(int *A, int freqCount)
{

  return realloc(A, 2 * sizeof(int));
}


text input:
-14
+15
+9
+19
+18
+14
+14
-18
+15
+4
-18
-20
-2
+17
+16
-7
-3
+5
+1
-5
-11
-1
-6
-20
+1
+1
+4
+18
+5
-20
-10
+18
+5
-4
-5
-18
+9
+6
+1
-19
+13
+10
-22
-11
-14
-17
-10
-1
-13
+6
-17
  • 1
    `realloc(A, 2 * sizeof(int));` - you realloc for only 2 values. You want `2 * freqCount` _more_ values. That's `realloc(A, 3 * freqCount * sizeof(int));` | `for (int i = 1; i < 15; i++)` - array numbering starts at `0`. – KamilCuk Jan 23 '19 at 08:08
  • 1
    You [should not cast `malloc` in C](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). – Some programmer dude Jan 23 '19 at 08:12
  • @KamilCuk ok, what if i want to expand the array by +freqCount each time, so asuming freqCount=1025, the array A is increased by +1025 each time the function is called ? –  Jan 23 '19 at 08:43
  • You need to track the current size. If `i want to expand the array by +freqCount` then you need to add freqCount to current size and call `realloc` with the result. If `int *a = malloc(1025 * sizeof(int))`, and then you want another 1025, you need `int *new_a = realloc(a, 2050 * sizeof(int))`. – KamilCuk Jan 23 '19 at 08:53
  • @KamilCuk hmmm, ok... but do You have an idea, how could i automate the whole process. What i mean, is to write a function, that whenever is called it will increase the size fo the array of the same size . Would such function need to include a line of code to find out what the current size of array is ? –  Jan 23 '19 at 11:46
  • Such function would need to include all the relevant code that are needed to pass along array size. Usually in C you wrap data / objects using structs. `struct intarray_s { int *arr; size_t size; }` and then `int intarray_resize(struct inarray_s *t, ssize_t inc) { void *pnt = realloc(t->arr, t->size + inc); .... }` You need to do it manually. – KamilCuk Jan 23 '19 at 12:21

1 Answers1

2

You unconditionally resize your array to only contain two int elements. Always. Any access to elements beyond those two will lead to undefined behavior.

You probably meant to do

return realloc(A, freqCount * sizeof(int));
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • well to be honest i was thinking that when i type 2 * sizeof(int) the array size will increase two times. so what does that line freqCount * sizeof(int) actualy mean ? –  Jan 23 '19 at 08:13
  • @kenshin Just like `malloc`, the size you pass to [`realloc`](https://en.cppreference.com/w/c/memory/realloc) is the size in bytes. The *new* and *full* size. – Some programmer dude Jan 23 '19 at 08:15
  • Basically. First the value freqCount was 1025 . Than I created array A of size 1025 . I stored 1025 elements in it. Than I wanted to increase the array A by another "freqCount" (1025) so that it has 2050 arguments. Is my logic ok ? –  Jan 23 '19 at 08:21
  • @kenshin That's good logic, and that's what you do with `freqCount = freqCount + freqCount;`. The problem is that you need to follow through with the actual reallocation (and remember that is can fail, which you need to check for). – Some programmer dude Jan 23 '19 at 08:22