1

This error occurs in the "bfs" function of my C++ code. I'm trying to code a graph data structure but it seems like there is something that is either not initialized or not storing input as it should. Can anyone please help me with this?

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
using namespace std;

void bfSearch(int n,vector<int> adj[])
{
    cout<<"Inside function"<<endl;
    vector<int> bfsv;
    vector<int> vis(n+1,0);

    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            queue<int> q;
            q.push(i);
            vis[i]=1;
            while(!q.empty())
            {
                int node=q.front();
                q.pop();
                bfsv.push_back(node);
                for(auto it:adj[node])
                {
                    q.push(it);
                    vis[it]=1;
                }
            }
        }
    }
    for(auto it:bfsv)
    {
        cout<<it<<"  ";
    }
}


int main()
{
    int n,m;
    cin>>n>>m;
    vector<int> adj[n+1];
    for(int i=0;i<m;i++)
    {
        int u,v;
        cin>>u>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    // vector<int> res=bfSearch(n,adj);
    // for(auto it:res)
    // {
    //     cout<<it<<" ";
    // }
    // cout<<endl;
    bfSearch(n,adj);
}

Is it because of the vector or queue?

Dominique Fortin
  • 2,212
  • 15
  • 20
  • Please, take a look at ["Why aren't variable-length arrays part of the C++ standard?"](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). – rawrex Aug 12 '21 at 05:44
  • What is the input to your program? Have you tried debugging? Please don't tag c and c++ unless you are truly using both languages – Alan Birtles Aug 12 '21 at 05:45
  • Search for that error message to get an idea what it means. Now, that doesn't explain why it happens in your code, but should get you started. Also, for a [mcve], you should replace the manual input with hard-coded numbers. – Ulrich Eckhardt Aug 12 '21 at 06:03
  • You know about `vector`, so why reach for a non-standard error-prone variable-length array? – molbdnilo Aug 12 '21 at 07:10

1 Answers1

0

Generally, std::bad_alloc means that Heap memory allocation failed (and that your system is out of RAM).

In your case, it could be caused by too high console-input (for n, m, u or v variable).

BTW, always first try to use debugger to locate exact line-of-code which caused the issue (to help people trying to answer).

Top-Master
  • 7,611
  • 5
  • 39
  • 71
  • You can edit your answer if your want to add more information. As `bfSearch` isn't modifying `adj` a const reference would be more appropriate than a pointer – Alan Birtles Aug 12 '21 at 06:32
  • I hadn't noticed it was an array, in that case it isn't being copied – Alan Birtles Aug 12 '21 at 07:06
  • Oh then should i use vector> for adj?? – Aarushi Shanker Aug 12 '21 at 11:12
  • @AarushiShanker that would just convert your stack allocated `array[]` into Heap as well, and cause even more RAM to be used, simply because stack-memory is something **reserved just for your program's stack** the moment you open the App (each thread has new stack!). – Top-Master Aug 12 '21 at 11:29