View Single Post
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Graph help please

 
0
  #2
Nov 12th, 2008
It might help if you explain what your Graph-class is supposed to do.

By the way, what is your constructor definition supposed to do?

You assign numV to be 0 then you de-reference an uninitialized array of vertices.

You probably meant to do something like this--

  1. Graph::Graph(int num) : numV(num), vertices(new VertexNode[num]){
  2.  
  3. memset (vertices, 0, sizeof(VertexNode) * num);
  4. }

-- the numV looks like it represents the total number of VertexNodes (or vertices) and it is initialized via pre-initialization, along with your pointer to VertextNodes.

The memset in the constructor body is necessary because by calling new you are invoking the default constructor of an instance of a class ( or struct or union in C++ ) in which it is typically the responsibility of the that constructor to initialize members of the object. However, because your struct of type VertexNode has no constructor you have to explicitly set the values inside the struct. A good way of doing so is by using memset. In the above code the memset sets the bit-value of every byte for each VertexNode to zero, so everything has a value of 0 in your struct.

So this means...

  1. struct VertexNode // Structure representing a vertex
  2. {
  3. string vname; // Name of vertex
  4. bool mark; // Marked flag
  5. EdgeNode* edgePtr; // Pointer to list of edges
  6. };

*string's members are set to zero (the backing char-pointer a string has is likely to point to null),
as well as any other members in the string object.

*bool is set to false (because 0 is false and any other number is true)

*EdgeNode points to null (size of pointer is typically 4-8 bytes, depending on the machine (might be more on some machines but lets assume 8 bytes), so lets assume each byte's character determines where the EdgeNode is pointing to. If the bytes are set to zero, then the only location EdgeNode is pointing to is NULL, or location 0x00000000 where each 0 (except the first one followed by an x) represents the value of the location in bytes via hexidecimal).

This should happen to each element in your array sense memory is being set for not just one VertexNode, but the amount that is pre-initialized.

That's probably overkill for an explanation of the choice of initialization for your array of values but hopefully it helps @_@.

Note: It may be better to migrate the initialization of your numV and VertexNode pointer to the body of the constructor so you can catch an out-of-memory error. This way if an error occurs and memory cant be assigned to the VertexNode pointer, your application wont crash for attempting to 'initialize' memory that may not belong to it.

Linkage
Last edited by Alex Edwards; Nov 12th, 2008 at 12:37 am.
Reply With Quote