字符串赋值错误

发布于 2024-12-20 00:54:22 字数 3147 浏览 2 评论 0原文

我正在编写一个 ns3 应用程序,为此我需要将向量写入文件,读取文件以再次构造向量并从向量中选取随机元素。这是代码:

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/mf-helper.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/data-rate.h"

#include "ns3/mobility-module.h"
#include "ns3/wifi-module.h"
#include "ns3/ideal-wifi-manager.h"
#include "ns3/wifi-remote-station-manager.h"
#include "ns3/wifi-mode.h"
using namespace ns3;
using namespace std;

void writeFile(string, vector<string>);
void readFile(string, vector<string> &);
unsigned int Random(int,int);
bool Find(vector<string> , string);
void selectNodes(vector<string>);

vector<string> senders;

int main(int argc, char **argv)
{
    vector<string> vect;
    vect.push_back("10.1.1.1");
    vect.push_back("10.1.1.2");
    vect.push_back("10.1.1.3");
    vect.push_back("10.1.1.4");
    vect.push_back("10.1.1.5");
    vect.push_back("10.1.1.6");
    vect.push_back("10.1.1.7");

    writeFile("data.txt", vect);

    vector<string> ret;
    readFile("data.txt",ret);
    selectNodes(ret);
}

void writeFile(string name, vector<string> vs)
{
    ofstream outfile(name.c_str(), ios::out);
    ostream_iterator<string> oi(outfile, "\n");
    copy(vs.begin(), vs.end(), oi);
}

void readFile(string name, vector<string> &vect)
{   
    ifstream file(name.c_str());
    copy(istream_iterator<string> (file), istream_iterator<string>(), back_inserter(vect));
}

void selectNodes(vector<string> ret)
{
    srand(time(NULL));

    string src;
    string dest;

    unsigned int s= ret.size();
    src = ret[Random(1,s)];
    dest = ret[Random(1,s)];


    while(Find(senders, src))
    {
        src = ret[Random(1,s)];
    }

    while (src == dest)
    {
        src = ret[Random(1,s)];
        if (dest != src)
            break;
    }

    cout << "##Source: " << src << std::endl;
    cout << "##Destination: " << dest << std::endl;

    senders.push_back(src);
}

unsigned int Random(int nLow, int nHigh)
{
    return (rand() % (nHigh - nLow + 1)) + nLow;
}

bool Find(vector<string> senders, string addr)
{
    for(unsigned int i=0;i<senders.size();i++)
        if(senders[i] == addr)
            return 1;
    return 0;
}

此代码随机崩溃。这就是 gdb 所说的

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xfffffffffffffff8
0x00007fff8ad5a220 in std::string::_Rep::_M_grab ()
(gdb) bt
#0  0x00007fff8ad5a220 in std::string::_Rep::_M_grab ()
#1  0x00007fff8ad5a29b in std::string::assign ()
#2  0x0000000100002a31 in selectNodes (ret=@0x7fff5fbff7c0) at test_write.cc:74
#3  0x0000000100003cf5 in main (argc=1, argv=0x7fff5fbff998) at test_write.cc:49

为什么字符串分配失败?我发现有些人由于内存泄漏而遇到这个问题。但这里的情况似乎并非如此。我错过了什么吗?

I am writing an ns3 application, for which I need to write a vector to a file, read the file to construct the vector back again and pick random elements from the vector. This is the code:

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/mf-helper.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/data-rate.h"

#include "ns3/mobility-module.h"
#include "ns3/wifi-module.h"
#include "ns3/ideal-wifi-manager.h"
#include "ns3/wifi-remote-station-manager.h"
#include "ns3/wifi-mode.h"
using namespace ns3;
using namespace std;

void writeFile(string, vector<string>);
void readFile(string, vector<string> &);
unsigned int Random(int,int);
bool Find(vector<string> , string);
void selectNodes(vector<string>);

vector<string> senders;

