我几天来一直在调试臭名昭着的EXC_BAD_ACCESS错误.NSZombieEnabled = YES没有提供任何东西.每次收到错误时,调用堆栈都不同,每隔5或6次运行一次.
我在Lou Franco的网站上看到了启用guard malloc(现在是Xcode 4的方案编辑器)的提示:了解EXC_BAD_ACCESS.一旦我这样做,我的程序就停止了导致这个难以捉摸的错误的确切行.
根据其描述,guard malloc为每个malloc创建单独的页面,并在释放内存时删除整个页面,从而在访问释放的内存时使程序崩溃.对于一般的开发,为什么我不能一直保持对malloc的保护?它似乎很容易捕获某些类型的内存错误.如果我没有专门测试内存管理或性能,那么使用它有一些缺点吗?
如何设置现有NSDate的时间?
那就是我有一个约会(比如说当前日期/时间),但是想把它设置为特定的时间(例如11.23am).在目标C中最快的方法是什么?
我在看NSCalendar,看的方法,如dateByAddingComponents:toDate:options:,和dateFromComponents:,但这些似乎并没有很切中要害了什么,我需要在这里.例如:
dateFromComponents: - 我必须解决所有这些工作的组件,这似乎有点矫枉过正dateByAddingComponents:toDate:options: - 我不想在我的情况下添加,而是"设置"时间.我编写了以下代码来获取元组元素的偏移量
template<size_t Idx,class T>
constexpr size_t tuple_element_offset() {
return static_cast<size_t>(
reinterpret_cast<char*>(&std::get<Idx>(*reinterpret_cast<T*>(0))) - reinterpret_cast<char*>(0));
}
Run Code Online (Sandbox Code Playgroud)
这实际上类似于offsetof宏的实现.它看起来很难看,但在gcc-4.6上编译并正常工作
typedef std::tuple<int,char,long> mytuple;
mytuple var = std::make_tuple(4,'c',1000);
char * ptr = reinterpret_cast<char*>(&var);
long * pt = reinterpret_cast<long*>(ptr+tuple_element_offset<2,mytuple>());
std::cout << *pt << std::endl;
Run Code Online (Sandbox Code Playgroud)
打印"1000".
我对constexpr了解不多,所以我的问题是:
据我所知,constexpr,编译器被迫在编译时评估表达式的结果,因此在实践中不会发生零去引用.
我试图通过向左边的零填充数字来生成数字字符串.
0将成为00000 1将成为00001 10将成为00010
我想NSString通过用零填充数字来创建五个字符.
我通过重复另一个字符串给定次数来读取这个创建NSString,但输出是一个NSMutableString.
如何将输出作为一个实现此算法NSString?
最好的祝福.
如果我在Firefox 4中创建一个IndexedDB,那么存储在我的硬盘上的文件在哪里?最好是对于Win7,但路径在OS之间可能是相似的.
在about_symbols.rb Ruby Koan(https://github.com/edgecase/ruby_koans)中,我有以下代码:
RubyConstant = "What is the sound of one hand clapping?"
def test_constants_become_symbols
all_symbols = Symbol.all_symbols
assert_equal true, all_symbols.include?(:"nonexistent")
assert_equal true, all_symbols.include?(:"What is the sound of one hand clapping?")
assert_equal true, all_symbols.include?("What is the sound of one hand clapping?".to_sym)
end
Run Code Online (Sandbox Code Playgroud)
按原样,测试通过.
三个问题:
为什么第一个断言通过?:"nonexistent"不应该包含在all_symbols中,但它包括在内,所以我必须误解一些东西.
当我注释掉第二个断言时,测试失败,因为"What is the sound of one hand clapping?".to_sym不包括在all_symbols中,:"What is the sound of one hand clapping?"而是包括在内.既然它们是等价的,为什么最后一个断言失败了?另外,为什么第二个断言未被注释时它会通过?(为什么第二个断言对第三个断言有影响?)
据我所知,这个Ruby Koan的目的是证明常量成为符号(至少,这是我从方法名称推断的).由于RubyConstant是一个带值的常量"What is the sound of one hand clapping?",为什么不"What is the …
我查看了Apple"PageControl"示例项目(见这里),我在代码中看到了这个函数:
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
// replace the placeholder if necessary
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
NSDictionary *numberItem = [self.contentList …Run Code Online (Sandbox Code Playgroud) 我已经启动了一个WPF(基于Web)应用程序,它有多个页面.我想在WPF中实现Master页面的概念,这样当我在一个页面的设计/布局中进行更改时,所有其他页面都会跟进而不需要复制和粘贴.
我正在做一种叫做"叠画"的技巧.确定特定函数使用多少堆栈空间.
如果我在堆栈上分配了 1MB的物品.然后我确定我没有使用任何这些项目.Windows会自动解除(免费)那些未使用的页面吗?
我特别想知道Windows的VMM.关于页面是否已经提交或者不一定是已经提交但只是保留?
换句话说,如果我手动访问内存到1MB可能会导致Windows违反访问冲突?