EXC_BAD_ACCESS代码= 13

时间:2014-02-13 02:32:17

标签: objective-c

我正在尝试创建一个用于动态存储int数组的Objective-C类(只是为了它的乐趣),但是我收到的错误是“错误代码= 13'从运行这个类。下面是我的类接口和实现。

接口部分

#import <Foundation/Foundation.h>

@interface AsIntArray : NSObject

- ( id ) initWithSize: ( unsigned int ) initialSize;

- ( id ) initWithSize:( unsigned int ) initialSize andDefaultValue: ( int ) defaultVal;

- ( unsigned int ) size;
- ( int ) itemAtIndex: ( unsigned int ) index;
- ( void ) setItemAtIndex: ( unsigned int ) index andValue: ( int ) newValue;

- ( void ) clear;
- ( void ) addItem: ( int ) itemToAdd;
- ( void ) removeItemAtIndex: ( unsigned int ) index;

- ( NSString * ) toString;

@end

实施部分

#import "AsIntArray.h"

@implementation AsIntArray  {
    unsigned int _uint_size;
    int *_int_array;
}

- ( id ) init   {
    self = [ super init ];
    if( self )  {
        _uint_size = 0;
        _int_array = nil;
    }
    return self;
}

- ( id ) initWithSize:(unsigned int)initialSize {
    self = [ super init ];
    if( self )  {
        _uint_size = ( initialSize > 0 ? initialSize : 0 );
        if( _uint_size > 0 )    {
            _int_array = ( int * ) malloc( _uint_size );
        } else {
            _int_array = nil;
        }
    }
    return self;
}

- ( id ) initWithSize:(unsigned int)initialSize andDefaultValue:(int)defaultVal {
    self = [ super init ];
    if( self )  {
        _uint_size = ( initialSize > 0 ? initialSize : 0 );
        if( _uint_size > 0 )    {
            _int_array = ( int * ) malloc( _uint_size );
            for( int i = 0; i < _uint_size; i++ )
                _int_array[ i ] = defaultVal;
        } else {
            _int_array = nil;
        }
    }
    return self;
}

- ( unsigned int ) size { return _uint_size; }

- ( int ) itemAtIndex:(unsigned int)index   {
    return ( index < _uint_size ? _int_array[ index ] : 0 );
}

- ( void ) setItemAtIndex:(unsigned int)index andValue:(int)newValue    {
    if( index < _uint_size )    {
        _int_array[ index ] = newValue;
    }
}

- ( void ) clear    {
    if( _uint_size > 0 )    {
        _uint_size = 0;
        free( _int_array );
        _int_array = nil;
    }
}

- ( void ) addItem:(int)itemToAdd   {
    if( _uint_size == 0 )   {
        _uint_size++;
        _int_array = ( int * ) malloc( _uint_size );
        _int_array[ 0 ] = itemToAdd;
    } else  {
        int *temp = ( int * ) malloc( _uint_size );
        for( unsigned int i = 0; i < _uint_size; i++ )
            temp[ i ] = _int_array[ i ];

        free( _int_array );
        _int_array = nil;
        _uint_size++;
        _int_array = ( int * ) malloc( _uint_size );

        for( unsigned int i = 0; i < _uint_size - 1; i++ )
            _int_array[ i ] = temp[ i ];
        _int_array[ _uint_size - 1 ] = itemToAdd;

        free( temp );
        temp = nil;
    }
}

- ( void ) removeItemAtIndex:(unsigned int)index    {
    if( index < _uint_size )    {
        if( _uint_size <= 1 )   {
            [ self clear ];
        } else  {
            int *temp = ( int * ) malloc( _uint_size );
            for( unsigned int i = 0; i < _uint_size; i++ )
                temp[ i ] = _int_array[ i ];

            free( _int_array );
            _int_array = nil;
            _uint_size--;
            _int_array = ( int * ) malloc( _uint_size );

            unsigned int counter = 0;
            for( unsigned int i = 0; i <= _uint_size; i++ ) {
                if( i != index )    {
                    _int_array[ counter ] = temp[ i ];
                    counter++;
                }
            }

            free( temp );
            temp = nil;
        }
    }
}

//  Throws error code=13
/*- ( NSString * ) description    {
    NSMutableString *desc = [ [ NSMutableString alloc ] initWithString:@"{ " ];

    for( unsigned int i = 0; i < _uint_size; i++ )  {
        if( i > 0 ) [ desc appendString:@", " ];
        [ desc appendFormat:@"%i", _int_array[ i ] ];
    }
    [ desc appendString:@" }" ];

    return [ desc description ];
}*/

- ( NSString * ) toString   {
    NSMutableString *desc = [ [ NSMutableString alloc ] initWithString:@"{ " ];

    for( unsigned int i = 0; i < _uint_size; i++ )  {
        if( i > 0 ) [ desc appendString:@", " ];
        [ desc appendFormat:@"%i", _int_array[ i ] ];
    }
    [ desc appendString:@" }" ];

    return [ desc description ];
}

@end

每次发生错误时,它总是出现在 - (NSString *)描述方法中。输出窗口显示:&#34; objc [947]: * 哈希表:重新计算后计数不同;可能表示一个破坏的不变量:有x和y,如isEqual(x,y)为TRUE但是hash(x)!= hash(y)&#34;

1 个答案:

答案 0 :(得分:0)

您传递给malloc()的大小是错误的。 malloc的参数是 bytes 中的大小,但是你的整数大于一个字节,因此你的数组只是它需要的大小的一小部分(可能只有25%!),其余的通过数组指针写入的内存是你正在涂鸦的其他人的空间。相反,您想要malloc(sizeof(int) * _uint_size)