我正在尝试检查字符串是否包含C中的子字符串,如:
char *sent = "this is my sample example";
char *word = "sample";
if (/* sentence contains word */) {
/* .. */
}
Run Code Online (Sandbox Code Playgroud)
什么是使用而不是string::find在C++中?
我用 C 语言实现了一个冒泡排序,并在测试其性能时发现该-O3标志使其运行速度甚至比没有标志时还要慢!与此同时-O2,它的运行速度比预期的要快得多。
没有优化:
time ./sort 30000
./sort 30000 1.82s user 0.00s system 99% cpu 1.816 total
Run Code Online (Sandbox Code Playgroud)
-O2:
time ./sort 30000
./sort 30000 1.00s user 0.00s system 99% cpu 1.005 total
Run Code Online (Sandbox Code Playgroud)
-O3:
time ./sort 30000
./sort 30000 2.01s user 0.00s system 99% cpu 2.007 total
Run Code Online (Sandbox Code Playgroud)
代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int n;
void bubblesort(int *buf)
{
bool changed = true;
for (int i = n; changed == true; …Run Code Online (Sandbox Code Playgroud) 目前,我们正在定义一个扩展日志机制来打印出日志的类名和源行号.
#define NCLog(s, ...) NSLog(@"<%@:%d> %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
__LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__])
Run Code Online (Sandbox Code Playgroud)
例如,当我调用NCLog(@"Hello world")时; 输出将是:
<ApplicationDelegate:10>Hello world
Run Code Online (Sandbox Code Playgroud)
现在我还想注销方法名称,如:
<ApplicationDelegate:applicationDidFinishLaunching:10>Hello world
Run Code Online (Sandbox Code Playgroud)
因此,当我们知道调用哪种方法时,这将使我们的调试变得更容易.我知道我们也有Xcode调试器,但有时候,我也希望通过注销来进行调试.
该块中提到的所有功能都是库函数.我怎样才能纠正这个内存泄漏?
它列在" 仍可达 "类别下.(还有4个,非常相似,但尺寸各异)
630 bytes in 1 blocks are still reachable in loss record 5 of 5
at 0x4004F1B: calloc (vg_replace_malloc.c:418)
by 0x931CD2: _dl_new_object (dl-object.c:52)
by 0x92DD36: _dl_map_object_from_fd (dl-load.c:972)
by 0x92EFB6: _dl_map_object (dl-load.c:2251)
by 0x939F1B: dl_open_worker (dl-open.c:255)
by 0x935965: _dl_catch_error (dl-error.c:178)
by 0x9399C5: _dl_open (dl-open.c:584)
by 0xA64E31: do_dlopen (dl-libc.c:86)
by 0x935965: _dl_catch_error (dl-error.c:178)
by 0xA64FF4: __libc_dlopen_mode (dl-libc.c:47)
by 0xAE6086: pthread_cancel_init (unwind-forcedunwind.c:53)
by 0xAE61FC: _Unwind_ForcedUnwind (unwind-forcedunwind.c:126)
Run Code Online (Sandbox Code Playgroud)
Catch:一旦我运行我的程序,它没有内存泄漏,但它在Valgrind输出中有一个额外的行,之前没有出现:
由于munmap()而丢弃/lib/libgcc_s-4.4.4-20100630.so.1中的0x5296fa0-0x52af438处的syms
如果泄漏无法纠正,有人可以解释为什么munmap()行导致Valgrind报告0"仍然可达"泄漏?
编辑:
这是一个最小的测试样本:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *runner(void …Run Code Online (Sandbox Code Playgroud) 我收到这个错误:
警告:内置函数'malloc'的不兼容隐式声明
我想这样做:
fileinfo_list* tempList = malloc(sizeof(fileinfo_list));
Run Code Online (Sandbox Code Playgroud)
仅供参考,手头使用的结构是:
typedef struct {
fileinfo** filedata;
size_t nFiles;
size_t size;
size_t fileblock;
} fileinfo_list;
Run Code Online (Sandbox Code Playgroud)
我没有看到我所做的事情有什么不妥.我只是创建一个tempList1 x的大小fileinfo_list.
C中的Integer变量是占用2个字节还是4个字节?它取决于哪些因素?
大多数教科书都说整数变量占用2个字节.但是当我运行程序打印整数数组的连续地址时,它显示了4的差异.
我在让C套接字API在C++上正常工作时遇到问题z/OS.
虽然我包括在内sys/socket.h,但我仍然遇到编译时错误,告诉我AF_INET没有定义.
我是否遗漏了一些显而易见的事情,或者这与这样一个事实相关:继续z/OS使我的问题变得更加复杂?
更新:经过进一步调查,我发现有一个#ifdef我正在打击.z/OS除非我定义我使用的哪种"类型"套接字,否则显然不高兴:
#define _OE_SOCKETS
Run Code Online (Sandbox Code Playgroud)
现在,我个人不知道这_OE_SOCKETS实际上是什么,所以如果任何z/OS套接字程序员在那里(你们三个人),或许你可以给我一个如何运作的概述?
测试应用程序
#include <sys/socket.h>
int main()
{
return AF_INET;
}
Run Code Online (Sandbox Code Playgroud)
编译/链接输出:
cxx -Wc,xplink -Wl,xplink -o inet_test inet.C
"./inet.C", line 5.16: CCN5274 (S) The name lookup for "AF_INET" did not find a declaration.
CCN0797(I) Compilation failed for file ./inet.C. Object file not created.
Run Code Online (Sandbox Code Playgroud)
检查sys/sockets.h确实包含了我需要的定义,据我所知,它没有被任何#ifdef语句阻止.
但我注意到它包含以下内容:
#ifdef __cplusplus
extern "C" {
#endif
Run Code Online (Sandbox Code Playgroud)
它基本上封装了整个文件.不确定是否重要.
假设我有'numb'=1025 [00000000 00000000 00000100 00000001]代表的数字:
在Little-Endian机器上:
00000001 00000100 00000000 00000000
Run Code Online (Sandbox Code Playgroud)
在Big-Endian机器上:
00000000 00000000 00000100 00000001
Run Code Online (Sandbox Code Playgroud)
现在,如果我在10位上应用Left Shift(即:numb << = 10),我应该:
[A]在Little-Endian机器上:
正如我在GDB中注意到的那样,Little Endian通过3个步骤执行左移:[我已经显示'3'步骤以更好地理解处理]
对待没有.在Big-Endian公约中:
00000000 00000000 00000100 00000001
Run Code Online (Sandbox Code Playgroud)应用左移:
00000000 00010000 00000100 00000000
Run Code Online (Sandbox Code Playgroud)再次在Little-Endian中表示结果:
00000000 00000100 00010000 00000000
Run Code Online (Sandbox Code Playgroud)[B].在Big-Endian机器上:
00000000 00010000 00000100 00000000
Run Code Online (Sandbox Code Playgroud)
我的问题是:
如果我直接在Little Endian公约上应用左移,它应该给:
numb:
00000001 00000100 00000000 00000000
Run Code Online (Sandbox Code Playgroud)
numb << 10:
00010000 00000000 00000000 00000000
Run Code Online (Sandbox Code Playgroud)
但实际上,它给出了:
00000000 00000100 00010000 00000000
Run Code Online (Sandbox Code Playgroud)
为了达到第二个结果,我在上面展示了三个假设步骤.
请解释一下为什么上述两个结果不同:实际结果numb << 10与预期结果不同.
在用C/C++编写的小应用程序中,我遇到了一个问题 rand函数也许是种子:
我想生成一系列具有不同顺序的随机数,即具有不同的对数值(基数为2).但似乎所有产生的数字都是相同的顺序,波动在2 ^ 25到2 ^ 30之间.
是因为rand()用Unix时间播种,现在是一个相对较大的数字?我忘记了什么?我rand()只在一开始播种一次main().