我想接口的Lua使用Objective-C,我觉得字符串转换与NSSelectorFromString()有太大的开销,因为lua有复制所有字符串内在它们(虽然我不知道这个).
所以我试图找到更轻量级的方式来表示Lua中的选择器.Objective-C选择器是一种抽象类型,但它被定义为指向某事物的指针:
typedef struct objc_selector *SEL;
Run Code Online (Sandbox Code Playgroud)
因此,作为常规指针处理它看起来很安全,所以我可以将其传递给Lua lightuserdata.这样好吗?
#include <iostream>
enum IsOptionAEnum
{
IsOptionA_YES,
IsOptionA_NO
};
enum IsOptionBEnum
{
IsOptionB_YES,
IsOptionB_NO
};
void TestFunc(IsOptionAEnum optionA, IsOptionBEnum optionB)
{
if (optionA == IsOptionA_YES || optionA == IsOptionB_YES) // typo
{
// ...
}
//if (optionA == IsOptionA_YES || optionB == IsOptionB_YES) // correct one
//{
//}
}
Run Code Online (Sandbox Code Playgroud)
问题> optionA是类型IsOptionAEnum,没有值IsOptionB_YES.为什么VS2010的编译器找不到这个错误?
如果是编译器无法找到错误的情况,有没有办法可以强制执行此限制,以便编译器可以找到错误?
我正在做另一个C++练习.我必须从无限系列计算pi的值:
pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 -4/11 +...
该程序必须在本系列的前1000个术语中的每一个之后打印pi的近似值.这是我的代码:
#include <iostream>
using namespace std;
int main()
{
double pi=0.0;
int counter=1;
for (int i=1;;i+=2)//infinite loop, should "break" when pi=3.14159
{
double a=4.0;
double b=0.0;
b=a/static_cast<double>(i);
if(counter%2==0)
pi-=b;
else
pi+=b;
if(i%1000==0)//should print pi value after 1000 terms,but it doesn't
cout<<pi<<endl;
if(pi==3.14159)//this if statement doesn't work as well
break;
counter++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译时没有错误和警告,但执行后只显示空控制台窗口.如果我删除行"if(i%1000 == 0)",我可以看到它确实运行并打印每个pi值,但它不会停止,这意味着第二个if语句也不起作用.我不知道还能做什么.我假设它可能是一个简单的逻辑错误.
我有一个琐碎的司机,像这样:
#include <ntddk.h>
NTSTATUS DriverEntry(__in DRIVER_OBJECT* a, __in UNICODE_STRING* b)
{
UNREFERENCED_PARAMETER(a);
UNREFERENCED_PARAMETER(b);
int c; // this fails the build
return 0;
}
Run Code Online (Sandbox Code Playgroud)
一个简单的makefile
TARGETNAME=main
TARGETTYPE=DRIVER
MSC_WARNING_LEVEL=/W4 /WX
SOURCES=main.c
Run Code Online (Sandbox Code Playgroud)
使用非trival构建输出
C:\Test>pushd %cd%
C:\Test>C:\WinDDK\7600.16385.1\bin\setenv.bat C:\WinDDK\7600.16385.1 fre x64 wnet
WARNING: x64 Native compiling isn't supported. Using cross compilers.
Launching OACR monitor
C:\WinDDK\7600.16385.1>popd
C:\Test>build
BUILD: Compile and Link for AMD64
BUILD: Loading c:\winddk\7600.16385.1\build.dat...
BUILD: Computing Include file dependencies:
BUILD: Start time: Thu Jan 17 10:57:58 2013
BUILD: Examining c:\test directory for files to …Run Code Online (Sandbox Code Playgroud) 我有一个字节数组(因为无符号字节不是一个选项),需要将其中的4个转换为32位int.我正在使用这个:
byte rdbuf[] = new byte[fileLen+1];
int i = (rdbuf[i++]) | ((rdbuf[i++]<<8)&0xff00) | ((rdbuf[i++]<<16)&0xff0000) | ((rdbuf[i++]<<24)&0xff000000);
Run Code Online (Sandbox Code Playgroud)
如果我不做所有的逻辑和,它标志扩展字节,这显然不是我想要的.
在c中,这将是一个没有脑子的人.Java中有更好的方法吗?
我正在使用ARC和ios sdk 6.0.
我很确定我有一些内存泄漏,我无法追踪.
运行静态分析器后,我会收到有关以下两种方法的警告:
+ (id<MXURLRequest>) requestWithURL:(NSURL*)url {
MXASIURLRequest *request = [[MXASIURLRequest alloc] init];
[request setUrl:url];
return request; // STATIC ANALYSER: Potential leak of an object stored into 'request'
}
- (id)parseBody:(NSError *)error {
NSString *contentType = [[_request responseHeaders] objectForKey:@"Content-Type"];
id body = nil;
if ([contentType hasPrefix:@"application/json"] ||
[contentType hasPrefix:@"text/json"] ||
[contentType hasPrefix:@"application/javascript"] ||
[contentType hasPrefix:@"text/javascript"]) {
body = [NSJSONSerialization JSONObjectWithData:[_request responseData] options:NSJSONReadingMutableLeaves error:&error];
} else if ([contentType hasPrefix:@"image/"] ||
[contentType hasPrefix:@"audio/"] ||
[contentType hasPrefix:@"application/octet-stream"]) {
body = [_request …Run Code Online (Sandbox Code Playgroud) 例如:
$str .= "Additional tax(2.34)";
$str .= "Additional tax(3)";
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我从字符串中提取数字.我需要这样的数组格式
Array ( [0] => 2.34 [1] => 3 );
Run Code Online (Sandbox Code Playgroud)