Visual studio 2022 doesn't complie because of this - c

In visual studio 2022 I can't compile this little program and on Clion, visual studio code and others it compiles.
I don't know if it could be a problem of the clang compiler or some configuration of visual studio (but stills on default).
#include <stdio.h>
int main() {
//Variable declarations
int size;
printf("INPUT\n");
printf("SIZE (2-3)?\n");
scanf("%d", &size);
int t[size][size]; //This is marked as wrong (only in visual studio 2022 not other IDE's)
int i = 0;
int j = 0;
//matrix read
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("POSITION(%d, %d)?\n", i, j);
scanf("%d", &t[i][j]);
}
}
int calc = 0;
//calc matrix
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
calc = calc + t[i][j];
}
}
printf("suma: %d", calc);
return 0;
}
Thanks for reading.
I've tested the code on other IDE's and compiles without problems.

Instead of a Variable Length Array, a pointer to pointer, int **t, could be used.
Allocate the pointers and then for each pointer, allocate the elements.
#include <stdio.h>
#include <stdlib.h>
int main() {
//Variable declarations
int size;
printf("INPUT\n");
printf("SIZE (2-3)?\n");
if ( 1 != scanf("%d", &size)) {
fprintf ( stderr, "Could not scan an integer\n");
return 1;
}
int **t = NULL; // pointer to pointer
if ( NULL == ( t = malloc ( size * sizeof *t))) {
fprintf ( stderr, "Could not allocate pointers\n");
return 1;
}
int each = 0;
for ( each = 0; each < size; ++each) {
if ( NULL == ( t[each] = malloc ( size * sizeof **t))) {
fprintf ( stderr, "Could not allocate elements\n");
return 1;
}
}
int i = 0;
int j = 0;
//matrix read
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("POSITION(%d, %d)?\n", i, j);
if ( 1 != scanf("%d", &t[i][j])) {
fprintf ( stderr, "Could not scan an integer\n");
return 1;
}
}
}
int calc = 0;
//calc matrix
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
calc = calc + t[i][j];
}
}
printf("suma: %d\n", calc);
for (i = 0; i < size; i++) {
free ( t[i]);
}
free ( t);
return 0;
}

Related

What is the difference between Array[n] ; and Array[ ]={ };

#include <stdio.h>
int main() {
int n, i, j, k, l;
int temp;
printf("Enter how many element on the array : ");
scanf("%d", &n);
int arr1[100] = {};
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d \t", arr1[i]);
}
}
My code for sorting an array in ascending order works properly. And it doesn't have any error but when I am changed the array size then the code doesn't work properly and has an error called stack smashing detected. What causes this problem?
#include <stdio.h>
int main() {
int n, i, j, k, l;
int temp;
printf("Enter how many element on the array : ");
scanf("%d", &n);
int arr1[] = {};
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d \t", arr1[i]);
}
}
Neither int arr1[100] = {}; nor int arr1[] = {}; is valid C code.
The program compiles because your compiler implements GNU extensions that allow empty initializers and zero length arrays.
The reason your program no longer works when you remove the length 100 is the array becomes too short for the elements you try and store into it.
You probably meant to write int arr1[n] = {}; which does not compile because VLAs (variable sized arrays) cannot have an initializer.
Here is a modified version:
#include <stdio.h>
int main() {
int n, i, j, k, l;
printf("Enter how many element on the array : ");
if (scanf("%d", &n) != 1 || n <= 0) {
fprintf(stderr, "invalid size\n");
return 1;
}
int arr1[n];
for (i = 0; i < n; i++) {
if (scanf("%d", &arr1[i]) != 1) {
fprintf(stderr, "invalid input\n");
return 1;
}
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
int temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d%c", arr1[i], "\t\n"[i == n - 1]);
}
return 0;
}

How to use strtok

