Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Segmentation fault / multidimansional arrays
Forum Updated to NodeBB v4.3 + New Features

Segmentation fault / multidimansional arrays

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    illyaSlobozhanin
    wrote on last edited by illyaSlobozhanin
    #1

    Hi all. I've got a problem during creating 2-dimensional array.

    Graph::Graph(){
        vertexList = new Vertex[MAX_VERTS];
    
        adjMat = new int*[MAX_VERTS]; // int** adjMat;
        for(int i(0); i < MAX_VERTS; i++){ //const int MAX_VERTS = 10;
            adjMat[MAX_VERTS] = new int[MAX_VERTS];
        }
    
        for(int i(0); i < MAX_VERTS; i++)
            for(int j(0); j < MAX_VERTS; j++)
                adjMat[i][j] = 0; //sigmentation fault here
    
        nVerts = 0;
    }
    

    qt 5.5.1, gcc compiler.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Nothing Qt specific here, look again at your for loop, you are initializing the same value over and over.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      I 1 Reply Last reply
      0
      • hskoglundH Online
        hskoglundH Online
        hskoglund
        wrote on last edited by
        #3

        Yes, exactly as SGaist says above, try changing

        adjMat[MAX_VERTS] = new int[MAX_VERTS];
        

        to

        adjMat[i] = new int[MAX_VERTS];
        
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          Nothing Qt specific here, look again at your for loop, you are initializing the same value over and over.

          I Offline
          I Offline
          illyaSlobozhanin
          wrote on last edited by
          #4

          @SGaist thanks a lot=)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved