在C ++中,自定义数据结构的函数返回向量抛出分段错误

时间:2018-09-02 06:07:41

标签: c++ vector

我的程序编译了,问题出在运行时。

我有以下C ++程序:

#include <iostream>
#include <vector>
#include <string>

struct SpotifyRelease {
    std::string release_date, id, href, name;
};


SpotifyRelease get_release(const std::string id, const std::string href, const std::string name, const std::string release_date) {
    SpotifyRelease sr;
    sr.id = id;
    sr.href = href;
    sr.name = name;
    sr.release_date = release_date;
    return sr;
}

std::vector<SpotifyRelease> spotify_get_latest_releases() {
    std::vector<SpotifyRelease> v1;
    std::vector<SpotifyRelease>::iterator it;
    v1.insert(it, get_release("1OyADPvYTlmT5hV0mtxLAY", "https://api.spotify.com/v1/albums/1OyADPvYTlmT5hV0mtxLAY", "Bloom", "2018-08-31"));
    v1.insert(it, get_release("3HNnxK7NgLXbDoxRZxNWiR", "https://api.spotify.com/v1/albums/3HNnxK7NgLXbDoxRZxNWiR", "Kamikaze", "2018-08-31"));
    v1.insert(it, get_release("1Xf5RZ7zQjF2WEkk7KpZVi", "https://api.spotify.com/v1/albums/1Xf5RZ7zQjF2WEkk7KpZVi", "Superhero", "2018-08-30"));
    v1.insert(it, get_release("317hvEJEf6RTxPT4m2NiRb", "https://api.spotify.com/v1/albums/317hvEJEf6RTxPT4m2NiRb", "4Freedom", "2018-08-30"));
    v1.insert(it, get_release("6ggnUqpjfkDULwX0dr164j", "https://api.spotify.com/v1/albums/6ggnUqpjfkDULwX0dr164j", "XTCY", "2018-08-30"));
    v1.insert(it, get_release("6ST7naJFCe9iBeOleU5Ccu", "https://api.spotify.com/v1/albums/6ST7naJFCe9iBeOleU5Ccu", "Michael Jackson x Mark Ronson: Diamonds are Invincible", "2018-08-29"));
    v1.insert(it, get_release("6OX3blc5vb3huGk2PTM0Kk", "https://api.spotify.com/v1/albums/6OX3blc5vb3huGk2PTM0Kk", "The Dark Side / Something Human / Thought Contagion / Dig Down", "2018-08-30"));
    v1.insert(it, get_release("4o8Y3L9WQvk3L3u9M0gngq", "https://api.spotify.com/v1/albums/4o8Y3L9WQvk3L3u9M0gngq", "Don't Leave Me Alone (feat. Anne-Marie) [Oliver Heldens Remix]", "2018-08-31"));
    v1.insert(it, get_release("0LOJHhz9ybWLVMLucHYtCe", "https://api.spotify.com/v1/albums/0LOJHhz9ybWLVMLucHYtCe", "REMEDY", "2018-08-31"));
    v1.insert(it, get_release("1d1MKgYRqFUs8Mner4uF4y", "https://api.spotify.com/v1/albums/1d1MKgYRqFUs8Mner4uF4y", "Hola", "2018-08-31"));
    v1.insert(it, get_release("3qX5J1RkaFxSXEqRc8Qzmo", "https://api.spotify.com/v1/albums/3qX5J1RkaFxSXEqRc8Qzmo", "Amigos Con Derechos", "2018-08-31"));
    v1.insert(it, get_release("3OLSEsIMEgtcn1U9s03qRR", "https://api.spotify.com/v1/albums/3OLSEsIMEgtcn1U9s03qRR", "BEBE", "2018-08-31"));
    v1.insert(it, get_release("2qIbHfs93z6eDwGoAdCkXA", "https://api.spotify.com/v1/albums/2qIbHfs93z6eDwGoAdCkXA", "8 Letters", "2018-08-31"));
    v1.insert(it, get_release("3LBRCisCaxgQjb5nwSMPgU", "https://api.spotify.com/v1/albums/3LBRCisCaxgQjb5nwSMPgU", "Big Red Machine", "2018-08-31"));
    v1.insert(it, get_release("29KdLwIDFZ8DSSgxOQ8kLa", "https://api.spotify.com/v1/albums/29KdLwIDFZ8DSSgxOQ8kLa", "Love Wins", "2018-08-31"));
    v1.insert(it, get_release("1aG5r6TZdMVUeNGY6Lf4YP", "https://api.spotify.com/v1/albums/1aG5r6TZdMVUeNGY6Lf4YP", "Never Comin Down", "2018-08-31"));
    v1.insert(it, get_release("0AykQgBtKmWJkQvomIKYSF", "https://api.spotify.com/v1/albums/0AykQgBtKmWJkQvomIKYSF", "Rotation 112th (feat. Rich The Kid) [Remix]", "2018-08-31"));
    v1.insert(it, get_release("28cah7y6l6TUl5G7aP5pZf", "https://api.spotify.com/v1/albums/28cah7y6l6TUl5G7aP5pZf", "Serious", "2018-08-30"));
    v1.insert(it, get_release("1xKEX6KhO9pRM85WT7aOel", "https://api.spotify.com/v1/albums/1xKEX6KhO9pRM85WT7aOel", "Weed Garden", "2018-08-31"));
    v1.insert(it, get_release("5eOlKFDAN9dXwYycRqEmnP", "https://api.spotify.com/v1/albums/5eOlKFDAN9dXwYycRqEmnP", "Flow State", "2018-08-31"));
    return v1;
}

