小编Iha*_*imi的帖子

无符号类型与带符号类型的比较

我的编译器(DSP SHARC)非常挑剔。当我用备注进行构建时,出现此错误:

[cc1123] foo.c:1511 {D} remark: comparison of unsigned type
          with signed type
      while (taille > 0)
Run Code Online (Sandbox Code Playgroud)

两种解决方案:

  1. 编译器是正确的,我应该写

    size_t taille;
    ...
    while(taille > (size_t)0)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 编译器很愚蠢,我应该忽略这一点

  3. 可能涉及ISO或MISRA标准的另一种解决方案

我该怎么办?

编辑

其实我最好写这样的例子

 while(taille) {...}
Run Code Online (Sandbox Code Playgroud)

但是,这与我最初的问题无关

c

4
推荐指数
1
解决办法
105
查看次数

如何使用c在Linux中获取当前时间戳(以纳秒为单位)

我知道我们可以使用clock_gettime(CLOCK_MONOTONIC).

我尝试问的问题是,如果我需要从纪元开始的纳秒时间,那将是一个巨大的数字。

例如:

  • 自纪元以来的秒数是13438461673这样13438461673 * 1000000000

我如何将它放入 64 位整数中?

c linux

4
推荐指数
1
解决办法
2万
查看次数

如何使用valgrind实际上是宏的扩展

让我们从一个例子开始,我想这将证明我正在处理的问题.这是一个简单的测试程序,远非现实,但它确实很好地说明了问题

 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)

c macros gcc valgrind

4
推荐指数
2
解决办法
218
查看次数

注入器返回未定义的值?

  1. 我正在尝试从遗留代码中获取服务并在injector()返回undefined时遇到奇怪的错误:

    检查这个plnkr


  1. 另外,我试图将新的属性值重新设置回服务,是否会在不使用手表的情况下反映到范围内?

非常感谢,非常感谢任何指针或建议.

angularjs

3
推荐指数
1
解决办法
8295
查看次数

C 会自动释放函数内分配的内存吗?

我创建了以下函数来获取日期时间字符串:

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 malloc free

3
推荐指数
1
解决办法
4407
查看次数

Ctypes 返回数组

我正在尝试为 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)

c python arrays ctypes pointers

3
推荐指数
1
解决办法
3917
查看次数

从 cygwin 移动到 VisualStudio2013,错误 LNK2019,snprintf(),c

我正在尝试使用 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)

c cygwin stdio linker-errors visual-studio-2013

3
推荐指数
1
解决办法
1138
查看次数

这段代码中的内存泄漏在哪里?

我无法弄清
我的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)

c memory-leaks

3
推荐指数
1
解决办法
138
查看次数

3
推荐指数
1
解决办法
156
查看次数

这是刷新C输入流的正确方法吗?

好吧,我一直在google上搜索很多关于如何正确刷新输入流的问题.我听到的只是关于如何为输入流定义fflush()的混合论点,有些人说只是这样做,而其他人只是说不要这样做,我没有太多运气找到明确的效率/这样做的正确方法,大多数人都同意..我在编程方面很陌生,所以我还不知道该语言的所有语法/技巧,所以我的问题是哪种方式是最有效/最合适的解决方案清除C输入流??

  1. getchar()在我尝试接收更多输入之前使用两次?

  2. 只需fflush()在输入上使用该功能?要么

  3. 这就是我认为我应该这样做的方式.

    void clearInputBuf(void);  
    void clearInputBuf(void) 
    { 
          int garbageCollector; 
          while ((garbageCollector = getchar()) != '\n' && garbageCollector != EOF) 
          {}
    }
    
    Run Code Online (Sandbox Code Playgroud)

因此,每当我需要读取一个新的scanf(),或者使用getchar()来暂停程序时,我只需要调用clearInputBuf ..那么这三种解决方案中最好的方法是什么?还是有更好的选择?

c flush fflush

3
推荐指数
2
解决办法
1815
查看次数