我试图计算NxN矩阵的行列式.这段代码在我尝试动态分配内存的行中给出了错误.
错误:类型为"int"的值无法分配给"float*"类型的实体
错误:类型为"int"的值无法分配给"float**"类型的实体
double CalcDeterminant( float **mat, int order)
{
float **minor;
unsigned short i;
float det = 0;
// order must be >= 0
// stop the recursion when matrix is a single element
if( order == 1 )
return mat[0][0];
// the determinant value
// allocate the cofactor matrix
**minor = malloc((order-1) * sizeof(float *));
for(i=0;i<order-1;i++)
minor[i] = malloc((order-1) * sizeof(float));**
//float *mat2d = malloc( rows * cols * sizeof( float ));
for(i = 0; i < order; …Run Code Online (Sandbox Code Playgroud) 我有一个自定义UIView,它通过UIViewController内的NIB加载.
我一直在努力- [UIScrollView retainCount]:消息整天发送到解除分配的实例错误.
我的自定义UIView子类dealloc方法如下所示:
-(void)dealloc {
[myScrollView dealloc];
[someProperty dealloc];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
问题是它总是在[super dealloc]上崩溃,因为前面有[myScrollView dealloc].
当我改变方法时:
-(void)dealloc {
[super dealloc];
[myScrollView dealloc];
[someProperty dealloc];
}
Run Code Online (Sandbox Code Playgroud)
一切都很好.我的问题是,如果首先或最后调用[super dealloc]会有所不同吗?在大多数例子中,我看到它被称为last.
我一直在做一个学校作业,它大量使用向量,集合,堆栈和队列.
是什么区别Foo和Bar,传球特别是当Foo与Bar功能之间?什么时候可以安全地在Bar上调用delete?我猜测除非该向量中的所有内容都被移动delete,Bar否则永远不会安全吗?如果我返回Foo,当函数退出时,不会删除它(及其内容)吗?
vector<Line *> Foo;
Run Code Online (Sandbox Code Playgroud)
和:
vector<Line *> * Bar = new vector<Line *>();
Run Code Online (Sandbox Code Playgroud)
同样,让我说我有一个功能:
vector<Line *> BSTIndex::query(string expression)
{
vector<Line *> result; //Holds the lines that match the expression
string query = expression;
queue<string> * output = expressionParser(query);
doSomeStuffWithOutputHere();
return result;
}
Run Code Online (Sandbox Code Playgroud)
我的表达式是:
queue<string> * BSTIndex::expressionParser(string query)
{
char * cQuery = new char[100];
strcpy_s(cQuery, 100,query.c_str());
//strcpy(cQuery, query.c_str());
queue<string> * output = new queue<string>(); //Operators …Run Code Online (Sandbox Code Playgroud) 嘿fellas,在通过调试器运行时我看到第二次设置变量时出现以下内容(时间戳和校验和是一个接一个地通过这个方法设置的,当没有DataFeedManager存在时它可以正常工作,但是当它再次返回时它在设置校验和时崩溃):

这是感兴趣的功能:
//sets specified attribute to the passed in value while ensuring that only one instance of the DataFeedManager exists
-(void)setItemInDFMWhilePreservingEntityUniquenessForItem:(attribute)attr withValue:(id)value {
SJLog(@"CoreDataSingleton.m setItemInDFMWhilePreservingEntityUniquenessForItem");
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"DataFeedManager" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSUInteger numEntities = [[self managedObjectContext] countForFetchRequest:fetchRequest error:&error];
if (numEntities == NSNotFound) { // ERROR
//...
} else if (numEntities == 0) {
DataFeedManager *dfm = (DataFeedManager *)[NSEntityDescription insertNewObjectForEntityForName:@"DataFeedManager"
inManagedObjectContext:[self managedObjectContext]];
if (attr == checksumAttr) { //BLOCK …Run Code Online (Sandbox Code Playgroud) 伙计我正在生成一个字符串,它可以显示文件的路径,连接宏和字符串.功能是这样的:
char *userPath(char *username)
{
char *path = (char*)malloc(sizeof(char) * (strlen(MAILBOXES) + strlen(username) + 1));
path[0] = '\0';
strcat(path, MAILBOXES);
strcat(path, "/");
strcat(path, username);
return path;
}
Run Code Online (Sandbox Code Playgroud)
返回的指针引用一个正确的字符串,但在调用此函数后,该进程抛出了一个非常非常糟糕的*glibc检测到./mmboxd:malloc():内存损坏:0x085310a8**带有相对回溯.我知道这就是问题,因为我一旦实现它就开始出现这个错误,而且因为我使用的唯一的malloc就在这里.这段代码有什么问题?
当从C#创建COM对象或任何其他非托管实例时,如何在系统中分配非托管内存?
我正在做一个教程,作者给出了一个例子,但我很好奇为什么他没有在函数结束时释放childController.有什么想法吗?
-(void)tableView:(UITableView *) tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
if (childController==nil) {
childController = [[DisclosureDetailController alloc] initWithNibName:@"DisclosureDetailController" bundle:nil];
}
childController.title=@"Disclosure Button Pressed"; //why this line?????
NSUInteger row = [indexPath row];
NSString *selectedMovie = [list objectAtIndex:row];
NSString *detailMessage = [[NSString alloc]initWithFormat:@"you pressed disclosure button for %@",selectedMovie];
childController.message = detailMessage;
childController.title = selectedMovie;
[detailMessage release];
[self.navigationController pushViewController:childController animated:YES];
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
Cocoa内存管理NSArray与对象
我有一个NSMutableArray填充了一些对象.
例如:
...
id test = [NSObject new];
NSMutableArray *myArray = [NSMutableArray new];
[myArray addObject: test];
...
Run Code Online (Sandbox Code Playgroud)
当多个对象需要对该数组的引用时,我不能只使用方法" - removeAllObjects".所以我只在我自己的类的" - dealloc"方法中使用数组的发布.
- (void)dealloc {
[myArray release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
那么我的对象名称"test"泄露了,因此我需要做更多的事情吗?
我在文档中找不到答案,对于NSMutableArray的"dealloc"或"release"方法没有意义.在NSObject参考中,他们没有描述NSMutableArray.可能是"内存管理编程指南"中的任何地方(希望如此).
我希望创建UIView的多个实例,所以我想而不是创建新变量我会分配一个UIView,然后再次重新分配它以创建另一个UIView.这个可以吗?我是否正确地发布了视图,或者在2次分配后,tempview的保留计数是2还是释放只会使保留计数为1?
NSMutableArray *array = [[NSMutableArray alloc] init];
UIView *tempview = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView];
tempview = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView];
[tempview release];
[array release];
Run Code Online (Sandbox Code Playgroud) 它是否正确?
- (void)dealloc {
[super dealloc];
[stageObjects release];
}
Run Code Online (Sandbox Code Playgroud)
或者我应该打电话
[super dealloc]
Run Code Online (Sandbox Code Playgroud)
总是在所有版本之后,我的意思是这个功能的最后一行?
iphone ×4
objective-c ×4
ios4 ×3
c ×2
cocoa-touch ×2
ios ×2
.net ×1
c# ×1
c++ ×1
containers ×1
core-data ×1
dealloc ×1
new-operator ×1
strcat ×1
string ×1