int main() {
    std::vector<SpotifyRelease> latest_releases;
    SpotifyRelease release;
    latest_releases = spotify_get_latest_releases();
    return 0;
}

问题是当我尝试运行程序时,我的程序在int main()的第二行中以代码11退出

我不太确定为什么,因为该程序对我来说还不错。

任何对正确方向的帮助将不胜感激。

2 个答案:

答案 0 :(得分:5)

您将未初始化的迭代器传递给insert。这是不确定的行为。

您可能指的是类似auto it = begin(v1);的东西,但这仍然行不通,因为您没有在任何地方重新分配it,并且std::vector::insert可能会使向量元素的所有现有迭代器无效

我想您还是真的不想使用insert。只需使用push_back

v1.push_back(get_release("1OyADPvYTlmT5hV0mtxLAY", "https://api.spotify.com/v1/albums/1OyADPvYTlmT5hV0mtxLAY", "Bloom", "2018-08-31"));
// ...

答案 1 :(得分:2)

问题是您正在使用{em>未初始化的无效 insert()来调用iterator。要使其正常工作,您需要初始化迭代器并通过从insert()返回值更新它来使其保持有效,如下所示:

std::vector<SpotifyRelease>::iterator it{v1.begin()};
it = v1.insert(it, get_release("1OyADPvYTlmT5hV0mtxLAY", "https://api.spotify.com/v1/albums/1OyADPvYTlmT5hV0mtxLAY", "Bloom", "2018-08-31"))++;
...

但在您的情况下,仅将那些v1.insert(...);行更改为完全不包含iterator似乎更简单:

v1.push_back(get_release("1OyADPvYTlmT5hV0mtxLAY", "https://api.spotify.com/v1/albums/1OyADPvYTlmT5hV0mtxLAY", "Bloom", "2018-08-31"));
...

或者更好的是,在没有其他未指定原因的情况下根本不需要使用get_release() -您可以保存所有来回的潜在复制,只需执行以下操作:

v1.push_back({"1OyADPvYTlmT5hV0mtxLAY", "https://api.spotify.com/v1/albums/1OyADPvYTlmT5hV0mtxLAY", "Bloom", "2018-08-31"});
...