Thread: 2 dimensional dynamic array SEG FAULT!

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    10

    2 dimensional dynamic array SEG FAULT!

    I am getting a Seg Fault on the 2 lines denoted below. Any ideas why i am getting a seg fault? Any help would be amazing! thanks.

    Code:
            int** pathArry;
            int i;
            int count;
    
    
            printf("-----Allocating Memory-----\n");
            pathArry = (int**)malloc(arry1Max * sizeof(int*));
            for (int i = 0; i < arry1Max; i++)
            {
              pathArry[i] = (int*)malloc(arry2Max * sizeof(int));
            }
    
    
            printf("-----Calculating Paths-----\n");
            count = 0;
            do{
                    for (i = 0; i <= cities;i++){
                            if (i == cities){
                                    pathArry[count][i] = 0; // SEGFAULT
                            }else{
                                    pathArry[count][i] = nodes[i]; // SEGFAULT
                            }
                    }
                    count++;
            } while (next_permutation(nodes+1,nodes+cities));

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    What's the relation between arry2Max and cities?
    Have you include <stdlib.h>? The cast to the return value of malloc makes me suspect you haven't. Make sure you have the include and lose the cast.
    You should also test whether the allocation succeeded.

    Do you get a segfault when both count and i are 0?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2-Dimensional Dynamic Array
    By juice_box in forum C Programming
    Replies: 4
    Last Post: 11-30-2010, 06:49 PM
  2. Dynamic two dimensional Array of Characters
    By nickman in forum C Programming
    Replies: 4
    Last Post: 11-24-2010, 03:42 PM
  3. Dynamic 2 Dimensional Array
    By mrb260478 in forum C Programming
    Replies: 23
    Last Post: 06-21-2008, 07:19 AM
  4. Dynamic 2 dimensional array with new?
    By Quantum1024 in forum C++ Programming
    Replies: 10
    Last Post: 11-19-2005, 02:46 PM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM