与内存管理概念的误解有关的错误?

发布于 2024-12-20 21:24:39 字数 2002 浏览 0 评论 0原文

我正在尝试创建自己的类来管理矩阵,但我是 Objective-C 的新手。

矩阵类 我正在创建此类来管理矩阵,并将其保存在 NSMutableArrayNSMutableArray 中。

@interface matrix : NSObject 
{
    NSMutableArray *grid;

    // Dimensiones de la matriz.
    NSUInteger num_columns;
    NSUInteger num_rows;

}

我创建了一个方法来添加行、列和其他操作。 addRow 方法是这样的:

-(BOOL) addRow:(NSArray *) fila
{
    NSLog(@"Dentro de Añadir una Fila de una matriz.");

    [grid addObject:fila];

    [self checkDimensions];

    return YES;
}

我使用 extern 类的方法。该方法很简单,因为我只想创建一个矩阵并打印它。

-(BOOL) matrixTEST
{
    NSLog(@"Dentro de Testeo de una matriz.");

    // create a new instance
    matrix *m = [[matrix alloc] init];

    NSMutableArray *vector = [[NSMutableArray alloc] init];
    [vector addObject:@"1"];
    [vector addObject:@"2"];
    [vector addObject:@"3"];
    [m addRow:vector];    
    [vector removeAllObjects];
    [vector addObject:@"4"];
    [vector addObject:@"5"];
    [vector addObject:@"6"];
    [m addRow:vector];
    [vector removeAllObjects];
    [vector addObject:@"7"];
    [vector addObject:@"8"];
    [vector addObject:@"3"];
    [m addRow:vector];
    [vector removeAllObjects];;

    // print matriz
    [m imprimirMatriz];

    [vector release];
    return YES;
}

问题是我不知道我做错了什么,因为方法 printMatrix 显示错误访问或零错误。此外,定义为包含行数和列数的变量显示奇怪的值: - Rows : 5.75753e+228 - Columns : 8.55507e-53

打印矩阵的方法。

-(void) imprimirMatriz
{
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    // NSNumber *numero = [[NSNumber alloc] init];

    NSLog(@"Dentro de imprimir matriz.");

    [self printDimensions];

    for(NSUInteger c = 0; c < num_rows; c++)
    {
        temp = [grid objectAtIndex:c];

        for(NSUInteger d = 0; d < num_columns; d++)
        {
            NSLog(@" %3g ", [temp objectAtIndex:d]);
        }
        NSLog(@"\n");
    }

    [temp release];
}

I'm trying to create my own class to manage matrixes, but I'm a newbie with Objective-C.

Matrix class
I'm creating this class to manage matrices saving it in a NSMutableArray of NSMutableArray's.

@interface matrix : NSObject 
{
    NSMutableArray *grid;

    // Dimensiones de la matriz.
    NSUInteger num_columns;
    NSUInteger num_rows;

}

I created a method to add rows, columns and other operations. The method addRow is like this:

-(BOOL) addRow:(NSArray *) fila
{
    NSLog(@"Dentro de Añadir una Fila de una matriz.");

    [grid addObject:fila];

    [self checkDimensions];

    return YES;
}

I use a method of an extern class. The method is so easy because I only want to create a matrix and print it.

-(BOOL) matrixTEST
{
    NSLog(@"Dentro de Testeo de una matriz.");

    // create a new instance
    matrix *m = [[matrix alloc] init];

    NSMutableArray *vector = [[NSMutableArray alloc] init];
    [vector addObject:@"1"];
    [vector addObject:@"2"];
    [vector addObject:@"3"];
    [m addRow:vector];    
    [vector removeAllObjects];
    [vector addObject:@"4"];
    [vector addObject:@"5"];
    [vector addObject:@"6"];
    [m addRow:vector];
    [vector removeAllObjects];
    [vector addObject:@"7"];
    [vector addObject:@"8"];
    [vector addObject:@"3"];
    [m addRow:vector];
    [vector removeAllObjects];;

    // print matriz
    [m imprimirMatriz];

    [vector release];
    return YES;
}

The problem is I don't know what I am doing wrong because method printMatrix shows an error of bad access or zeros. Also, the vars defined to contain number of rows and columns show weird values: - Rows : 5.75753e+228 - Columns : 8.55507e-53

Method to print Matrix.

