0

Here's the code:

int EdgeCount = 0;
int numOfEdges = 0;

void addEdge() {
    // some code
    numOfEdges++;
}

int EdgeWeightArray[numOfEdges]; // error

I want that global array with variable parameters to use it later but I couldn't do that because without #define we can't define globally arrays parameters; and #define is not a variable thing. In my code numOfEdges is variable and I couldn't make it constant.

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • This simple thing confused me a lot, and still after pondering so much in hashing and graphs I am unable to solve this. – umair mughal Jun 14 '18 at 13:17
  • 1
    [Variable Length Arrays (VLAs)](https://en.wikipedia.org/wiki/Variable-length_array) cannot be used at the global/file scope. Either move it to some function or allocate it dynamically (`malloc()`). See https://stackoverflow.com/questions/12363558/variable-length-array-in-file-scope for more information. – Acorn Jun 14 '18 at 13:20
  • 1
    You should use a pointer for this: `int* EdgeWeightArray = 0;`. Then you need to allocate memory using `EdgeWeightArray = malloc(numOfEdges, sizeof(int));`. Don't forget to free the pointer after being use – Cyclonecode Jun 14 '18 at 13:23

4 Answers4

1

EdgeWeightArray has global scobe, so it must be a fixed size. But numOfEdges is of course not a constant expression.

What size do you expect EdgeWeightArray to be? Do you expect it to grow when you increment numOfEdges? If so, you nwed to look into dynamic memory allocation; namely malloc and realloc.

Brief example with no error checking:

int numOfEdges = 0;
int *EdgeWeightArray;
void addEdge(some parameters) {
    //SOME CODE
    numOfEdges++;
    EdgeWeightArray = realloc(EdgeWeightArray, numOfEdges * sizeof(EdgeWeightArray[0]));
}
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Can you tell me why you use size as `sizeof(EdgeWeightArray[0])`. It always gives size as **'8'**. To print I use condition in **'for loop'** as sizeof(Array). Hence it executes '8' times no matter what I change. I solved this problem by changing 'for loop' condition. But can I know the reason why size remains '8' – umair mughal Jun 15 '18 at 12:16
  • `EdgeWeightArray = realloc(EdgeWeightArray, numOfEdges * sizeof(EdgeWeightArray[0]));` **SIZE = 8**. `EdgeWeightArray = realloc(EdgeWeightArray, anySize)` **SIZE REMAINS 8**. – umair mughal Jun 15 '18 at 12:18
0

Why not use a global int pointer and allocate memory with malloc() with desired number of elements?

What you are trying to do is not possible because memory for global variables are calculated at compile time and the value of numOfEdges is updated at runtime.

Soumen
  • 1,068
  • 1
  • 12
  • 18
0

In my eyes, you want to create a not well designed code, where the size has global scope and the vector has to have local scope in order to be stored in the stack (according to your starting point). Anyway, you might do something like that:

void defineSize() {
    numOfEdges++;
}

void useIt()
{
    int EdgeWeightArray[numOfEdges];
    /* Use EdgeWeightArray, once the function has been executed, \
       EdgeWeightArray will dissapear */
}
Jose
  • 3,306
  • 1
  • 17
  • 22
0

This is an expansion to Jonathons answer.

If you already know how many Elements there will be at maximum for your array you could use a fixed size for your array. This could be faster in the moment, because there is no reallocation happening. But you have to make sure to do appropriate error handling with this approach. It can be quite error prone.

You would then need a counter to keep track of your Edges, but you appear to already have one of those.

Your code would then look like:

#define MAX_EDGE_COUNT 1000
int EdgeCount = 0;

void addEdge() {
    // some code
    EdgeWeightArray[EdgeCount] = newEdge;
    EdgeCount++;
}

int EdgeWeightArray[MAX_EDGE_COUNT];
  • Yes, but elements of my array are not fixed. They change as numOfEdges increment by 1. In main() I could recall function three times OR could use 'n' times. – umair mughal Jun 15 '18 at 12:23
  • Exactly. But maybe you already now an upper limit, so you could use this limit. Alternatively you could use the c++ std::vector which increments its size automatically. – Phillipp Mevenkamp Jun 15 '18 at 12:55