0

I need the syntax correction for dynamic memory allocation of array of list in c++ using malloc its giving runtime error. The first dynamic memory allocation syntax works fine with new keyword but i need using malloc keyword, how can get that memory block with the malloc keyword as im getting with new keyword

#include<bits/stdc++.h>
using namespace std;
class Graph{
    int nodes;
    list<int> *l;
    public:

    Graph(int nodes)
    {
        this->nodes=nodes;
     // l=new list<int>[nodes];
        l=(list<int>*)malloc(sizeof(list<int>[nodes]));// syntax correction of this is needed

        if(!l)
        {
            cout<<"memory not allocated";
        }
        
    }

    void add_edge(int u,int v)
    {
        l[u].push_back(v);
        l[v].push_back(u);
    }
    int size()
    {
        return sizeof(l);
    }
    void print_list()
    {
        for(int i=0;i<nodes;i++)
        {   
            cout<<"adjacency nodes node "<<i<<" is : ";
            for(auto ele:l[i])
            {
                cout<<ele<<" ";
            }
            cout<<endl;
        }
    }

};


int main()
{
    int n;
    cin>>n;
    Graph graph=Graph(n);
    for (int i = 0; i < n; i++)
    {
        int u,v;
        cin>>u>>v;
        graph.add_edge(u,v);
    }
    graph.print_list();

    cout<<graph.size();
    
} 
AMU
  • 1
  • 1
  • 4
    Why are you using `malloc` instead of `new`? – NathanOliver Aug 11 '21 at 18:58
  • 2
    Why would you `malloc` a `std::list`? It's elements are already heap allocated, it serves no purpose to heap allocate the container itself. – Cory Kramer Aug 11 '21 at 19:00
  • i just want to know how can i allocate the memory with malloc – AMU Aug 11 '21 at 19:01
  • If it causes runtime error, its not syntax. Please provide a [mcve]. – Quimby Aug 11 '21 at 19:02
  • can you please correct the inside of that malloc function – AMU Aug 11 '21 at 19:02
  • `malloc(nodes * sizeof(list));` is correct, but still really bad and useless. – Quimby Aug 11 '21 at 19:03
  • 2
    You can allocate memory with `malloc` but it will _not_ start the lifetime of objects. You're in for a world of pain if you continue down this route. – Ted Lyngmo Aug 11 '21 at 19:04
  • malloc(nodes * sizeof(list)) i tried this still giving runtime error – AMU Aug 11 '21 at 19:06
  • @AMU What do you expect `malloc(nodes * sizeof(list))` to do? – Ted Lyngmo Aug 11 '21 at 19:07
  • let me know what should i use malloc or new and why – AMU Aug 11 '21 at 19:10
  • 1
    As mentioned before `new` is not `malloc`. `new` allocates memory for, and constructs the object, `malloc` allocates memory where an object might be placed. Nothing is actually placed there unless you do it yourself. You are accessing raw memory you have not initialised. Unless you write C or actually know what you are doing use `new` (or even better smart pointers, or other stl magic) – Lala5th Aug 11 '21 at 19:10
  • For now (and many many years going forward) - use `new`/`new[]` and `delete`/`delete[]` - and you usually don't even need that. Use `std::vector`:s to save yourself from the pain of manual memory management. Do you have a teacher that suggested `malloc` or are you self-converting from having programmed in C? – Ted Lyngmo Aug 11 '21 at 19:10
  • "_and why_" - is because `malloc` only allocates memory while `new` construct object(s) by making sure there is memory for the objects and then by calling the constructor(s) of the object(s) to be created. The constructor(s) may then allocate additional memory and/or initialize member variables etc. – Ted Lyngmo Aug 11 '21 at 19:22
  • My hierarchy of allocation in order of preference: [Automatic](https://en.wikipedia.org/wiki/Automatic_variable), [RAII-observant container](https://en.cppreference.com/w/cpp/language/raii)(library container or smart pointer), `new`/`new[]` (almost always immediately wrapped in RAII), `malloc` and family (also almost always immediately wrapped in RAII). `malloc` is only used for extremely low-level building blocks. – user4581301 Aug 11 '21 at 19:25
  • @AMU -`#include` -- I bet you are learning from a very poor website. No good C++ book has this non-standard header, and no good teacher would even introduce this header. It seems you are trying to implement some sort of data structure without knowledge of how to use C++ in the proper fashion, and unfortunately you're blindly copying examples from that (and other) websites that do *not* emphasize learning C++ properly. Add to that, the usage of `malloc`, and I am almost certain you are picking these bad habits up from those (I won't name them) websites. – PaulMcKenzie Aug 11 '21 at 21:30

0 Answers0