Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6896269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:00:53+00:00 2026-05-27T07:00:53+00:00

I am writing an ns3 application, for which I need to write a vector

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:00:53+00:00Added an answer on May 27, 2026 at 7:00 am

    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:

    • Why numbering should start at zero

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing to a file is not working after hosting the web application, whereas they
Writing my first silverlight application. I need to deliver some bitmap that the customer
Writing a program in which I need to split strings from a struct linked
Writing my first Linq application, and I'm trying to find the best way to
Writing a .NET DLL how do I find Application.ProductName ? EDIT: Obviously, importing Windows.Forms
Writing an application in Cocoa that will take input from the user, format it
Writing some additional classes for an existing GWT project. I need to: Request a
Writing a file utility to strip out all non-ASCII characters from files. I have
Writing a rather simple command line tool in ruby I need to report meaningful
writing to a text file is working good...But my problem is text file is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.