如下因素的功能的代碼:C++函數給出存儲器的alloc錯誤
void add_edge(int** point, int start, int end)
{
int x;
//start->end edge
x=point[start][0];
if(x>=2)
{
int* temp=new int[x+1];
for(int i=0; i<=x; i++)
temp[i]=point[start][i];
// delete[] point[start];
point[start]=temp;
}
point[start][++point[start][0]]=end;
if(start==end) return;
//end->start edge
x=point[end][0];
if(x>=2)
{
int* temp=new int[x+1];
for(int i=0; i<=x; i++)
temp[i]=point[end][i];
// delete[] point[end];
point[end]=temp;
}
point[end][++point[end][0]]=start;
}
具有存儲器分配錯誤,我無法找到。
從Valgrind的,我得到這樣的:
==9253== Invalid write of size 4
==9253== at 0x8048643: add_edge(int**, int, int) (c1.cpp:34) (line: point[start][++point[start][0]]=end;)
==9253== by 0x8048C8B: main (c1.cpp:184) (line: add_edge(point,start,end);)
==9253== Address 0x2d6a7074 is 0 bytes after a block of size 12 alloc'd
==9253== at 0x4025FE5: operator new[](unsigned int) (vg_replace_malloc.c:299)
==9253== by 0x80485D0: add_edge(int**, int, int) (c1.cpp:28)
==9253== by 0x8048C8B: main (c1.cpp:184)
==9253==
==9253== Invalid write of size 4
==9253== at 0x80486EA: add_edge(int**, int, int) (c1.cpp:48) (line: point[end][++point[end][0]]=start;)
==9253== by 0x8048C8B: main (c1.cpp:184)
==9253== Address 0x2d6a7134 is 0 bytes after a block of size 12 alloc'd
==9253== at 0x4025FE5: operator new[](unsigned int) (vg_replace_malloc.c:299)
==9253== by 0x8048677: add_edge(int**, int, int) (c1.cpp:42)
==9253== by 0x8048C8B: main (c1.cpp:184)
==9253==
--9253-- REDIR: 0x41e07c0 (__GI_strlen) redirected to 0x4026ccc (__GI_strlen)
這是一個令人不快的代碼構造,你在那裏使用... –
我也不明白代碼的目的。你如何表達圖表?用簡單的話來解釋可能會幫助我們和你。它似乎比它需要的複雜得多。 –
噢,順便說一下:如果你按照http://sscce.org/的建議,我們可能真的能夠幫助你。 –