一个简单的问题,虽然我无法在更快的时间内找到答案.
我通过'new'关键字使用动态分配来为数组分配内存块,如
int *array = new int[size]; //The Size is got by some logic
Run Code Online (Sandbox Code Playgroud)
现在从逻辑的长远来看,我需要解析这个数组,这样我需要得到/计算这个数组的大小.
我在这里很无能为力.请帮帮我.
提前致谢 :)
我试图理解C++中的内存分配.我想到的一个问题是为什么分配内存是如此必要?如果我们在不分配内存的情况下使用内存会发生什么?另外,我很震惊地看到C++在内存分配方面是多么粗心.如果通过没有边界检查的数组提供对内存的自由访问.
int main()
{
int *p = new int[5];
p[1] = 3;
p[11118] = 9;
cout<<p[11118]<<'\n';
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作,输出9.
在什么情况下将值分配给未分配的内存位置会很危险?什么是潜在的不良影响?我正在访问的内存位置是否可能已分配给某个其他程序并为其分配值可能会导致该程序以非常意外的方式崩溃/表现?
我有一个我保留的NSString,我释放它的唯一地方是dealloc方法.但是,出于某种原因,稍后在程序中我尝试引用它(具体来说是它的长度)时,我得到一个崩溃,说[CFString length]:消息发送到解除分配的实例0xff32c50.
我在程序的早期明确地保留了字符串.有什么理由会发生这种情况吗?任何帮助表示赞赏.
字符串entityParameter在标头中声明,稍后定义.以下是一些代码:
entityParameter = [[EntitySearchWindow stringByEvaluatingJavaScriptFromString:@"f();"] retain];
我遇到崩溃的地方看起来像这样:
if([entityParameter length] != 0 && entityParameter != nil)
{
return;
}
我正在使用TBXML解析器,但遇到了一些困难.
我使用以下代码将产品对象中的数据添加到名为laarzen的MutableArray中.
- (void)loadURL {
// Load and parse an xml string
Products *product = [[Products alloc] init];
tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"url..."]];
// If TBXML found a root node, process element and iterate all children
// Obtain root element
TBXMLElement * root = tbxml.rootXMLElement;
// if root element is valid
if (root) {
// search for the first category element within the root element's children
TBXMLElement * Category = [TBXML childElementNamed:@"Category" parentElement:root];
Products *product = [[Products alloc] init]; …Run Code Online (Sandbox Code Playgroud) 为什么会这样,导致段错?
#include<stdio.h>
#include<stdlib.h>
struct node
{
double d;
int *array;
char c;
};
void allocator(struct node *ptr)
{
int *tmp;
tmp = (int*)realloc(ptr, 10);
if(!tmp)
{
ptr->array=tmp;
ptr->array[0] = 23;
}
}
int
main()
{
struct node *ptr = (struct node*)malloc(sizeof(struct node));
ptr->c = 'y';
allocator(ptr);
printf(" %c\n", ptr->c);
printf(" %d\n", ptr->array[0]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的印象好像分配器函数中的realloc()分配内存,该内存也映射到main中malloc()分配的内存.
但这怎么可能发生?内存管理器(我猜这里的lib(stdlib))是否跟踪进程中的空闲和分配空间?
假设我有一个指针*p,它指向堆上的10个内存块.现在,我用NULL(或'\ 0')手动清除它,而不是free()它,如下所示:
for (int i = 0; i < length; ++i{
p[i] = '\0';
}
Run Code Online (Sandbox Code Playgroud)
这被认为是当每个位被清零时释放的存储器块?或者,我必须使用free()来告诉操作系统内存块真正被释放并且可用(我猜)?
如果我的类有一个可以由类客户端设置的某种指针,我该如何处理删除?
例:
class A {
};
class B {
public:
void setA(A* a) {
this->a = a;
}
private:
A* a;
};
Run Code Online (Sandbox Code Playgroud)
该类的析构函数应该如何B?应该删除a吗?正如我所看到的,用户可以通过两种方式设置此指针:
... // Assume B object b being created elsewhere
A aObj;
A* aPtr = new A();
b.setA(&aObj); // It is not OK to use delete, and class member will
// point to invalid memory location once aObj goes out
// of scope
b.setA(aPtr); // Using new will make the pointer available even after …Run Code Online (Sandbox Code Playgroud) 建议我释放,autorelease,drain,nil和null之间的区别.当应用程序的dealloc方法被调用?
一个非常基本的问题,当我有类似的东西:
TTStyledText * text = [TTStyledText textFromXHTML:message.message lineBreaks:YES URLs:NO];
text.width = self.frame.size.width - 60;
text.font = [UIFont fontWithName:@"ArialMT" size:17.0];
_main_title.text = text;
Run Code Online (Sandbox Code Playgroud)
当我分配text时_main_title.text,是否意味着_main_title.text保留text?
好的,所以我知道在这个问题上有一堆问题,但在阅读它们并尝试使用这些方法之后,我的应用程序似乎仍然会泄漏内存.我研究上的内存Manegment苹果直营店和读取显着的问题在这里,这里和这里.我有一个方法解析一个JSON字符串,然后将它们返回到一个NSMutableDictionary.我autorelease从返回方法调用该对象,然后调用retain接收方法(确保该对象在池的下一个排水管上不是dealloc).然后,当我完成它时,我释放对象.但它仍然泄漏.谁能看到我做错了什么?
退货方法
+ (NSMutableArray *)parseSpecialtyResult:(NSString *)json
{
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dictionary = [parser objectWithString:json];
NSArray *jsonObjects = [dictionary valueForKey:@"Rows"];
NSMutableArray *jsonArray = [[NSMutableArray alloc] initWithCapacity:[jsonObjects count]];
//Storing objects
for (NSDictionary *dict in jsonObjects)
{
Specialty *specialty = [[Specialty alloc] init];
[specialty setName:[dict objectForKey:@"name"]];
[specialty setIdenity:[dict objectForKey:@"id"]];
[jsonArray addObject:specialty];
}
[parser release];
//Relinquish ownership of this object
return [jsonArray autorelease];
}
Run Code Online (Sandbox Code Playgroud)
打电话给班级
- …Run Code Online (Sandbox Code Playgroud) c++ ×4
ios ×4
objective-c ×4
iphone ×3
c ×2
cocoa ×2
memory ×2
memory-leaks ×2
arrays ×1
autorelease ×1
nsstring ×1
null ×1
pointers ×1
properties ×1
tbxml ×1