我正在从指针执行转换然后它让我继续运行此警告(赋值使得指针来自整数而没有强制转换).这是代码:
#include<stdio.h>
#include<stdbool.h>
typedef int TipoChave;
typedef struct TipoRegistro {
TipoChave Chave;
/*outros componentes*/
} TipoRegistro;
typedef struct TipoPagina* TipoApontador;
typedef struct TipoPagina {
int registros;
TipoRegistro *r;
TipoApontador *p;
} TipoPagina;
TipoApontador NovaSubArvore(int ordem){
TipoApontador A;
A=malloc(sizeof(TipoPagina));
int i;
A->registros=0;
A->r=malloc((2*ordem)*sizeof(TipoRegistro));
A->p=malloc((2*ordem+1)*sizeof(TipoPagina));
for (i=0;i<(2*ordem+1);i++){
A->p[i]=NULL;
if(i!=2*ordem){
A->r[i].Chave=0;
}
}
return (A);
}
Run Code Online (Sandbox Code Playgroud)
在主要我打电话:
TipoApontador Raiz;
Run Code Online (Sandbox Code Playgroud)
然后:
Raiz=NovaSubArvore(ordem); //Warning happens here
Run Code Online (Sandbox Code Playgroud)
如果我做:
if (Raiz!=NULL)
free(Raiz);
Run Code Online (Sandbox Code Playgroud)
它运行一个免费的invallid(奇怪,因为如果Raiz是NULL,免费应该没有运行.请问有人帮我解决这个问题吗?我认为这个警告是让我免于"解放"的问题.
编辑:关于waring解决的确定问题.但是如果我做了2次免费游戏就会运行一个无效的免费游戏(我有一个免费的东西,其他时间没有.如果我自由地执行"if(Raiz!= NULL)"应该阻止另一个免费的跑步.但事实并非如此.
我知道在头文件中使用包含警戒是为了防止某些内容被定义两次.但是,使用此代码示例,完全没问题:
foo.c的
#include <stdio.h>
#include <string.h>
#include "bar.h"
int main() {
printf("%d", strlen("Test String"));
somefunc("Some test string...");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
bar.h
#ifndef BAR_H_INCLUDED
#define BAR_H_INCLUDED
void somefunc(char str[]);
#endif
Run Code Online (Sandbox Code Playgroud)
bar.c
#include <stdio.h>
#include <string.h>
#include "bar.h"
void somefunc(char str[]) {
printf("Some string length function: %d", strlen(str));
}
Run Code Online (Sandbox Code Playgroud)
上面的代码片段是用编译的,gcc -Wall foo.c bar.c -o foo没有错误.然而,无论是<stdio.h>和<string.h>被列入没有包括后卫.将bar.h删除到单个语句时仍然没有错误void somefunc(char str[]);.为什么没有错误?
以下是#include来自Google的C++风格指南的部分:
如果您依赖bar.h中的符号,请不要指望您包含foo.h这个(当前)包含bar.h:自己包含bar.h,除非foo.h明确表明其意图为您提供bar.h的符号.
但是,相关头中存在的任何包含不需要再次包含在相关的cc中(即,foo.cc可以依赖于foo.h的包含).
当我读到这篇文章时,这些句子似乎是矛盾的.他们对我说:
foo.cc需要的东西bar.h,它必须包括bar.h.foo.cc需要填充bar.h,foo.cc包含foo.h和foo.h包含bar.h,则foo.cc不需要包含bar.h.为什么这些句子不相矛盾?
"C接口和实现"显示了数据结构的一些有趣的使用模式,但我确信还有其他的.
我正在尝试解决消费者/生产者问题,我必须创建三个不同的类.
主类包括线程的创建和消费者/生产者逻辑
另外两个类是
我在尝试编译时遇到以下错误:
ringbuf.c: In function ‘rb_init’:
ringbuf.c:10: warning: incompatible implicit declaration of built-in function ‘malloc’
ringbuf.c:10: error: invalid application of ‘sizeof’ to incomplete type ‘struct ringbuf_t’
ringbuf.c:12: error: dereferencing pointer to incomplete type
Run Code Online (Sandbox Code Playgroud)
我还有很多其他的错误,但是一旦我完成这个错误,我就可以自己处理.
这是缓冲区的头文件:
struct ringbuf_t
{
pthread_mutex_t mutex; /* lock to protect from simutaneous access to the buffer */
pthread_cond_t cond_full; /* producer needs to wait when the buffer is full */
pthread_cond_t cond_empty; /* consumer needs to wait when the buffer is empty …Run Code Online (Sandbox Code Playgroud) 假设我在C中使用多个.c文件处理一个大项目,有什么理由我更喜欢有多个头文件而不是单个头文件?
还有一个问题:
比方说,我有3个文件:header.h,main.c和other.c.
我有一个名为的函数func(),它只在文件中定义和使用other.c.我应该将函数原型放在头文件或文件中other.c吗?
人.h
#ifndef PERSON_H_
#define PERSON_H_
/* Person.h */
class Person {
int age;
std::string name;
public:
Person(int, std::string);
std::string getName();
int getAge();
};
#endif /* PERSON_H_ */
Run Code Online (Sandbox Code Playgroud)
person(int std::string) 函数声明使用 std::string 名称,但我尚未将其包含在头文件中。因此,我希望编译器会抱怨丢失的符号。但它编译并运行良好!为什么?
其余代码...
个人.cpp
#include <string>
#include "Person.h"
Person::Person(int age, std::string name)
{
this->name = name;
this->age = age;
}
std::string Person::getName()
{
return this->name;
}
int Person::getAge()
{
return this->age;
}
Run Code Online (Sandbox Code Playgroud)
主程序
#include <string>
#include "Person.h"
int main() {
printFunc();
Person chelsea_manning(5, std::string("Chelsea Manning"));
}
Run Code Online (Sandbox Code Playgroud)
另外,我是 C++ 的新手,所以如果你发现我的代码/OOP 有什么奇怪的地方,请告诉我。
例如:
工程/工具/ a.cpp
和
工程/工具/啊
是在此项目中随处使用的一批函数的实现文件和头文件.
现在,想象一下
项目/组件/ BH
想包括a.h.一个常见的解决方案是使用:
#include "../utility/a.h"
在b.h.
但谷歌C++风格不允许这样做:
所有项目的头文件都应该作为项目源目录的后代列出,而不使用UNIX目录快捷方式
.(当前目录)或..(父目录).
我尝试将其包含在上面的文档中,但它不起作用.
那么我应该怎么做才能包含它而不使用'.' 要么 '..' ?
什么size_t是未定义的,据说获得它的定义的唯一可移植方式是包含其中一个标题.
但是,据我所知,#include在头文件中使用是不好的做法,因为标题不应该包含其他标题,对吧?
因此,程序员有责任找到一种方法来确保标题编译而不会导致糟糕的编程习惯.
据我了解,size_t类型是保证支持系统可以处理的最长对象的最小类型.例如,如果你运行64位操作系统,那么size_t应该是64位,因为这就是RAM和指针的工作方式.
对于指针,任何给定的指针都表示与RAM中第0个字节的偏移量,其中取消引用该偏移量将为您提供存储在该偏移量处的值.并且CPU(在这种情况下,64位)可以完全控制它访问它能够以其支持的最大单元(即64位整数)进行寻址的任何18个元素或左右的字节. ,又名指针.
因此,不是size_t和任何给定的指针类型保证相同的大小?
那么,问题是以下哪一项是给定标题的最佳实践:
void some_function(ptr *some_object, size_t n);
...
Run Code Online (Sandbox Code Playgroud)
可能性似乎是:
#include <stddef.h>
// would definitely work...but including a header in a header is ugly practice
typedef long long unsigned size_t
// to account for win64 making `long` 32-bits...but it's possibly non-portable
typedef char * size_t
// I like this one...but am not 100% sure about how portable it is
Run Code Online (Sandbox Code Playgroud)
此外,为什么使用指针定义size_t是不合需要的?好像应该没问题.据我所知,指针保证占用寄存器的全部大小,因此应该与架构的"理想"大小相同size_t
我正在阅读嵌入式C标准书,我注意到以下内容:
没有头文件应包含#include语句
对于具有非标准类型的函数声明,我该怎么办?
例: void function(some_funky_type x);
我收到了错误
from error.c:31:
/usr/include/ap/mas.h:254: error: expected specifier-qualifier-list before ‘time_t’
make: *** [error.o] Error 1
Run Code Online (Sandbox Code Playgroud)
反馈
我们至少需要看到error.c的第31行和mas.h的第254行,最好是每行的几行上下文.此错误可能与time_t的声明方式无关. - 约翰博德
然后我检查error.c(第31行) - #include"mas.h"然后我检查mas.h中的第254行.
in mas.h
#include <sys/types.h>
typedef struct _x{
time_t time;
}x;
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议我哪里出错了?
我有一个C主文件,其中包括此.h文件:
#pragma pack(1)
#ifndef PACKAGE
#define PACKAGE
struct A {
uint8_t a;
uint8_t b;
uint64_t c;
} typedef A;
#endif
Run Code Online (Sandbox Code Playgroud)
编译警告后:
myfile.c:28:10: warning: the current #pragma pack alignment value is modified in
the included file [-Wpragma-pack]
#include "structures.h"
^
./structures.h:1:9: note: previous '#pragma pack' directive that modifies
alignment is here
#pragma pack(1)
Run Code Online (Sandbox Code Playgroud)
出现。
我不明白我的代码有什么问题。有什么办法可以删除此警告?
这是一个完整的示例:
这是一个名为“ myfile.c”的简单C文件:
#include "structures.h"
int main(){
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是一个名为“ structures.h”的.h文件:
#include <stdlib.h>
#include <stdio.h>
#pragma pack(1)
#ifndef PACKAGE
#define PACKAGE
struct A {
uint8_t …Run Code Online (Sandbox Code Playgroud)