English 中文(简体)
Valgrind: Address 0x0 is not ack d, malloc d or (recently) free d for greater content Value only
原标题:Valgrind: Address 0x0 is not stack d, malloc d or (recently) free d for larger input values only

我不想努力执行迪吉克勒,这是我已经制定的图表制作法。

#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

没有人会想如何回避,或者这里是否有任何明显错误?

我也注意到了在朝圣时的这个界限。

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

我不敢肯定这一点,但似乎也是一种奇怪的事情。

问题回答

查阅malloc( Manual: 出于某种原因,其论点为“size_t。 <代码>int不保证持有任何可能的物体大小,size_t。 它是没有签字的。 消极规模没有意义。

因此,只是字不提

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

您的<代码>v为int, 您必须把这一多重复作为size_t, 提出其中一项论点。

稍微改进的方式是将<代码>v改为un signed long,并使用strtoul()而不是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.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签