小编pmg*_*pmg的帖子

unix中read和pread有什么区别?

函数read()pread()unix有什么区别?
在他们之间做出选择时,我应该考虑哪些方面?

我用谷歌搜索他们之间的差异,但没有结果.

c unix

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

将clock_gettime移植到窗口

我在qnx momemntics上运行以下代码.

#define BILLION 1000000000L;

struct timespec start_time;
struct timespec stop_time; 

void start MyTestFunc() {
    //Initialize the Test Start time
     clock_gettime(CLOCK_REALTIME,&start_time)
    // ... additonal code.

    cout << "The exectuion time of func "<< calculateExecutionTime();
}


double calculateExecutionTime ()
{

    clock_gettime(CLOCK_REALTIME,&stop_time);

    double dSeconds = (stop_time.tv_sec - start_time.tv_sec);

    double dNanoSeconds = (double)( stop_time.tv_nsec - start_time.tv_nsec ) / BILLION;

    return dSeconds + dNanoSeconds;
}
Run Code Online (Sandbox Code Playgroud)

现在我想将代码移植到Windows上.任何人都可以提供示例代码.

谢谢!

c c++ windows

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

如何在shell脚本中抑制diff的所有输出?

问候,

我想知道是否有办法抑制diff命令的所有输出,以便它不输出差异但只返回成功状态?

diff $FILE1 $FILE2
if [ $? -ne 0 ];then
    echo Does not match output.
else
    echo Match.
Run Code Online (Sandbox Code Playgroud)

shell diff

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

是否未定义行为抛弃函数参数的常量?

想象一下,我有这个C函数(以及头文件中的相应原型)

void clearstring(const char *data) {
    char *dst = (char *)data;
    *dst = 0;
}
Run Code Online (Sandbox Code Playgroud)

有未定义行为在上面的代码,铸造const,或者是它只是一个非常不好的编程习惯?

假设没有使用const限定对象

char name[] = "pmg";
clearstring(name);
Run Code Online (Sandbox Code Playgroud)

c casting const undefined-behavior language-lawyer

15
推荐指数
1
解决办法
1510
查看次数

如何在表格中找到"洞"

我最近继承了一个数据库,其中一个表的主键由编码值组成(Part1*1000 + Part2).
我将该列标准化,但我无法更改旧值.所以现在我有

select ID from table order by ID
ID
100001
100002
101001
...
Run Code Online (Sandbox Code Playgroud)

我想找到表格中的"洞"(更准确地说,是100000之后的第一个"洞").
我正在使用以下选择,但是有更好的方法吗?

select /* top 1 */ ID+1 as newID from table
where ID > 100000 and
ID + 1 not in (select ID from table)
order by ID

newID
100003
101029
...
Run Code Online (Sandbox Code Playgroud)

该数据库是Microsoft SQL Server 2000.我可以使用SQL扩展.

sql

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

strdup在传递NULL时转储核心

strdup(null)转储核心.

尝试了ubuntu和freeBSD.

为什么?它不应该返回null吗?

char *b = NULL;
a = strdup(b);
Run Code Online (Sandbox Code Playgroud)

这将在strdup调用上转储核心.

c linux compiler-construction posix freebsd

9
推荐指数
2
解决办法
9303
查看次数

x的冲突类型和先前的声明在这里......什么?

当我有时间的时候,我一直在教自己C几个月,我遇到了一个问题我不知道如何解决.

具体来说,当我尝试使用gcc编译它时,我得到:

geometry.c:8: error: conflicting types for ‘trapezoid’
geometry.c:7: note: previous declaration of ‘trapezoid’ was here
geometry.c:48: error: conflicting types for ‘trapezoid’
geometry.c:7: note: previous declaration of ‘trapezoid’ was here
geometry.c:119: error: conflicting types for ‘trapezoid_area’
geometry.c:59: note: previous implicit declaration of ‘trapezoid_area’ was here
geometry.c: In function ‘cone_volume’:
geometry.c:128: error: called object ‘3.14100000000000001421085471520200371742248535156e+0’ is not a function
geometry.c: In function ‘cylinder_volume’:
geometry.c:136: error: called object ‘3.14100000000000001421085471520200371742248535156e+0’ is not a function

现在,我想我可能需要对这些功能进行类型转换,但同样,我不确定.

看起来它想要读取我定义为3.141的PI作为函数.有没有办法可以避免使用魔法数字3.141(虽然它比其他数字少得多)?

//Geometric formulae
#include <stdio.h>
#include <math.h>

#define …
Run Code Online (Sandbox Code Playgroud)

c function void

8
推荐指数
2
解决办法
5万
查看次数

如何配置组合python和c代码

我有一个由多个python脚本组成的应用程序.其中一些脚本正在调用C代码.应用程序现在运行速度比现在慢得多,因此我想对其进行分析以查看问题所在.是否有工具,软件包或只是一种方式来分析这样的应用程序?一个工具,它将遵循python代码到C代码并配置这些调用?

注1:我很清楚标准的Python分析工具.我特意在这里寻找组合的Python/C分析.

注2:Python模块使用ctypes调用C代码(有关详细信息,请参阅http://docs.python.org/library/ctypes.html).

谢谢!

c python profiling

7
推荐指数
1
解决办法
1074
查看次数

如何指定是否取得编组字符串的所有权?

假设我在C++中有x.dll,看起来像这样

MYDLLEXPORT
const char* f1()
{
   return "Hello";
}

MYDLLEXPORT
const char* f2()
{
   char* p = new char[20];
   strcpy(p, "Hello");
   return p;   
}
Run Code Online (Sandbox Code Playgroud)

现在,假设我想在C#中使用它

[DllImport("x.dll")]
public static extern string f1();

[DllImport("x.dll")]
public static extern string f2();
Run Code Online (Sandbox Code Playgroud)

有没有办法告诉CLR对从f2返回的字符串采取强有力的所有权,但不是f1?事实是,从f1返回的字符串最终将被GC释放,删除或其他任何事实同样糟糕的事实是从f2返回的字符串不会.希望问题很清楚.提前致谢

c# c++ interop marshalling

7
推荐指数
1
解决办法
260
查看次数

不应该这个代码崩溃

int *p;
while(true)
{
 p = new int;
}
Run Code Online (Sandbox Code Playgroud)

由于内存空间不足,此代码不应该崩溃.我已经尝试打印出p的值,即p的内存地址,它似乎增加但没有崩溃.

为什么会这样?

c++ new-operator dynamic-memory-allocation

6
推荐指数
3
解决办法
469
查看次数