0

Here is the code for adding vertex to a graph:

void myGraph::addVertex(const string &newVertex)
{
    if (getIndex(newVertex) != -1)
    {
        std::cout << ("addVertex: ");
        std::cout << newVertex;
        std::cout << (" failed -- vertex already exists.") << std::endl;
        return;
    }

    // if array of vertices is full, we need to expand it and 
    // also expand Edges
    if (sizeof(Vertices)/sizeof(Vertices[0])==numVertices)
    {
        Vertices = resize(Vertices, 2*numVertices + 1);
        Edges = resize(Edges, 2*numVertices + 1);
    }

    Vertices[numVertices++] = newVertex;

}

and here is the code for resizing of Vertices array:

string *myGraph::resize(string array1[], int newSize)
{

    // make array of size equal to new size
    string *temp = new string [newSize];
    int smallerSize = newSize;
    // if the size of input array is less than the new size then smaller size will be that of input array
    if (sizeof(array1)/sizeof(array1[0]) < smallerSize)
    {
        smallerSize = sizeof(array1) / sizeof(array1[0]);
    }
    // loop till smaller size and copy the content of input array to newly created array
    for (int i = 0; i < smallerSize; i++)
    {
        temp[i] = array1[i];
    }

    return temp;
}

When I debug this code, it adds only 1 vertice, i.e. numVertices=1 and on next step it says in Vertices[numVertices++]

1
  • Where do people keep learning this kind of mistake?! Commented Jan 15, 2015 at 16:56

2 Answers 2

2

sizeof is giving the size of the pointer to the data in your array, not the total size of the array. It depends on your platform, but it is very likely that sizeof(string*)/sizeof(string) (equivalent to your size calculation) is always going to return 1. You should probably be using something like std::vector or std::list for this, the right choice depends on how exactly you will be using it. These standard container classes will handle allocating memory and resizing for you, so you don't have to worry about it.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much it really helped. can you guide me how to use 2D vector
@MuhammadOwais You're welcome. :-) A 2D vector would be a vector of vectors: std::vector<std::vector>. If you need detailed help with it, then you can search here and online - I'm sure others have asked about 2D vectors. If you don't find an answer then ask another question.
1

You can fix it by passing old array size to resize:

string *myGraph::resize(string array1[], int array1Size, int newSize)

then:

if (array1Size < smallerSize) {
   smallerSize = array1Size ;
}

as Katie explains, sizeof(array1) in your code is not the size of the actual array, you should rather use string* array1 to make clear that it is pointer to allocated memory on heap

4 Comments

I'll upvote you on that - I almost mentioned the option of passing the size around in my response, but decided he would probably be better off using the stl. There is also a memory leak because resize allocates a new array, but nothing seems to delete the old one.
I agree, vector is the way to go - also there are more places where OP uses sizeof(Vertices)/sizeof(Vertices[0]) to get array size - all of them must be fixed.
i replaced this sizeof (Vertices)/ sizeof(Vertices[0]) by Vertice->length(). its still not working it just add one vertice and then threw bad ptr, it is not resizing array for other vertices
@MuhammadOwais Vertice->length() is wrong, it only compiles because std::string provides function: std::string::length(). If you allocate dynamically array on heap, you must keep its size in some variable, its no java where you can call .length on array to get its size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.