Below is a solution to the codewars training.
https://www.codewars.com/kata/59f4a0acbee84576800000af/train/c
Test Crashed
Caught unexpected signal: SIGSEGV (11). Invalid memory access.
I get an error message like this, what's wrong?
I think it's probably part of the strtok function, but please tell me what's wrong.
#include <stdlib.h>
#include <string.h>
double pos_average(char *s, unsigned n)
{
char **matrix;
char *p;
unsigned int i, j, k;
unsigned int subst_len = ( strchr(s,',') - s ) / sizeof(char);
double count = 0;
matrix = (char**)calloc( n + 1 ,sizeof(char*) );
if(!matrix) exit(0);
for(i = 0; i < n; i++)
{
matrix[i] = (char*)calloc( subst_len + 1 ,sizeof(char) );
if(!matrix[i]) exit(0);
}
for(i = 0; i < n; i++)
{
if(i == 0){
p = strtok(s, " ");
strncpy(matrix[i], p, subst_len);
}
else{
p = strtok(NULL," ");
strncpy(matrix[i], p, subst_len);
}
}
for(i = 0; i < n - 1; i++)
{
for(j = i + 1; j < n; j++)
{
for(k = 0; k < subst_len; k++)
{
if(matrix[i][k] == matrix[j][k]) count++;
}
}
}
for(i = 0; i < n; i++) free(matrix[i]);
free(matrix);
return (count / ( ( (double)n * ( (double)n - 1.0 ) ) / 2.0 ) ) * 100.0;
}

The pointer variables overflows when they store integers larger than 1024 and some adresses seem to be locked.in C

