-1

I am having some problems with my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void union1(int belongs[],int c1,int c2, int n);

int main()
{
int i=0;
    int j=0;
    int N, num_AERO, num_E;
    if (scanf("%d", &N) != 1) { exit(1); }
    if (scanf("%d", &num_AERO) != 1) { exit(1); }
    int custoAERO[num_AERO];
    for (i = 0; i < num_AERO; i++) 
    {
        int id_aero, cst_aero;
        if (scanf("%d %d", &id_aero, &cst_aero) != 2) { exit(1); }
        custoAERO[i] = cst_aero;
    }
    if (scanf("%d", &num_E) != 1) { exit(1); }
    int *matriz_estradas[N];
    for (i = 0; i < N; i++) 
    {
        matriz_estradas[i] = malloc(sizeof(*matriz_estradas[i]));
    }

    int cid_a, cid_b, cust;
    for(i=0; i< num_E; i++)
    {
        if (scanf("%d %d %d", &cid_a, &cid_b, &cust) != 3) { exit(1); }
        matriz_estradas[cid_a -1][cid_b -1] = cust;
        matriz_estradas[cid_b -1][cid_a -1] = cust;
    }
    for(i=0; i<N; i++)
    {
        int cidade_sozinha = 0;
        for(j=0; j<N; j++)
        {

            if(matriz_estradas[i][j]>0 || matriz_estradas[j][i]>0)
                cidade_sozinha = 1;
        }
        if(cidade_sozinha==0)
        {
            if((sizeof(custoAERO)/sizeof(int))<i+1)
            {
                printf("Insuficiente\n");
                return EXIT_SUCCESS;
            }
        }
    }

The code works fine, but when the value of N is too high, I get a segmentation fault error when alloc'ing in

    for (i = 0; i < N; i++) 
    {
        matriz_estradas[i] = malloc(sizeof(*matriz_estradas[i]));
    }

Is there a maximum of memory I can alloc in an array?

  • 1
    How large is `N`? Are you sure it isn't a stack overflow, since `int *matriz_estradas[N];` allocates the space (for pointers) on the stack? – vgru May 02 '17 at 12:10
  • 1
    @Groo when N is >=100000, might be a stack overflow indeed. – Vasco Morganho May 02 '17 at 12:10
  • 1
    Just move the declaration outside the function, or `malloc` the array also. – vgru May 02 '17 at 12:12
  • Why don't you test for errors? – too honest for this site May 02 '17 at 12:28
  • Each of your `N` 'row' pointers in `matriz_estradas[]` is pointing to `sizeof(int)` of dynamic memory, but from the remainder of your code, it seems your 'row' pointers _should_ be pointing to `N * sizeof(int)` of dynamic memory. I.e. change `matriz_estradas[i] = malloc(sizeof(*matriz_estradas[i]));` to `matriz_estradas[i] = malloc(N * sizeof(*matriz_estradas[i]));`. – Ian Abbott May 02 '17 at 12:31
  • matriz_estradas[i] = malloc(N * sizeof(*matriz_estradas[i])); did resolve a large part of the issue, thank you. – Vasco Morganho May 02 '17 at 12:59

2 Answers2

1

You need to correct this statement

matriz_estradas[i] = malloc(sizeof(*matriz_estradas[i]));

matriz_estradas[i] hasn't been allocated memory yet, and *matriz_estradas[i] tries to access a value from unallocated memory

Pras
  • 4,047
  • 10
  • 20
0

Is there a max array length limit in C++? you can read about memory allocation here and why your code fail to work on , if N is a high number

Community
  • 1
  • 1
Zed Evans
  • 126
  • 1
  • 12