3
votes

I'm trying to work on a Dijikstra implementation and this is the graph generation code that I have

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <math.h>
#define MAX 300
int main (int argc, char *argv[]){
    int v = atoi(argv[1]);
    int SIZE = v*v;
    int* adjMatrix = malloc(sizeof(int)* SIZE);
    graphGeneration(adjMatrix, v);
    free(adjMatrix);
    return 0;
}

void graphGeneration(int* adj, int numV){
    int i, j, r;
    for(i = 0; i< numV; i++){
        for(j=0; j < numV; j++){
            if(i == j){
                adj[i * numV + j] = 0;
            }
            else{
                r = rand() % MAX;
                adj[i * numV + j] = r;
                adj[j * numV + i] = r;
            }
        }
    }

}

When I try a value of v in 1000's it seems to work fine, but when I try to enter a value of v = 10,000+ I get a segfault (Specifically at 50,000 is the number I noticed). Running valgrind gets me the error in the title at this method. Reposting here for convenience:

Invalid write of size 4
at 0x400800: graphGeneration 
by 0x4006E3: main
Address 0x0 is not stack'd, malloc'd or (recently) free'd
Access not within mapped region at address 0x0

Anyone have any ideas for how to debug this or if there are any obvious errors here?

I also noticed this bit in valgrind

Warning: silly arg (-7179869184) to malloc()

which I'm not sure if it's related, but it seems like a strange thing as well.

1
First, what type of argument does malloc() take? Second, what does malloc() return when you get the segfault?Andrew Henle
@AndrewHenle Sorry I'm not sure if I understand but I'll try my best to provide details. what I want to allocate is the size of a 2d integer array in a 1d format. (The input from commandline is the width of the array) The errors I'm getting from the compiler is just Segmentation fault (core dumped)Brain Meme
With any decent modern compiler you should be able to do sizeof(double[v][v]) so you don't have to do the computations manually. Then, check the return of malloc for such large allocations. Third, try your code with small examples, such that you have any chance to debug.Jens Gustedt

1 Answers

6
votes

Have a look at some malloc() manual: Its argument is of type size_t for a reason. int is not guaranteed to hold any possible object size, size_t is. And it is unsigned btw -- negative sizes don't make much sense.

So just write

size_t SIZE = ((size_t)v) * v;

as your v is an int you have to force this multiplication to be done as size_t by casting one of the arguments.

A slightly better way would be to make v an unsigned long and use strtoul() instead of atoi().


Then, check the result of your malloc() before you use it. It might still return NULL, even with a correct size argument. If it does, this simply means you don't have enough RAM available at that moment.

After all, with v=10000 and assuming an int takes four bytes (which is very common), you already attempt to allocate 400 MB at once.