How do I get to write to 2D pointers where I have pnumber[2%4][2%4] and how can I get pnumber with more than 3 ciphers to be displayed?
I'm making a program to write pascals triangle in C.
When the pointer pnumbers[i][j] have both i and j = 2 mod 4, except for when i and j = 2, then my program won't write to the address and give the error message:
pascals triangle: malloc.c:2406: sysmalloc: Assertion '{old_top == initial_top (av) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int factorial(int p) {
if (p>=1) {
return p*factorial(p-1);
}
else {
return 1;
}
}
int NchooseM(int n, int m) {
return factorial(n)/(factorial(n-m)*factorial(m));
}
int main() {
int n =7;
int x = n-2;
int i, j, k;
/*
printf("How many rows of Pascals triangle do you want to write?\n");
scanf("%d", &n);
*/
int **pnumbers;
pnumbers = (int **) malloc(n *sizeof(int *));
/* Allocate memory for storing the individual elements in a row */
for (i = 0; i < n; i++) {
pnumbers[i] = (int *) malloc(i * sizeof(int));
}
pnumbers[0][1] = 1;
/* Calculating the value of pnumbers[k][l] */
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
/*
if (!(i % 4 == 2 && i != 2))
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
} else if (i > 2) {
for (j = 0; j <= i-1; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
}
*/
}
/* Writing out the triangle */
for (i = 0; i < n; i++) {
for (k = 0; k <= x; k++){
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("%d ", pnumbers[i][j]);
}
x = x-1;
printf("\n");
}
for (i = 0; i < n; i++) {
free(pnumbers[i]);
}
free(pnumbers);
return 0;
}
When I avoid writing to these addresses and just print them out I get some seemingly random integer at these memory addresses.
Also when avoid these addresses and just print out so many rows that I get some spots with a higher integer with more than 3 siphers, it seems to overflow - and I don't see the logic behind it.
The result of running the second code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int factorial(int p) {
if (p>=1) {
return p*factorial(p-1);
}
else {
return 1;
}
}
int NchooseM(int n, int m) {
return factorial(n)/(factorial(n-m)*factorial(m));
}
int main() {
int n =20;
int x = n-2;
int i, j, k;
/*
printf("How many rows of Pascals triangle do you want to write?\n");
scanf("%d", &n);
*/
int **pnumbers;
pnumbers = (int **) malloc(n *sizeof(int *));
/* Allocate memory for storing the individual elements in a row */
for (i = 0; i < n; i++) {
pnumbers[i] = (int *) malloc(i * sizeof(int));
}
pnumbers[0][1] = 1;
/* Calculating the value of pnumbers[k][l] */
for (i = 0; i < n; i++) {
/*
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
*/
if (!(i % 4 == 2 && i != 2))
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
} else if (i > 2) {
for (j = 0; j <= i-1; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
}
}
/* Writing out the triangle */
for (i = 0; i < n; i++) {
for (k = 0; k <= x; k++){
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("%d ", pnumbers[i][j]);
}
x = x-1;
printf("\n");
}
for (i = 0; i < n; i++) {
free(pnumbers[i]);
}
free(pnumbers);
return 0;
}
But row number 13 is still quite messed up.
Code is experiencing int overflow and thus undefined behavior (UB).
With 32-bit int and int factorial(int p), p > 12 oveflows the int range.
Code could use a wider integer type (long long works up to p==20), but improvements can be made at NchooseM() to avoid overflow for higher values.
Something like the below. Works up to int n = 30;
int NchooseM(int n, int m) {
// return factorial(n)/(factorial(n-m)*factorial(m));
int nm = 1;
int den = 1;
for (int i = m+1; i <= n; i++) {
assert(INT_MAX/i >= nm);
nm *= i;
assert(nm % den == 0);
nm /= den++;
}
return nm;
}
Tried unsigned long long and works up to int n = 62;
Edit: Another bug:
I "fixed" by initializing all to 1, yet I suspect something remains amiss in /* Calculating the value of pnumbers[k][l] */ for (i = 0; i < n; i++) { code.
pnumbers[i] = malloc((i + 1) * sizeof pnumbers[i][0]);
for (int j = 0; j < i + 1; j++) {
pnumbers[i][j] = 1;
}
Aside: rather than pnumbers[i] = (int *) malloc((i+1) * sizeof(int));, consider below with no unneeded cast nor trying to match the right type.
pnumbers[i] = malloc(sizeof pnumbers[i][0] * (i+1));

Runtime error in a C program to find an index of an element

This program prints the index of element after sorted. Here is the code:
#include<stdio.h>
#include<limits.h>
int n;
int value[10000];
int index[10000];
int enable[10000];
//return the element which is smallest and never selected
int min() {
int i, tmp=INT_MAX;
for (i = 0; i < n; i++) {
if (enable[i] == 0 && tmp > value[i] )
tmp = value[i];
}
return tmp;
}
void solve() {
int i,j;
int tmp;
for (i= 0; i < n; i++) {
tmp = min();
for (j = 0; j < n; j++) {
if (enable[j] == 0 && tmp == value[j] ) {
index[j] = i + 1;
enable[j] = 1;
}
}
}
}
void print() {
int i = 0;
for (; i < n-1; i++)
printf("%d ", index[i]);
printf("%d\n", index[i]);
}
int main() {
int i;
while (scanf("%d", &n) != EOF) {
for (i = 0; i < n; i++) {
scanf("%d", &value[i]);
}
solve();
print();
}
return 0;
}
It worked well on my computer, but the online judge shows runtime error. I could not figure it out where it went wrong, maybe my test is not enough.

what is error in this C code using realloc?

I am trying to have some kind of dynamically growing array/data structure in C. Below is the C code I have for it. But after it prints the array, it gives a run-time error as shown below in the snapshot. What is going wrong? It is being compiled using MS-Visual C++ 2010 (Free version) on a Windows-7.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a;
int i = 5;
if((a = (int *)malloc(i * sizeof(int))) == NULL) {
fprintf(stderr, "Error: failed malloc\n");
return 1;
}
for(i = 0; i < 5; i++)
a[i] = i;
printf("-- array after malloc\n");
for(i = 0; i < 5; i++)
printf(" a[%d] = %d\n", i, a[i]);
if((a = (int *)realloc(a, i * sizeof(int))) == NULL) {
fprintf(stderr, "Error: failed realloc\n");
return 1;
}
for(i = 0; i < 10; i++)
a[i] = i;
printf("\n-- array after realloc\n");
for(i = 0; i < 10; i++)
printf(" a[%d] = %d\n", i, a[i]);
free(a);
return 0;
}
//<important>
//weren't you supposed to do i = 10 here ????
//</important>
if((a = (int *)realloc(a, i * sizeof(int))) == NULL) {
fprintf(stderr, "Error: failed realloc\n");
return 1;
}
for(i = 0; i < 10; i++)
a[i] = i;
I think this was supposed to resize array to 10 and use it, but you never changed i to 10 so i is still 5 and you go out of range
On this line:
if((a = (int *)realloc(a, i * sizeof(int))) == NULL) {
i still contains the value 5. So basically you are realloc'ing for 5 integers while you assign 10 integers in the next loop: an error.
This would be the fixed code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a;
int i;
if((a = (int *)malloc(5 * sizeof(int))) == NULL) {
fprintf(stderr, "Error: failed malloc\n");
return 1;
}
for(i = 0; i < 5; i++)
a[i] = i;
printf("-- array after malloc\n");
for(i = 0; i < 5; i++)
printf(" a[%d] = %d\n", i, a[i]);
if((a = (int *)realloc(a, 10 * sizeof(int))) == NULL) {
fprintf(stderr, "Error: failed realloc\n");
return 1;
}
for(i = 0; i < 10; i++)
a[i] = i;
printf("\n-- array after realloc\n");
for(i = 0; i < 10; i++)
printf(" a[%d] = %d\n", i, a[i]);
free(a);
return 0;
}

Resources