关于两个指针的矢量赋值的段错误

时间:2012-05-01 12:53:44

标签: c++ memory-management assign

我正在尝试使用assign方法分配一个向量,但是在我的笔记本电脑上使用相同的代码段错误但是在我们的计算群集上工作。 segfault来自于分配调用,

  double* val = mat.address_data();
  int* rp     = mat.address_major();
  int* cp     = mat.address_minor();
  // assign to the reserved location
  row_pointer_data.assign( rp,
                           rp+(m+1) );
  col_index_data.assign( cp,
                         cp+nz );
  value_data.assign( val,
                     val+nz );

其中mat是MTL4矩阵,指向其内部数据,指针指向int和double。这些指针分别指向nz双精度块,m + 1个整数和nz整数。我在两个环境中使用intel c ++编译器,并且所有代码都是相同的,对于两个shell,即bash shell,堆栈大小是无限的。

但是,col_index_data.assign()调用会在我的笔记本电脑和群集上产生分段错误。

使用gdb或valgrind指向与之相关的同一行,我猜一个汇编命令_memmove_sse3(),用于调用assign方法,即

> #0  __memmove_ssse3 () at ../sysdeps/x86_64/multiarch/memcpy-ssse3.S:2928
> #1  0x000000000042df49 in VibroSys::read_triplet_data (mat=Traceback (most recent call last):   File
> "/home/utab/external_libraries/gdb_printers/python/mtl/printers.py",
> line 124, in to_string
>     A= empty_matrix(nr, nc)   File "/home/utab/external_libraries/gdb_printers/python/mtl/printers.py",
> line 38, in empty_matrix
>     return [copy.deepcopy(nr * ['0']) for c in range(nc)] MemoryError
> 
> , row_pointer_data=std::vector of length 19173284840251003, capacity
> 429530399489 = {...}, row_index_data=<error reading variable: Cannot
> access memory at address 0xa3bf88>, 
>     col_index_data=<error reading variable: Cannot access memory at address 0x100008>, value_data=std::vector of length 0, capacity
> 4764228, col_indices_on_row=Traceback (most recent call last):   File
> "/home/utab/external_libraries/stl_printers/python/libstdcxx/v6/printers.py",
> line 427, in children
>     rep_type = find_type(self.val.type, '_Rep_type')   File "/home/utab/external_libraries/stl_printers/python/libstdcxx/v6/printers.py",
> line 43, in find_type
>     field = typ.fields()[0] IndexError: list index out of range
> 
> std::map with 25701680 elements, is_symmetric=false) at
> /home/utab/vibroSys/src/boost_matrix_utilities.cc:354
> #2  0x000000000042e172 in VibroSys::extract_sub_matrix (input_matrix=Traceback (most recent call last):   File
> "/home/utab/external_libraries/gdb_printers/python/mtl/printers.py",
> line 124, in to_string
>     A= empty_matrix(nr, nc)   File "/home/utab/external_libraries/gdb_printers/python/mtl/printers.py",
> line 38, in empty_matrix
>     return [copy.deepcopy(nr * ['0']) for c in range(nc)] MemoryError
> 
> , sub_index1=std::vector of length 19173284840251003, capacity
> 429530399489 = {...}, sub_index2=<error reading variable: Cannot
> access memory at address 0xa3bf88>, sub_matrix=
>     <error reading variable: Cannot access memory at address 0x100000>) at /home/utab/vibroSys/src/boost_matrix_utilities.cc:995
> #3  0x000000000040f93a in main (argc=36433120, argv=0xa3c000) at timing_test.cc:145

在两种不同的环境中,相同代码的这两种不同行为可能是什么原因?

1 个答案:

答案 0 :(得分:0)

在第二个架构上编译时发现错误是很常见的。考虑一个缓冲区溢出:如果它没有负面结果(例如,覆盖重要的东西,或超出界限和seg错误),你可能不会注意到它存在。然后在另一台机器上,可执行文件以不同的方式排列,并且溢出确实会产生负面影响,所以你会注意到它。

是的,你有一个错误。它出现在一个地方而不出现在另一个地方的事实有时并不意味着它不是一个地方的错误,期间,潜在的负面后果。