我的编译器(DSP SHARC)非常挑剔。当我用备注进行构建时,出现此错误:
[cc1123] foo.c:1511 {D} remark: comparison of unsigned type
with signed type
while (taille > 0)
Run Code Online (Sandbox Code Playgroud)
两种解决方案:
编译器是正确的,我应该写
size_t taille;
...
while(taille > (size_t)0)
Run Code Online (Sandbox Code Playgroud)编译器很愚蠢,我应该忽略这一点
可能涉及ISO或MISRA标准的另一种解决方案
我该怎么办?
编辑
其实我最好写这样的例子
while(taille) {...}
Run Code Online (Sandbox Code Playgroud)
但是,这与我最初的问题无关
我知道我们可以使用clock_gettime(CLOCK_MONOTONIC).
我尝试问的问题是,如果我需要从纪元开始的纳秒时间,那将是一个巨大的数字。
例如:
13438461673这样13438461673 * 1000000000我如何将它放入 64 位整数中?
让我们从一个例子开始,我想这将证明我正在处理的问题.这是一个简单的测试程序,远非现实,但它确实很好地说明了问题
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 struct first {
5 int i_value;
6 };
7
8 struct second {
9 float f_value;
10 };
11
12 #define DEFINE_FUNCTION(type, struct_name, field_name) \
13 void my_ ## type ## _function(struct struct_name *object, type value) \
14 { \
15 /* Deliberately read an uninitialized value to make valgrind */ \
16 /* report the issue */ \
17 if (object->field_name == -1) \
18 return; \
19 object->field_name = …Run Code Online (Sandbox Code Playgroud) 我正在尝试从遗留代码中获取服务并在injector()返回undefined时遇到奇怪的错误:
非常感谢,非常感谢任何指针或建议.
我创建了以下函数来获取日期时间字符串:
char *GetDateTime (int Format)
{
if (Format > 2) Format = 0;
double DateTimeNow;
int BufferLen;
char *DateTimeFormat [3] = { "%X %x" , //Time Date
"%x" , //Date
"%X" }; //Time
char *DateTimeBuffer = NULL;
GetCurrentDateTime (&DateTimeNow);
BufferLen = FormatDateTimeString (DateTimeNow, DateTimeFormat [Format], NULL, 0);
DateTimeBuffer = malloc (BufferLen + 1);
FormatDateTimeString (DateTimeNow, DateTimeFormat [Format], DateTimeBuffer, BufferLen + 1 );
return DateTimeBuffer;
}
Run Code Online (Sandbox Code Playgroud)
我不释放“DateTimeBuffer”,因为我需要传递它的内容。我想知道那段记忆是否会自行清除。请帮忙。
我正在尝试为 C 中的数组排序函数提供一个 python 包装器。C 接受数组,按从小到大对整数进行排序,然后返回数组。但是当我运行它时,我收到错误:
Traceback (most recent call last):
File "sortarray.py", line 25, in <module>
newarray = sortArray(array)
File "sortarray.py", line 8, in sortArray
libsortarray.sortArray.argtypes = (ctypes.c_int, ctypes.POINTER(ctypes.c_int))
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 378, in __getattr__
func = self.__getitem__(name)
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 383, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: dlsym(0x7f84484280e0, sortArray): symbol not found
Run Code Online (Sandbox Code Playgroud)
Python:
import ctypes
libsortarray = ctypes.CDLL('libsortarray.so')
def sortArray(array):
global libsortarray
libsortarray.sortArray.argtypes = (ctypes.c_int, ctypes.POINTER(ctypes.c_int))
arraySize = len(array)
array_type = ctypes.c_int * arraySize
result = libsortarray.sortArray(ctypes.c_int(arraySize), …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 MS Visual-Studio 2013 运行用 c 编写的 unix 编译器项目,但无法摆脱以下错误:
error LNK2019: unresolved external symbol "_snprintf" referenced in
function "PUBLIC void SyntaxError( int Expected, TOKEN CurrentToken )"
Run Code Online (Sandbox Code Playgroud)
如果我做对了,那就是 VisualStudio 无法从snprintf()函数中找到主体/声明的问题,该函数应该在stdio.h.
该项目适用于 cygwin。我必须添加_CRT_SECURE_NO_WARNINGS到预处理器设置才能做到这一点,但我认为这没有影响。
这是命名函数:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "line.h"
#include "strtab.h"
#include "scanner.h"
[..code..]
PUBLIC void SyntaxError( int Expected, TOKEN CurrentToken )
{
char s[M_LINE_WIDTH+2];
snprintf( s, sizeof(s), "Syntax: Expected %s, got %s\n", Tokens[Expected], Tokens[CurrentToken.code] );
Error( s, CurrentToken.pos );
} …Run Code Online (Sandbox Code Playgroud) 我无法弄清
我的Valgrind给出的
代码中的内存泄漏
==26373== HEAP SUMMARY:
==26373== in use at exit: 24 bytes in 1 blocks
==26373== total heap usage: 7 allocs, 6 frees, 136 bytes allocated
==26373==
==26373== 24 bytes in 1 blocks are definitely lost in loss record 1 of 1
==26373== at 0x4C2ABBD: malloc (vg_replace_malloc.c:296)
==26373== by 0x400592: graph_init (in /media/goutam/CommonPartition/My Research/DataStructures/Graphs/main)
==26373== by 0x40081D: main (in /media/goutam/CommonPartition/My Research/DataStructures/Graphs/main)
Run Code Online (Sandbox Code Playgroud)
这是创建内存泄漏的代码
/*
* Allocates memory for graph,adjacency matrix and nodes
*/
graph_t* graph_init(int n){
graph_t *graph = malloc(sizeof(graph_t)); …Run Code Online (Sandbox Code Playgroud) 谁能帮我?两者之间有什么区别吗?
if (!n / 10)
return;
Run Code Online (Sandbox Code Playgroud)
和
if (n / 10 == 0)
return;
Run Code Online (Sandbox Code Playgroud) 好吧,我一直在google上搜索很多关于如何正确刷新输入流的问题.我听到的只是关于如何为输入流定义fflush()的混合论点,有些人说只是这样做,而其他人只是说不要这样做,我没有太多运气找到明确的效率/这样做的正确方法,大多数人都同意..我在编程方面很陌生,所以我还不知道该语言的所有语法/技巧,所以我的问题是哪种方式是最有效/最合适的解决方案清除C输入流??
getchar()在我尝试接收更多输入之前使用两次?
只需fflush()在输入上使用该功能?要么
这就是我认为我应该这样做的方式.
void clearInputBuf(void);
void clearInputBuf(void)
{
int garbageCollector;
while ((garbageCollector = getchar()) != '\n' && garbageCollector != EOF)
{}
}
Run Code Online (Sandbox Code Playgroud)因此,每当我需要读取一个新的scanf(),或者使用getchar()来暂停程序时,我只需要调用clearInputBuf ..那么这三种解决方案中最好的方法是什么?还是有更好的选择?