C - 获取SIGABRT错误,对我没有任何意义

时间:2017-12-10 15:13:21

标签: c arrays realloc sigabrt

所以,我的程序应该以{1,2,3,4,5}格式读取一个未知大小的正整数数组,将其处理成一个数组,然后开始读取整数a和b对并查找该数组的第(b-1)个元素的最小公倍数。 这是我目前的解决方案:

#include <stdio.h>
#include <stdlib.h>

int gcf(int a, int b) // greatest common divisor
{
    while (a*b!=0)
    {
        if (a<b)
            b=b-a;
        else
            if (b<a)
                a=a-b;
        else
            if (a==b)
                return a;
    }
    return 0;
}

int lcm(int a, int b) // least common multiplier
{
    return a*b/gcf(a,b);
}

int main()
{
    int a,bufUsed=0,bufCurr=0,i,b,signal=1,curlcm;
    char c;
    int* tmp;
    int* array;
    c=getchar();
    if (c!='{')
    {
        printf ("err0");
        return 0;
    }
    while((scanf("%d",&a))==1)
    {
        if (signal==0) // checking for the comma
        {
            printf("err1");
            return 0;
        }
        signal=0;
        printf("%d ",a); // displaying current values, used just for debugging
        if (bufUsed == bufCurr) //resizing the current array 
        {
           bufCurr += 20;
           tmp = (int*)realloc(array, bufCurr); // the line that causes trouble
           if (!tmp)
               printf("err2");
           array = tmp;
        }
        array[bufUsed] = a;
        bufUsed++;
        if (scanf(" %c",&c)==1) // checking for commas or closing bracket
        {
            if (c==',')
            {
                signal=1;
                continue;
            }
            if (c=='}')
                break;
            else
            {
                printf("err3");
                return 0;
            }
        }
        else
        {
            printf("err4");
            return 0;
        }
    }
    while ((scanf("%d %d",&a,&b))==2) // the second part, finding the LCM
    {
        curlcm=lcm(array[a],array[a+1]);
        for (i=2;i<b-a;i++)
        {
            curlcm=lcm(curlcm,array[a+i]);
        }
        printf("%d\n",curlcm);
    }
    return 0;
}

该行

  

tmp =(int *)realloc(array,bufCurr);

根据gdb,

似乎导致SIGABRT,但是,如果我删除程序的第二部分(在循环中找到LCM),它就可以正常工作。我已经尝试检查第二部分如何在第二个程序中使用已定义的数组:

int main()
{
    int a,i,b,curlcm;
    int array[10];
    for (i=0;i<10;i++)
        array[i]=i+1;      
    scanf("%d %d",&a,&b);
    curlcm=lcm(array[a],array[a+1]);
    for (i=2;i<b-a;i++)
    {
        curlcm=lcm(curlcm,array[a+i]);
    }
    printf("%d",curlcm);
}

它运行得也很好。

那么如何将这两者结合起来导致SIGABRT呢?我已经尝试检查访问阵列是否会导致问题,但是,情况似乎并非如此。提前感谢任何建议。

1 个答案:

答案 0 :(得分:3)

根据手册,功能

import { Component } from '@angular/core';
import { Employee } from './employee';
//import { NgForm } from '@angular/forms';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { promise } from 'selenium-webdriver';


@Component({
  selector: 'employee-data',
  templateUrl: './employee-data.component.html',
  styles: [``]
})

export class EmployeeDataComponent {

  userForm: FormGroup;

  constructor(private formBuilder: FormBuilder) {

    this.userForm = this.formBuilder.group({
        'ModalId': ['', Validators.required],
        'ModalName': ['', Validators.required],
        'ModalAddress': ['', Validators.required],
        'ModalCompany': ['', Validators.required]

    })

  }

  id: number;
  name: string;
  address: string;
  gender: string;
  company: string;
  isEditable: boolean;

  //emp: Employee[] = [];

  employees: Employee[] = [
    {
        id: 1,
        name: 'Ram',
        address: 'Kupondole',
        gender: 'Male',
        company: 'XYZ Techno Sales',
        isEditable: false
    },
    {
        id: 2,
        name: 'Shyam',
        address: 'Baneshwor',
        gender: 'Male',
        company: 'ABC Enterprises',
        isEditable: false
    },
    {
        id: 3,
        name: 'Prashansha',
        address: 'Tripureshwor',
        gender: 'Female',
        company: 'MNO Inc',
        isEditable: false
    },
    {
        id: 4,
        name: 'Rita',
        address: 'Ghatthaghar',
        gender: 'Female',
        company: 'Subisu',
        isEditable: false
    }
  ];

  addRow() {
    //prompt("Checking the control!");
    this.employees.push({
        id: this.id,
        name: this.name,
        address: this.address,
        gender: this.gender,
        company: this.company,
        isEditable: this.isEditable

    });
  }

  deleteEmployee(index) {

    this.employees.splice(index, 1);
  }

  editEmployee(index) {

   debugger;
   this.employees[index].isEditable = true;

  }
}
  

realloc()函数将ptr指向的内存块的大小更改为size字节。内容将在从区域开始到新旧尺寸的最小范围内保持不变。如果新大小大于旧大小,则不会初始化添加的内存。 如果ptr为NULL ,则该调用等同于malloc(size)

在该计划中,我们有

void *realloc(void *ptr, size_t size);

第一次调用int* array; ... tmp = (int*)realloc(array, bufCurr); ,而realloc指针未初始化。

内存管理器尝试根据该随机地址(如段的大小与请求的大小)读取一些内部数据,这会导致未定义的行为(在您的情况下崩溃)。

根据建议,一个简单的解决方案包括将array设置为array,以便NULL执行简单realloc(因为其malloc地址为ptr)。

NULL