0

Can anybody help me with this code: I was trying running it but the compiler popped up with:

Stack Trace: user_code.out: malloc.c:2406: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.

I have no idea what this error is about or how do I solve it.

Please Give Solution

main() {
  unsigned int location = 1, c, x;
  long int * A, n, t;
  scanf("%d", & t);
  while (t--) {
    scanf("%d", & n);
    A = (int * ) malloc(sizeof(int) * n);
    for (int i = 0; i < n; i++)
      scanf("%d", (A + i));

    for (int i = 0; i < n - 1; i++) {
      for (int j = 1; j < n; j++) {
        if (i == j)
          continue;
        else {
          x = ((A[i] & A[j]) ^ (A[i] | A[j]));
          if (x < c)
            c = x;
        }
      }
    }
    printf("%d", c);
    free(A);
  }
}
selbie
  • 100,020
  • 15
  • 103
  • 173
Yatender
  • 3
  • 1
  • 2
    You malloc space for `n` ints (and `scanf` into it using `%d`), but `A` is a pointer to `long int`. – Paul Hankin Aug 01 '21 at 14:10
  • Because of this, you corrupt the heap, and that happened to manifest as some failed assertions in malloc. – Paul Hankin Aug 01 '21 at 14:11
  • Same for `scanf("%d", &t)` and `scanf("%d", &n);` and I'm surprised that the compiler didn't produce warnings for these. – Paul Hankin Aug 01 '21 at 14:13
  • A decent compiler *does* produce warnings for these: https://godbolt.org/z/bfMTn5xrs. OP: Use a decent compiler, turn on its warnings, and don't ignore them! (Also: I suggest proper indentation and more meaningful variable names.) – Nate Eldredge Aug 01 '21 at 17:17

1 Answers1

1

Your primary issue is that

A=(int*)malloc(sizeof(int)*n); 

allocates space for n objects with the size of int, and you later refer to this memory through a pointer to objects of size long int. If these types are not the same size, you run the risk of reading and writing with incorrect memory sizes.

A good practice is to use the identifier with the sizeof operator

array = malloc(sizeof *array * length);

so that if the type of array changes, the expression here does not need to.

The "%d" conversion specifier for scanf expects an int. For long int, you'll need "%ld". For unsigned int, you'll need "%u".

Additionally, c is never initialized, and thus

if(x<c)

will invoke undefined behavior by reading the value of it.

Other issues:

  • There are two signatures for main that you should use:

    1. int main(void), or
    2. int main(int argc, char **argv) (or equivalent).
  • You should not cast the return of malloc.

  • scanf can fail, and you should be prepared to handle these events.

  • The proper type for storing sizes of objects is size_t. This should be used when iterating on array indices.

  • Using signed types for sizes and iteration counts can lead to strange edge cases.

  • Coding style is subjective, but I'm willing to bet most would find the style of this code to be very hard to read. Be smart, not clever - try to use more meaningful variable names, use clear and consistent indentation, and don't be so afraid of the spacebar.


A more complete example:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

void die(const char *msg) {
    fprintf(stderr, "%s\n", msg);
    exit(EXIT_FAILURE);
}

unsigned int andxoror(long int a, long int b) {
    return (a & b) ^ (a | b);
}

int main(void) {
    unsigned int lowest, calculation, times;
    long int *array;
    size_t length;

    printf("Input number of repetitions: ");

    if (scanf("%u", &times) != 1)
        die("Invalid repetition input.");

    while (times--) {
        printf("Input length of array: ");

        if (scanf("%zu", &length) != 1)
            die("Invalid length input.");

        if ((array = malloc(sizeof *array * length)) == NULL)
            die("Could not allocate memory.");

        for (size_t i = 0; i < length; i++) {
            printf("Input array member %ld of %ld: ", i + 1, length);

            if (scanf("%ld", array + i) != 1) {
                free(array);
                die("Invalid array member input.");
            }
        }

        lowest = UINT_MAX;

        for (size_t i = 0; i < length - 1; i++)
            for (size_t j = 1; j < length; j++)
                if (i != j && (calculation = andxoror(array[i], array[j])) < lowest)
                    lowest = calculation;

        printf("Final calculation: %u\n", lowest);
        free(array);
    }
}
Oka
  • 23,367
  • 6
  • 42
  • 53