将指针重用于for循环中的结构

时间:2015-03-24 13:23:59

标签: c++

我对C ++很陌生,实际上对编程很新。我正在编写代码来解决2D Navier-Stokes方程。我的部分代码涉及使用结构将值分配给PETSc对象。临时计算需要该结构,只需将值分配给PETSc对象即可。 为了做到这一点,我使用了' bodyVel'指针,我分配内存,执行计算并解除分配。该程序在' j = 0'中运行良好,当j递增时,程序崩溃。

void function();

int main()
{   
function();

return 0;
}

void function()
{

     int b[5] = { 1, 2, 3, 4, 5};  //values to be assigned to the PETSc object
     int c[5] = { 6, 7, 8, 9, 10};

     int g[5] = {10, 11, 12, 13, 14}; //(globalIndexMapping) indices where the values need to be assigned, index of the PETSc object starts from 10 (say)



    for(int j=0; j<3; j++)  //This is the outer time loop
    {
        typedef struct{
        int ub, vb;
        }bodyVelocity;  

        bodyVelocity *bodyVel;

        std::cout<<"This is iteration \t"<<j<<"\n";
        bodyVel = new bodyVelocity[5];
        for(int i=0; i<5; i++)
        {   
           int k = g[i];
           bodyVel[k].ub = b[i];
           bodyVel[k].vb = c[i];
           std::cout<<i<<"\t"<<bodyVel[k].ub<<"\t"<<bodyVel[k].vb<<"\t"<<"\n";
        }
    //values to the PETSc object is assigned here

        delete[] bodyVel;
    }

}

这是valgrind的输出

==12248== Memcheck, a memory error detector
==12248== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==12248== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==12248== Command: ./a.out
==12248== 
This is iteration   0
==12248== Invalid write of size 4
==12248==    at 0x4008AA: function() (in /home/shreenidhi/Desktop/a.out)
==12248==    by 0x4007BC: main (in /home/shreenidhi/Desktop/a.out)
==12248==  Address 0x5a03090 is not stack'd, malloc'd or (recently) free'd
==12248== 
==12248== Invalid write of size 4
==12248==    at 0x4008C3: function() (in /home/shreenidhi/Desktop/a.out)
==12248==    by 0x4007BC: main (in /home/shreenidhi/Desktop/a.out)
==12248==  Address 0x5a03094 is not stack'd, malloc'd or (recently) free'd
==12248== 
==12248== Invalid read of size 4
==12248==    at 0x4008D3: function() (in /home/shreenidhi/Desktop/a.out)
==12248==    by 0x4007BC: main (in /home/shreenidhi/Desktop/a.out)
==12248==  Address 0x5a03094 is not stack'd, malloc'd or (recently) free'd
==12248== 
==12248== Invalid read of size 4
==12248==    at 0x4008E3: function() (in /home/shreenidhi/Desktop/a.out)
==12248==    by 0x4007BC: main (in /home/shreenidhi/Desktop/a.out)
==12248==  Address 0x5a03090 is not stack'd, malloc'd or (recently) free'd
==12248== 
0   1   6   
1   2   7   
2   3   8   
3   4   9   
4   5   10  
This is iteration   1
--12248-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting
--12248-- si_code=1;  Faulting address: 0x605A03088;  sp: 0x408bdae00

valgrind: the 'impossible' happened:
   Killed by fatal signal
==12248==    at 0x38058236: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==12248==    by 0x38021ADC: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==12248==    by 0x38021D2D: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==12248==    by 0x380902A7: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==12248==    by 0x3809F7D5: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)

sched status:
  running_tid=1

Thread 1: status = VgTs_Runnable
==12248==    at 0x4C2AC27: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12248==    by 0x400876: function() (in /home/shreenidhi/Desktop/a.out)
==12248==    by 0x4007BC: main (in /home/shreenidhi/Desktop/a.out)

不可能发生?什么是valgrind抱怨?

我看到很多帖子都在解决这个问题,但无法找到解决方案。任何帮助将不胜感激。 谢谢!!!

2 个答案:

答案 0 :(得分:1)

在内循环的最后一次迭代中,i == 4k == 5

这意味着bodyVel[k]bodyVel[5],它是由new bodyVelocity[5]创建的5个元素数组的第6个(因此超出了数组的末尾,这意味着取消引用未定义的行为)。

这是valgrind警告你的问题。

答案 1 :(得分:0)

在C ++中,数组索引从0开始。没有必要执行&#39; int k = i + 1;&#39;,你可以在循环中使用i本身。