C++ Xcode:memcpy EXC\u访问错误?

C++ Xcode:memcpy EXC\u访问错误?,c++,ios,c,xcode,memcpy,C++,Ios,C,Xcode,Memcpy,我在构建和运行时遇到以下崩溃: 以下是我在调试器中看到的消息(注意:我确实启用了Guard Malloc&Log Malloc堆栈) 以下是我的整个方法: - (MatrixObject *)matrixByRemovingColumnAtIndex:(int)index { // In this example this should be 2 NSLog(@"int debug: %d", index); //Store new column size

我在构建和运行时遇到以下崩溃:

以下是我在调试器中看到的消息(注意:我确实启用了Guard Malloc&Log Malloc堆栈)

以下是我的整个方法:

- (MatrixObject *)matrixByRemovingColumnAtIndex:(int)index {

    // In this example this should be 2
    NSLog(@"int debug: %d", index);

    //Store new column size
    int newColumnCount = (columnCount - 1);

    //Make sure our index is not out of bounds
    if (index > newColumnCount) {

        @throw [NSException exceptionWithName:@"Index Out Of Bounds Exception"
                                       reason:[NSString stringWithFormat:@"Oops! %@'s index input is out of bounds. Maximum bounds allowed is: %d.", [self class], columnCount - 1]
                                     userInfo:nil];
    }

    //Create new double array
    double *newMatrix = malloc(rowCount * newColumnCount * sizeof(double));

    /*
     We need to copy in 3 steps.
     Step 1: Copy first row elements before our column at index.
     Step 2: Copy every row in every column except for our column at index rows
     Step 3: Copy last row elements after our column at index.
     */

    // Do Step 1
    memcpy(newMatrix, self->matrix, index * sizeof(double));

    //Total number of elements
    int allElements = (rowCount * columnCount);

    //Simplify our index
    int i = (index + 1);

    //Do Step 2 while looping through all our rows in our columns
    do {

        //Copy every element
        memcpy(newMatrix + index, self->matrix + i, newColumnCount * sizeof(double));

        //Increment our index by our newColumnCount
        index += newColumnCount;

        //Increment our i by columnCount
        i += columnCount;
    }

    //Loop through all our rows in our columns until we reach the end
    while ((i < allElements) && ((i + columnCount) < allElements));


    // Do Step 3 (Don't forget our rows after our column at index bc we want those)
    if (i < allElements) {

        //Copy our last elements (THIS IS WHERE THE EXCEPTION OCCURS)
        memcpy(newMatrix + index, self->matrix + i, (allElements - i) * sizeof(double));
    }

    return [[self class] matrixFromDoubleArray:newMatrix withRows:rowCount andColumns:newColumnCount];
}
-(MatrixObject*)MatrixByRemovingColumnIndex:(int)索引{
//在本例中,这应该是2
NSLog(@“int debug:%d”,索引);
//存储新的列大小
int newColumnCount=(columnCount-1);
//确保我们的索引没有超出范围
如果(索引>新列计数){
@抛出名为@“索引越界异常”的[NSException Exception Exception”
原因:[NSString stringWithFormat:@“Oops!%@”的索引输入超出范围。允许的最大范围为:%d.,[self class],columnCount-1]
用户信息:无];
}
//创建新的双数组
double*newMatrix=malloc(rowCount*newColumnCount*sizeof(double));
/*
我们需要分三步进行复制。
步骤1:将第一行元素复制到索引处的列之前。
步骤2:复制每一列中的每一行,除了我们在索引行中的列
步骤3:在索引处复制列之后的最后一行元素。
*/
//执行步骤1
memcpy(newMatrix,self->matrix,index*sizeof(double));
//元素总数
int等位基因=(行计数*列计数);
//简化我们的索引
int i=(指数+1);
//执行步骤2,同时循环遍历列中的所有行
做{
//复制每个元素
memcpy(newMatrix+index,self->matrix+i,newColumnCount*sizeof(double));
//通过newColumnCount增加索引
索引+=newColumnCount;
//通过列计数增加我们的i
i+=列数;
}
//循环遍历列中的所有行,直到到达末尾
而((i<等位基因)&&((i+列数<等位基因));
//执行步骤3(不要忘记索引bc中列后面的行,我们需要这些行)
if(i<等位基因){
//复制我们最后的元素(这是异常发生的地方)
memcpy(newMatrix+index,self->matrix+i,(等位基因-i)*sizeof(double));
}
返回[[self class]矩阵fromDoubleArray:newMatrix with Rows:rowCount and Columns:newColumnCount];
}

有没有人在memcpy上遇到过这种类型的崩溃。注意:这个方法是从<代码> MatrxObjult< /Cuff>子类调用的,但是我怀疑它会影响这个。

目标C不是C++,也不是C…true,但是MimcPy和Maloc是,问题的根源。你的代码> MeMCPY(DouLyPPTR+INT/CODE)看起来很可疑。您应该通过调试器(如lldb)运行程序,并确保所有计数器和索引都具有正确的值。EXC_BAD_ACCESS表示您正在写入或读取内存中尚未被访问的区域allocated@JonathanPotterObjective-C是C,它是一个严格的超集。所有C代码都是有效的Objective-C。所有C构造都是有效的Objective-C。Objective-C是添加到C中的薄层。
- (MatrixObject *)matrixByRemovingColumnAtIndex:(int)index {

    // In this example this should be 2
    NSLog(@"int debug: %d", index);

    //Store new column size
    int newColumnCount = (columnCount - 1);

    //Make sure our index is not out of bounds
    if (index > newColumnCount) {

        @throw [NSException exceptionWithName:@"Index Out Of Bounds Exception"
                                       reason:[NSString stringWithFormat:@"Oops! %@'s index input is out of bounds. Maximum bounds allowed is: %d.", [self class], columnCount - 1]
                                     userInfo:nil];
    }

    //Create new double array
    double *newMatrix = malloc(rowCount * newColumnCount * sizeof(double));

    /*
     We need to copy in 3 steps.
     Step 1: Copy first row elements before our column at index.
     Step 2: Copy every row in every column except for our column at index rows
     Step 3: Copy last row elements after our column at index.
     */

    // Do Step 1
    memcpy(newMatrix, self->matrix, index * sizeof(double));

    //Total number of elements
    int allElements = (rowCount * columnCount);

    //Simplify our index
    int i = (index + 1);

    //Do Step 2 while looping through all our rows in our columns
    do {

        //Copy every element
        memcpy(newMatrix + index, self->matrix + i, newColumnCount * sizeof(double));

        //Increment our index by our newColumnCount
        index += newColumnCount;

        //Increment our i by columnCount
        i += columnCount;
    }

    //Loop through all our rows in our columns until we reach the end
    while ((i < allElements) && ((i + columnCount) < allElements));


    // Do Step 3 (Don't forget our rows after our column at index bc we want those)
    if (i < allElements) {

        //Copy our last elements (THIS IS WHERE THE EXCEPTION OCCURS)
        memcpy(newMatrix + index, self->matrix + i, (allElements - i) * sizeof(double));
    }

    return [[self class] matrixFromDoubleArray:newMatrix withRows:rowCount andColumns:newColumnCount];
}