小编pf8*_*f85的帖子

从两个(64位)整数获得可靠的整数百分比

在我的平台上,unsigned long long是64位(8字节).假设我有两个这样的变量:

unsigned long long partialSize;
unsigned long long totalSize;
//somehow determine partialSize and totalSize
Run Code Online (Sandbox Code Playgroud)

如何可靠地确定百分比(四舍五入到附近的整数)partialSizetotalSize多少?(如果可能的话,如果我不必假设前者小于后者,那将是很好的,但如果我真的必须做出这个假设,那就没关系.但我们当然可以假设两者都是非-负.)

例如,以下代码是完全防弹的吗?我担心它会包含某种舍入,转换或转换错误,这些错误可能导致比率在某些条件下失控.

unsigned long long ratioPercentage
    = (unsigned long long)( ((double)partialSize)/((double)totalSize) * 100.0 );
Run Code Online (Sandbox Code Playgroud)

c int 64-bit computer-science

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

这些对象何时在ARC下发布?

我有几个关于ARC(自动引用计数)的问题:

CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
//Question 1: Here, I would expect the NSURL object to be autoreleased and
//therefore the CFURLRef will also be available for “a while.” Is this correct?

url = NULL;
//Question 2: Will this cause the NSURL to be released immediately?
Run Code Online (Sandbox Code Playgroud)
NSURL *url = [NSURL fileURLWithPath:appPath];
url = nil;
//Question 3: Does the “url = nil” result in an immediate release of the NSURL?
Run Code Online (Sandbox Code Playgroud)
NSURL *url = [[NSURL alloc] initWithString:@"/something"];
url = nil;
//Question …
Run Code Online (Sandbox Code Playgroud)

cocoa cocoa-touch objective-c core-foundation automatic-ref-counting

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

在Core Foundation中保留和"自动释放"

假设我正在编写自己的函数,该函数接收CFDataRef对象,对其执行某些操作,并返回另一个CFDataRef对象:

CFDataRef transformData(CFDataRef inData)
{
  //Question 1: Should I call CFRetain(data) here to make sure it doesn't
  //go away? (This of course would involve releasing data just before returning
  //from this function, or as soon as I no longer need data.)

  CFDataRef outData;

  //Somehow produce the new outData from inData (and assume we are the
  //owner of outData, since we created it right here).

  //Question 2: What, if anything, should I do with outData before
  //returning it? I'm unsure of …
Run Code Online (Sandbox Code Playgroud)

c objective-c core-foundation

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

ABRecordSetValue返回的CFErrorRef的内存管理

考虑一些涉及错误处理的典型CF代码,比如说:

ABRecordRef aRecord = ABPersonCreate();
CFErrorRef anError = NULL;
ABRecordSetValue(aRecord, kABPersonFirstNameProperty, CFSTR("Joe"), &anError);
Run Code Online (Sandbox Code Playgroud)

anError这段代码后如何处理?我是否必须保留它,以确保它不会消失,然后再释放它?或者我已经是主人了,我只需要稍后发布它?

c objective-c core-foundation

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

保证4*ceil(n/3)的足够存储空间,其中n是int

假设n是一个整数(intC中的变量).我需要足够的空间"4倍于n的上限除以3"字节.我如何保证足够的空间?

您是否认为malloc(4*(int)ceil(n/3.0))会这样做,或者我必须添加,比如说,1为了绝对安全(由于可能的舍入错误)?

c malloc computer-science

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

阐明 OpenSSL 中的 EVP_BytesToKey() 函数

我在看这个页面:http : //www.openssl.org/docs/crypto/EVP_BytesToKey.html

在那里,它说:

如果总密钥和 IV 长度小于摘要长度并且使用 MD5,则派生算法与 PKCS#5 v1.5 兼容,否则使用非标准扩展来派生额外数据。

我正在使用 AES-256-CBC 密码和 MD5。从上面的摘录来看,这告诉我什么?这是否意味着我与 PKCS#5 v1.5 兼容,还是意味着它使用了一些非标准的东西?

c openssl cryptography

0
推荐指数
1
解决办法
3288
查看次数