Language: C++

//
//  main.cpp
//  ALDS1_6
//
//  Created by ptyuan on 2018/10/31.
//  Copyright © 2018 YY. All rights reserved.
//

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int n = 0;

void CountingSort(int *A, int *B, int k) {
    int *C = (int *)malloc(sizeof(int) * k + 1);
    for (int i = 0; i <= k; i++) {
        C[i] = 0;
    }
    
    for (int i = 1; i <= n; i++) {
        C[A[i]]++;
    }
    
    for (int i = 1; i <= k; i++) {
        C[i] += C[i - 1];
    }
    
    for (int j = n; j >= 1; j--) {
        B[C[A[j]]] = A[j];
        C[A[j]]--;
    }
    free(C);
}

int main(int argc, const char * argv[]) {
    scanf("%d", &n);
    int A = (int)malloc(sizeof(int) * n + 1);
    int B = (int)malloc(sizeof(int) * n + 1);
    int _max = 0;
    for (int i = 1; i <= n; ++i) {
        scanf("%d", A + i);
        _max = max(_max, A[i]);
    }
    CountingSort(A, B, _max);
    for (int i = 1; i <= n; ++i) {
        if (i > 1) {
            printf(" ");
        }
        printf("%d", *(B + i));
    }
    printf("\n");
    free(A);
    free(B);
    return 0;
}



Compiler output:
a.cc: In function 'int main(int, const char**)':
a.cc:38:13: error: cast from 'void*' to 'int' loses precision [-fpermissive]
   38 |     int A = (int)malloc(sizeof(int) * n + 1);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:39:13: error: cast from 'void*' to 'int' loses precision [-fpermissive]
   39 |     int B = (int)malloc(sizeof(int) * n + 1);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:43:27: error: invalid types 'int[int]' for array subscript
   43 |         _max = max(_max, A[i]);
      |                           ^
a.cc:45:18: error: invalid conversion from 'int' to 'int*' [-fpermissive]
   45 |     CountingSort(A, B, _max);
      |                  ^
      |                  |
      |                  int
a.cc:15:24: note:   initializing argument 1 of 'void CountingSort(int*, int*, int)'
   15 | void CountingSort(int *A, int *B, int k) {
      |                   ~~~~~^
a.cc:45:21: error: invalid conversion from 'int' to 'int*' [-fpermissive]
   45 |     CountingSort(A, B, _max);
      |                     ^
      |                     |
      |                     int
a.cc:15:32: note:   initializing argument 2 of 'void CountingSort(int*, int*, int)'
   15 | void CountingSort(int *A, int *B, int k) {
      |                           ~~~~~^
a.cc:50:22: error: invalid type argument of unary '*' (have 'int')
   50 |         printf("%d", *(B + i));
      |                      ^~~~~~~~
a.cc:53:10: error: invalid conversion from 'int' to 'void*' [-fpermissive]
   53 |     free(A);
      |          ^
      |          |
      |          int
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/c++/14/ext/string_conversions.h:43,
                 from /usr/include/c++/14/bits/basic_string.h:4154,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:9:
/usr/include/stdlib.h:687:25: note:   initializing argument 1 of 'void free(void*)'
  687 | extern void free (void *__ptr) __THROW;
      |                   ~~~~~~^~~~~
a.cc:54:10: error: invalid conversion from 'int' to 'void*' [-fpermissive]
   54 |     free(B);
      |          ^
      |          |
      |          int
/usr/include/stdlib.h:687:25: note:   initializing argument 1 of 'void free(void*)'
  687 | extern void free (void *__ptr) __THROW;
      |                   ~~~~~~^~~~~