警告:在此函数中可以使用未初始化的 X [英] Warning: X may be used uninitialized in this function

查看:29
本文介绍了警告:在此函数中可以使用未初始化的 X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个自定义的向量"结构.我不明白为什么我会收到 Warning: "one" may be used uninitialized 在这里.

I am writing a custom "vector" struct. I do not understand why I'm getting a Warning: "one" may be used uninitialized here.

这是我的 vector.h 文件

This is my vector.h file

#ifndef VECTOR_H
#define VECTOR_H

typedef struct Vector{
    int a;
    int b;
    int c;
}Vector;

#endif /* VECTOR_ */

警告发生在 one->a = 12

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "vector.h"

int main(void){
    Vector* one;
    one->a = 12;
    one->b = 13;
    one->c = -11;
}

推荐答案

one 尚未分配,因此指向不可预测的位置.您应该将其放在堆栈中:

one has not been assigned so points to an unpredictable location. You should either place it on the stack:

Vector one;
one.a = 12;
one.b = 13;
one.c = -11

或者为它动态分配内存:

or dynamically allocate memory for it:

Vector* one = malloc(sizeof(*one))
one->a = 12;
one->b = 13;
one->c = -11
free(one);

注意在这种情况下使用 free.通常,每次调用 malloc 时,您只需要调用一次 free.

Note the use of free in this case. In general, you'll need exactly one call to free for each call made to malloc.

这篇关于警告:在此函数中可以使用未初始化的 X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