引发异常:访问冲突

时间:2020-02-23 14:02:11

标签: c++ arrays algorithm pointers reverse

此函数反转指针数组并将其返回到主数组。主要问题是代码返回了 引发异常:读取访问冲突。 fptr为0xCCCCCCCC。

Minimal Description

错误的根源是什么?

int* mirror(int* p[], int n) {
    int* ptr,* fptr;
    int swap;
    ptr = p[0];
    fptr = p[n-1];
    while (fptr > ptr) {
        swap = *ptr;
        *ptr = *fptr;
        *fptr = swap;
        ptr++;
        fptr--;
    }
    return *p;
}

4 个答案:

答案 0 :(得分:1)

这是问题所在

while (fptr > ptr) { ... }

ptr是第一个元素(第一个指针),fptr是最后一个元素(最后一个指针),当第一个元素小于最后一个元素时,您正在遍历数组元素,但这暗示着数组中的元素是按地址顺序插入的,我认为不是。

相反,您应该使用尺寸(n)来做到这一点:

int** mirror(int* p[], int n) { // return a pointer to integers pointer, not a pointer to integer
    for(int i = 0; i < n/2 ; i++){ // go through the first half of the array
       int* tmp = p[i];  // and those three lines swap the current and the n - current elements
       p[i] = p[n-i-1];
       p[n] = tmp;
    }
    return p;
}

答案 1 :(得分:0)

对于初学者来说,该函数的返回类型int *没有意义。

int* mirror(int* p[], int n) {

最好将函数声明为具有返回类型void

如果您有int *类型的指针数组,则指向其元素的指针的类型为int **

此外,用户可以将0作为第二个参数传递。您应该检查这种情况。

出现异常的原因可能是数组包含指向原始数组之外的无效指针。

如果指针数组的形成方式不必使例如第一个指针指向原始数组的第一个元素,那么您的功能将无法工作。

可以像这样定义函数

void mirror( int * p[], size_t n ) 
{
    if ( n != 0 )
    {
        for ( int **first = p, **last = p + n; first < --last; ++first )
        {
            int tmp = **first;
            **first = **last;
            **last = tmp;
        }
    }
}

这是一个演示程序

#include <iostream>

void mirror( int * p[], size_t n ) 
{
    if ( n != 0 )
    {
        for ( int **first = p, **last = p + n; first < --last; ++first )
        {
            int tmp = **first;
            **first = **last;
            **last = tmp;
        }
    }
}

int main() 
{
    const size_t N = 5;
    int a[N] = { 10, 30, 30, 40, 50 };
    int * b[N] = { a, a + 1, a + 2, a + 3, a + 4 };

    for ( const auto &item : a )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';

    mirror( b, N );

    for ( const auto &item : a )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';

    return 0;
}

程序输出为

10 30 30 40 50 
50 40 30 30 10

请注意,您可以使用标准函数std::swap来代替“手动”交换元素。

答案 2 :(得分:0)

问题是您使用了错误的类型。正确的版本是:

log/production.log

进行这些更改后,您的函数将按预期工作,尽管它返回的内容在大多数情况下是无法使用的。返回import React from "react"; import axios from "axios"; export default class TeamList extends React.Component { state = { teams: [], }; componentDidMount() { axios.get('http://localhost:8080/teams') .then(res => { const teams= res.data; this.setState({teams}); }); } render() { return ( <ul> {this.state.teams.map(team => ( <li key = {team.id}>Name: {team.name} Seeding: {team.seeding}</li> ))} </ul> ); } } import axios from 'axios'; export default class TeamInput extends React.Component { noTeams=1; state = { name: '', id: 1, seeding: '10' }; handleName = event => { this.setState({ name: event.target.value }); // this.setState({id: this.noTeams}); }; handleSeeding = event =>{ this.setState({seeding: event.target.value}); }; handleSubmit = event => { event.preventDefault(); this.noTeams=this.noTeams+1; this.setState({id: this.noTeams}); const team = { name: this.state.name, seeding: this.state.seeding, id: this.state.id, }; axios.post(`http://localhost:8080/teams`, team ) .then(res => { console.log(res); console.log(res.data); }); }; render() { return ( <div> <form onSubmit={this.handleSubmit}> <label> Team Name: <input type="text" name="name" onChange={this.handleName} /> </label> <label> Seeding: <input type = "text" name= "seeding" onChange={this.handleSeeding} /> </label> <button type="submit">Add</button> </form> </div> ) } } )或设为 int **ptr, **fptr; int* swap; ptr = &p[0]; fptr = &p[n - 1];

p

请注意,已经有一个用于交换两个值的标准实用程序,称为appendChild()

int**

最后一点,还有一个标准实用程序,用于反转名为std::swap的容器中的值,该容器执行您的void函数的作用。

代替

void mirror(int* p[], int n) {
    if(n > 0) {                  // check this or you risk undefined behavior
        int** ptr = &p[0];
        int** fptr = &p[n-1];
        int* swap;
        while(ptr < fptr) {
            swap = *ptr;
            *ptr = *fptr;
            *fptr = swap;
            ptr++;
            fptr--;
        }
    }
}

void mirror(int* p[], int n) {
    if(n > 0) {
        for(int **ptr = &p[0], **fptr = &p[n - 1]; ptr < fptr; ++ptr, --fptr) {
            std::swap(*ptr, *fptr);
        }
    }
}

答案 3 :(得分:-1)

尝试按以下步骤导航阵列:

void mirror(int* p, int n){
  int* ptr,* fptr;
  int swap;
  ptr = p;
  fptr = p+n-1;
  while (fptr != ptr) {
    swap = *ptr;
    *ptr = *fptr;
    *fptr = swap;
    ptr++;
    fptr--;
  }
}