Segmentation Fault (SIGSEGV) when getting data as input in the vector of struct

前端 未结 2 1863
旧时难觅i
旧时难觅i 2021-01-28 01:32

Segmentation Fault (SIGSEGV). Link to code The code gives Segmentation fault error due to the input taken to store the values for vector of struct.

#include<         


        
2条回答
  •  天涯浪人
    2021-01-28 02:19

    The problem is that you're accessing the vector outside of its bounds. The vector is empty, but v[i] tries to access elements of the vector that don't exist. Therefore the behaviour of the program is undefined.

    I suspect that you may have intended to use the vector's constructor that takes a count of elements as an argument.

提交回复
热议问题