-2
typedef struct _set{
    int root;
    int rank;
}Set;

void Kruskal(Graph* g)

{
        Set uni[g->nv];
        Edge result[g->nv - 1];
        int i;
        int count = 0;
        int num = 0;
        int aRoot, bRoot;

        for(i = 0; i < g->nv; i++){
            uni[i].root = i;
            uni[i].rank = 0;
        }

        QuickSort(g, 0, g->ne-1);

        while(count != (g->nv-1) && num != g->ne){
            WeightedUnion(uni, g->path[num].src, g->path[num].dest);
            aRoot = Find(uni, g->path[num].src);
            bRoot = Find(uni, g->path[num].dest);

            if( aRoot != bRoot){
                result[num] = g->path[num];
                count++;
            }
            num++;
        }
        if(count != g->nv-1){
            printf("No spanning tree\n");
        }
        else{
            for(i = 0; i <= count; i++){
                printf("[%d] %d - %d : %d\n",i+1,result[i].src,result[i].dest,result[i].weight);
            }
        }
    }

This is my part of code. The problem is that I can't initialize 'uni[g->nv]'. You can see 'for' loop next to the variable area. And I was sure about that reputation must initialize this array but a result was not. That array didn't include any other values. just empty. I cannot find my problem. Please tell me my problem or mistakes.

  • I run my code in Xcode. Maybe this information is helpful
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
Won
  • 65
  • 7
  • 1
    @StillLearning: Well for me this is clearly C code. Why do you think its not? – ckruczek Aug 13 '15 at 05:58
  • 1
    IF you need the `g->nv`'th element of `uni` define it as `uni[g->nv + 1]`. – alk Aug 13 '15 at 05:58
  • 2
    "The problem is that I can't initialize `uni[g->nv]`" - why not? That loop is fine. Please explain clearly what problem you are having. – M.M Aug 13 '15 at 06:02
  • The prior answer is deleted. That was my solution. I'm not sure but Xcode recognize 'g->nv' as a dynamic things. Like I said, the problem is array didn't have any values. So, I changed that value as a REAL scalar(uni[6] or uni[5]) and then, I can clearly solve my problem. Likewise, 'Edge result[g->nv -1]' has the same problem so I changed it // plus, I found another way that using 'malloc' – Won Aug 13 '15 at 06:09
  • You can also use constructor for struct to initialize, though where your problem is, is too unclear. – vish4071 Aug 13 '15 at 06:16

1 Answers1

3

You are using a variable length array (VLA), that is an array with a length that depends dynamically on an expression during run time. Since the size is not known at compile time, you can't initialize them with an initializer expression, but must do it with a for loop as you are doing.

VLA are usually realized when your program executes on the so-called stack of the function in which it is defined. That stack has a size limit and you have to be careful that you don't overrun it. (And if you do, there is no tool to know directly.)

So don't use VLA as you do for big data of unknown size. Instead, use a pointer and malloc to allocate the memory that you need.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • Thanks. Although my question is unclear, you give me a answer. It's really helpful for me. – Won Aug 13 '15 at 06:23