crash caused by a 2D array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: crash caused by a 2D array

  1. #1
    Join Date
    Jun 2009
    Posts
    118

    Question crash caused by a 2D array

    Hi All,

    I have a function such that one of its parameters is a 2D array of type int. The parameter is defined as follows:

    [code]
    int topoGraph [][MAX_VERTICES]
    [\code]

    However, the following code snippet from the body of the function crashes at the specified line:

    [code]
    if( counter >= 4 && !adjMatrixAlreadySet)
    {
    if( edgeCost == 10 )
    {
    topoGraph[srcVertex][dstVertex] = 1; <<<<<---------- The point of crash
    }
    else if( edgeCost == 100000 )//restricted link
    {
    topoGraph[srcVertex][dstVertex] = edgeCost;
    }
    else if( edgeCost == 300000 )//isolated link
    {
    topoGraph[srcVertex][dstVertex] = 0;
    }
    else{}

    adjMatrixAlreadySet = true;

    totalEdges++;
    }
    [\code]

    It seems like there is an undefined behaviour at the specified point of crash since it crashes with different values of srcVertex and dstVertex each time I run it.

    What might be the reason for this crash ?

    Thanks.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,357

    Re: crash caused by a 2D array

    Please, edit your post correcting theCode tags! The closing one must have slash, not a back slash.

    Quote Originally Posted by aryan1 View Post
    I have a function such that one of its parameters is a 2D array of type int. The parameter is defined as follows:

    Code:
    int topoGraph [][MAX_VERTICES]
    However, the following code snippet from the body of the function crashes at the specified line:
    Could you show this function signature, the definition of the array you are passing in it as well as the how you call this function?

    Quote Originally Posted by aryan1 View Post
    What might be the reason for this crash ?
    With the very high probability there is a bug in your code causing the crash.
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2009
    Posts
    118

    Question Re: crash caused by a 2D array

    Here is the prototype of the function:

    Code:
    void fillTopoArray(string topoFileName, int topoGraph[][MAX_VERTICES], int *nodeCount, int *edgeCount);
    Here is how the parameter passed to the function is defined:

    Code:
    int topoGraph[MAX_VERTICES][MAX_VERTICES] = {0};
    Here is how the function is called:

    Code:
    int numberOfNodes = 0, numberOfEdges = 0;
    fillTopoArray("myfile.txt", topoGraph, &numberOfNodes, &numberOfEdges);

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,357

    Re: crash caused by a 2D array

    Quote Originally Posted by aryan1 View Post
    Here is how the function is called:

    Code:
    int numberOfNodes = 0, numberOfEdges = 0;
    fillTopoArray("myfile.txt", topoGraph, &numberOfNodes, &numberOfEdges);
    So you are passing zeros as the array dimensions?
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 1999
    Posts
    27,426

    Re: crash caused by a 2D array

    Quote Originally Posted by aryan1 View Post
    Here is the prototype of the function:

    Code:
    void fillTopoArray(string topoFileName, int topoGraph[][MAX_VERTICES], int *nodeCount, int *edgeCount);
    Here is how the parameter passed to the function is defined:

    Code:
    int topoGraph[MAX_VERTICES][MAX_VERTICES] = {0};
    Here is how the function is called:

    Code:
    int numberOfNodes = 0, numberOfEdges = 0;
    fillTopoArray("myfile.txt", topoGraph, &numberOfNodes, &numberOfEdges);
    OK, so why not debug the function if this is how easy it is to duplicate the error? You told us it crashes on a line in the function, but why not debug the code to determine where things start going wrong? Start from the first line in the function, single step through it, and see at each step which variable(s) start to go off in the wrong direction, or which logic you've written will not work.

    The line of code that crashes doesn't mean that is where the problem started. The problem started earlier, and you have to debug the code to figure this out.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center