-(void) imprimirMatriz
{
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    // NSNumber *numero = [[NSNumber alloc] init];

    NSLog(@"Dentro de imprimir matriz.");

    [self printDimensions];

    for(NSUInteger c = 0; c < num_rows; c++)
    {
        temp = [grid objectAtIndex:c];

        for(NSUInteger d = 0; d < num_columns; d++)
        {
            NSLog(@" %3g ", [temp objectAtIndex:d]);
        }
        NSLog(@"\n");
    }

    [temp release];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千鲤 2024-12-27 21:24:39

在函数-(BOOL)matrixTest中插入字符串。这完全没问题。但是当您打印网格数组中的值时,您指定“%3g”。据我所知,“%g”打印的是浮点型或双精度型,当您的值是字符串时将不起作用。如果你用%@替换%g,它应该可以工作。

关于包含数组宽度和高度的变量:您没有提供如何设置这些变量的实现,因此不可能知道问题是什么。

附带说明一下,您还有一些其他错误。在 -(void) imprimirMatriz 中,您有行 NSMutableArray *temp = [[NSMutableArray alloc] init];,但稍后您会分配 temp到别的东西。这会导致内存泄漏。当您将变量分配给 NSArray 中的某些内容时,Alloc/init 不是必需的。

作为对您评论的回答:您只能将对象插入 NSMutableArray (或 NSArray)。当您编写[myArray addObject:1]时,您尝试插入一个原始整数。基元(int、bool、char、float、double 等)不是对象,因此不能插入 NSArray 或 NSMutableArray 中。相反,您可以通过将 int 包装在 NSNumber 实例中来插入它。以下行将起作用:[myArray addObject:[NSNumber numberWithInt:1]];。 NSNumber 对象中还可以包含浮点数和其他类型。请参阅 NSNumber 类参考 以获得完整参考。

In your function -(BOOL) matrixTest you insert strings. This is perfectly fine. But when you print the values in your grid array, you specify "%3g". "%g" prints, as far as I remember, a float or a double, and will not work when your values are strings. If you substitute %g with %@, it should work.

Regarding the variables containing width and height of array: you have not provided your implementation of how you set these variables, so it is impossible to know what the problem is.

As a side note, you have some other errors as well. in -(void) imprimirMatriz you have the line NSMutableArray *temp = [[NSMutableArray alloc] init];, but you later on assign temp to something else. This causes a memory leak. Alloc/init is not neccessary when you assign a variable to something from an NSArray.

As an answer to your comment: you can only insert objects into an NSMutableArray (or NSArray for that sake). When you write [myArray addObject:1], you try to insert a primitive integer. Primitives (int, bool, char, float, double and so on) are not objects, and therefore cannot be inserted into an NSArray or NSMutableArray. Instead, you can insert an int by wrapping it in an instance of NSNumber. The following line will work: [myArray addObject:[NSNumber numberWithInt:1]];. You can also have floats and other types in an NSNumber object. See NSNumber Class Reference for a full reference.

鱼窥荷 2024-12-27 21:24:39

您应该为测试函数中的每一行使用一个新数组 - 否则您将三次添加指向同一对象的指针,删除所有对象并重新填充。我希望在上述代码结束时,您的矩阵中将拥有同一空数组的三个副本。

因此,

[m addRow:vector];         
[vector removeAllObjects]; // This removes the objects from the array in your matrix as well!

最后

[m addRow:vector];
[vector release];
vector = [NSMutableArray alloc] init];

一个 removeAllObjects 会清空数组(从而清空矩阵中的每一行),但不会更新内部维度变量,因此可能会导致稍后出现错误。

此外,正如 Bendik 所说,您的日志语句的字符串格式错误。应该是%@

You should use a new array for each row in your test function - otherwise you are adding a pointer to the same object three times, removing all of the objects and re-filling. I expect by the end of the above code you have three copies of the same empty array in your matrix.

So, instead of

[m addRow:vector];         
[vector removeAllObjects]; // This removes the objects from the array in your matrix as well!

Do

[m addRow:vector];
[vector release];
vector = [NSMutableArray alloc] init];

The last removeAllObjects will empty the array (and so empty every row in your matrix) but not update your internal dimension variables, so is likely to lead to errors later on.

In addition, as Bendik says, your log statement is in the wrong format for strings. It should be %@

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文