int main(int argc, char **argv)
{
    vector<string> vect;
    vect.push_back("10.1.1.1");
    vect.push_back("10.1.1.2");
    vect.push_back("10.1.1.3");
    vect.push_back("10.1.1.4");
    vect.push_back("10.1.1.5");
    vect.push_back("10.1.1.6");
    vect.push_back("10.1.1.7");

    writeFile("data.txt", vect);

    vector<string> ret;
    readFile("data.txt",ret);
    selectNodes(ret);
}

void writeFile(string name, vector<string> vs)
{
    ofstream outfile(name.c_str(), ios::out);
    ostream_iterator<string> oi(outfile, "\n");
    copy(vs.begin(), vs.end(), oi);
}

void readFile(string name, vector<string> &vect)
{   
    ifstream file(name.c_str());
    copy(istream_iterator<string> (file), istream_iterator<string>(), back_inserter(vect));
}

void selectNodes(vector<string> ret)
{
    srand(time(NULL));

    string src;
    string dest;

    unsigned int s= ret.size();
    src = ret[Random(1,s)];
    dest = ret[Random(1,s)];


    while(Find(senders, src))
    {
        src = ret[Random(1,s)];
    }

    while (src == dest)
    {
        src = ret[Random(1,s)];
        if (dest != src)
            break;
    }

    cout << "##Source: " << src << std::endl;
    cout << "##Destination: " << dest << std::endl;

    senders.push_back(src);
}

unsigned int Random(int nLow, int nHigh)
{
    return (rand() % (nHigh - nLow + 1)) + nLow;
}

bool Find(vector<string> senders, string addr)
{
    for(unsigned int i=0;i<senders.size();i++)
        if(senders[i] == addr)
            return 1;
    return 0;
}

This code crashes randomly. This is what gdb says

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xfffffffffffffff8
0x00007fff8ad5a220 in std::string::_Rep::_M_grab ()
(gdb) bt
#0  0x00007fff8ad5a220 in std::string::_Rep::_M_grab ()
#1  0x00007fff8ad5a29b in std::string::assign ()
#2  0x0000000100002a31 in selectNodes (ret=@0x7fff5fbff7c0) at test_write.cc:74
#3  0x0000000100003cf5 in main (argc=1, argv=0x7fff5fbff998) at test_write.cc:49

Why is the string assignment failing? I found that some people had this porblem due to memory leaks. But that does not seem the case here. Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2024-12-27 00:54:22

这些行存在问题:

 src = ret[Random(1,s)];
 dest = ret[Random(1,s)];

因为 Random 返回的值可能等于超出范围的 sindexret的最大值为s-1

所以解决方案是,要么你这样写:

 src = ret[Random(1,s-1)];
 dest = ret[Random(1,s-1)];

或者,将 Random 定义为:

unsigned int Random(int nLow, int nHigh)
{
    return (rand() % (nHigh - nLow + 1)) + nLow  - 1;
}

我建议你按照上面的建议重新调整 Random ,因为从数学上来说,这样说是合理的Random 生成一个落在 [nLow, nHigh) 范围内的值。 Dikkstra 对此提供了合理的论据。阅读以下内容:

  • 为什么编号应从零开始

这样,您应该通过引用接受向量参数。

There is problem with these lines:

 src = ret[Random(1,s)];
 dest = ret[Random(1,s)];

because the value Random returns could be equal to s which is out of range. The maxim value of index to ret is s-1.

So the solution is, either you write this:

 src = ret[Random(1,s-1)];
 dest = ret[Random(1,s-1)];

Or, define Random as:

unsigned int Random(int nLow, int nHigh)
{
    return (rand() % (nHigh - nLow + 1)) + nLow  - 1;
}

I would suggest you to redine the Random as suggested above, because it is mathematically sound to say that Random generates a value which falls in the range [nLow, nHigh). Dikkstra has provided a sound argument for this. Read this:

By the way, you should accept vector argument by reference.

童话 2024-12-27 00:54:22

我想说的问题是你使用非法索引访问你的向量。向量的索引范围为 0 到 size-1,就像任何其他 C 和 C++ 数组一样。将 Random 调用更改为 Random(0, s - 1)

I would say that the problem is you access your vector with illegal indexes. A vector has an index from 0 to size-1, just like any other C and C++ array. Change your Random calls to Random(0, s - 1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文