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

  • Home
  • SEARCH
  • 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 7895471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:39:06+00:00 2026-06-03T07:39:06+00:00

Ok, so I am creating a Graph class, I am hoping to be able

  • 0

Ok, so I am creating a Graph class, I am hoping to be able to run algorithms on and maybe add a gui to later on this summer when I have some free time. Right now I have an adjList that is implemented as an array of vectors (one for each vertex), each vector is a list of pointers representing edges going from each associated vertex to other vertices. It is declared as a protected member of my Graph class like so:

std::vector <Node*> *adjList;
adjList = new std::vector<Node*>[V];

I have a side question. Now here I have an array (through a pointer) of vectors that hold pointers. If instead this was not an array, but rather a pointer to just a single vector of node pointers then I could call the constructor like so:

adjList = new std::vector<Node*>(10);

This would allow me to specify a default size for the dynamic array in the vector, but it seems I cannot call the constructor, or at least can’t get the syntax right when I have an array.

Now for my main concern. For each of these vectors in my pointer array I add a number of node pointers to each vector using a call to the new operator in my addVertex method. Now I need to make sure I handle deallocation of all this correctly. I believe I have an understanding of how this should work in C++, but I know pointers are tricky so I wanted to get someone to take a look before I go on adding lots to this code base. I couldn’t find anything with a few searches that was quite like what I have. Here is my deallocation:

for(int i =0; i < V; i++)
    for (unsigned int j = 0; j < adjList[i].size(); j++)
        delete adjList[i][j];
delete adjList;

Will this get free all of the memory. Also is there an easy way for me to verify this for sure, eg. to keep a count of how much memory has been allocated using new while I am debugging?

[EDIT: Update w/ more info]

Here is a link to Google Books showing one of the algorithms I am wanting to implement given in psuedocode. This version of breadth-first search operates on an adjacency list, (an array of lists of pointers). Pointers must be used because of the attributes that are assigned to each node using the adjacency list.

I am wanting to retain these attributes from my BFS algorithm stored to with each node after it is ran. I know it is possible to do this in other ways, perhaps indexing nodes and using parallel arrays to store the attributes. But I wanted to have code that worked similar to this psuedo-code (for BFS in the link).

  • 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-06-03T07:39:07+00:00Added an answer on June 3, 2026 at 7:39 am
    1. Why are you using an array of vectors?
    2. Why are you maintaining a pointer to a vector?
    3. Why are you maintaining a vector of pointers?

    All three of those decisions cost you and directly negate the memory-managing ability of the vector class. A vector is not simply an array that can grow under the covers, it also manages memory for you via a pattern known as RAII.

    When you create a vector of pointers the vector cannot clean up the memory that the pointers refer to upon destruction, so you still need to call delete on every element of the vector.

    When you create a pointer to a vector you cannot take advantage of the fact that the vector deallocates any memory it has allocated in its destructor. Thus, once again, you negate the vector’s ability to manage memory for you as you must call delete on the vector in order to prevent a memory leak.

    When you maintain an array of vectors… well, you’re already using vectors, why not just use a vector<vector<T>>?

    The vector type dynamically allocated memory for you behind the scenes specifically to avoid the sort of issues you are encountering now. Sure, you can manage your own memory (you simply deallocate in the opposite order that you allocated, which you seem to grasp), but why bother when there are mechanisms in place to do it for you?

    I don’t understand the design goal here. Why not simply use a vector<vector<Edge>> and rid yourself of these problems entirely?

    class Edge {
        // whatever
    }
    
    class Graph {
    private:
        // when instances of this class go out of scope, 
        // all of the memory allocated to these vectors is deallocated for you!
        vector<vector<Edge>> vertices;  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im currently creating a graph for an app, using coreplot and have a problem
I am creating graphs using GD::Graph::lines . For option set, I have to write
I have a class with a graph inside. I iterate the graph and create
How do you structure you graphs/nodes in a graph search class? I'm basically creating
I've been thinking about creating a class in C++ on graph theory. The idea
I'm creating a graph using flot javascript library. I have enabled clickable event and
I have the following code: Class B { void generator() { // creating random
I'm having some trouble getting NH to persist my object graph. I have (something
I have a class that does an implementation of a graph (template class) in
We have a problem creating photo album in facebook using android and graph API.

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.