c++ 警告:‘t’可能在此函数中使用未初始化的 [-Wmaybe-uninitialized] cm(t,n);
c++ warning: ‘t’ may be used uninitialized in this function [-Wmaybe-uninitialized] cm(t,n);
编译完成
我明白为什么不起作用
!g++ -Wall -Wextra -g -c cm1.cpp -o cm1.o
cm1.cpp: In function ‘int main(int, char**)’:
cm1.cpp:14:5: warning: ‘t’ may be used uninitialized in this function [-Wmaybe-uninitialized]
cm(t,n);
~~^~~~~
编译所有部分进行测试
!g++ cm1.o cm.o -o test
执行
这里当我只使用 g++ cm1.cpp -o cm1.o
!./test 4
4: 参数对
!./test 4 4
语法:./test (n impair)
但是当我尝试正确的数字时,什么也没有给我
!./test 5
代码部分是:
--
第一名
%%writefile cm.hpp
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
void cm(int **t,int n);
第二
%%writefile cm.cpp
#include "cm.hpp"
void cm(int **t,int n){
int l, c, exl, exc;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) t[i][j] = 0;
l = 0; c = n/2; t[l][c] = 1;
for(int i = 2; i <= n*n; i++) {
exl = l; exc = c;
l--; if(l < 0) l = n-1;
c++; if(c > n-1) c = 0;
if(t[l][c] != 0) { l = exl+1; c = exc; }
t[l][c] = i;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
cout << setw(6) << t[i][j];
cout << endl;
}
unsigned trace = 0;
for(int i = 0; i < n; i++) trace += t[i][i];
cout << "Somme magique = trace: " << trace << endl;
}
第 3 部分
int main(int argc, char *argv[]) {
if(argc != 2) {
cout << "Syntaxe: " << argv[0] << " <n impair>" << endl;
exit(1);
}
int n;
n= atoi(argv[1]);
if(n % 2 == 0) {
cout << n << ": argument pair\n";
exit(2);
} int **t1;
cm(t1,n);
return 0;
}
您必须在将缓冲区传递给函数之前通过分配缓冲区来初始化t1
。
} int **t1;
// allocate an array of pointers
t1 = new int*[n];
// allocate arrays for each row
for (int i = 0; i < n; i++) {
t1[i] = new int[n];
}
cm(t1,n);
编译完成
我明白为什么不起作用
!g++ -Wall -Wextra -g -c cm1.cpp -o cm1.o
cm1.cpp: In function ‘int main(int, char**)’:
cm1.cpp:14:5: warning: ‘t’ may be used uninitialized in this function [-Wmaybe-uninitialized]
cm(t,n);
~~^~~~~
编译所有部分进行测试
!g++ cm1.o cm.o -o test
执行
这里当我只使用 g++ cm1.cpp -o cm1.o
!./test 4
4: 参数对
!./test 4 4
语法:./test (n impair)
但是当我尝试正确的数字时,什么也没有给我
!./test 5
代码部分是:
-- 第一名
%%writefile cm.hpp
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
void cm(int **t,int n);
第二
%%writefile cm.cpp
#include "cm.hpp"
void cm(int **t,int n){
int l, c, exl, exc;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) t[i][j] = 0;
l = 0; c = n/2; t[l][c] = 1;
for(int i = 2; i <= n*n; i++) {
exl = l; exc = c;
l--; if(l < 0) l = n-1;
c++; if(c > n-1) c = 0;
if(t[l][c] != 0) { l = exl+1; c = exc; }
t[l][c] = i;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
cout << setw(6) << t[i][j];
cout << endl;
}
unsigned trace = 0;
for(int i = 0; i < n; i++) trace += t[i][i];
cout << "Somme magique = trace: " << trace << endl;
}
第 3 部分
int main(int argc, char *argv[]) {
if(argc != 2) {
cout << "Syntaxe: " << argv[0] << " <n impair>" << endl;
exit(1);
}
int n;
n= atoi(argv[1]);
if(n % 2 == 0) {
cout << n << ": argument pair\n";
exit(2);
} int **t1;
cm(t1,n);
return 0;
}
您必须在将缓冲区传递给函数之前通过分配缓冲区来初始化t1
。
} int **t1;
// allocate an array of pointers
t1 = new int*[n];
// allocate arrays for each row
for (int i = 0; i < n; i++) {
t1[i] = new int[n];
}
cm(t1,n);