I am running breadth first search and bellman ford algorithms on large sets of graph data in the format (u, v, weight) from an input file.
I initialize in the breadth first search, that all vertex's should be marked 0 for unvisited.
Later in the program, since I am calling BFS after every time I add an edge, rather than at the end of the program all together (it's part of a research project on bellman ford and BFS even though it doesn't quite make sense) I reinitialize the vertex array to unvisited.
However, I am getting a segmentation fault when I run larger sets it seems when I reinitialize the vertex array. I make the assumption about larger sets because I have a few smaller sets of test data going from 8 vertices, to 10, then at 100 and larger it fails.
Here is how I initialize at the very beginning of the program:
for(i=0;i<numberVertices;i++)
{
vertexArray[i].v = i+1;
vertexArray[i].adj = NULL;
vertexArray[i].marked = 0;
if(i==0)
{
vertexArray[i].weight = 0;
}
else{
vertexArray[i].weight = 10000;
}
}
And here is how I reinitialize at the end of a while loop set to break when it reaches the end of the file directly after BFS finishes:
BFS(q, u)
for(i=0;i<numberVertices;i++)
{
vertexArray[i].marked = 0;
}
Like I said it seems to work for smaller data sets, but I do not understand why when I reinitialize it seems to seg fault.
Please let me know your thoughts and suggestions!
Valgrind Output On An Example Case:
==6634== Memcheck, a memory error detector
==6634== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==6634== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==6634== Command: ./a.out 100S.txt
==6634==
==6634== Conditional jump or move depends on uninitialised value(s)
==6634== at 0x400C13: checkonqueue (bfs.c:160)
==6634== by 0x400B7F: BFS (bfs.c:142)
==6634== by 0x40094A: main (bfs.c:103)
==6634==
==6634== Invalid write of size 4
==6634== at 0x40096E: main (bfs.c:107)
==6634== Address 0x4d00000041 is not stack'd, malloc'd or (recently) free'd
==6634==
==6634==
==6634== Process terminating with default action of signal 11 (SIGSEGV)
==6634== Access not within mapped region at address 0x4D00000041
==6634== at 0x40096E: main (bfs.c:107)
==6634== If you believe this happened as a result of a stack
==6634== overflow in your program's main thread (unlikely but
==6634== possible), you can try to increase the size of the
==6634== main thread stack using the --main-stacksize= flag.
==6634== The main thread stack size used in this run was 10485760.
==6634==
==6634== HEAP SUMMARY:
==6634== in use at exit: 3,448 bytes in 181 blocks
==6634== total heap usage: 181 allocs, 0 frees, 3,448 bytes allocated
==6634==
==6634== LEAK SUMMARY:
==6634== definitely lost: 0 bytes in 0 blocks
==6634== indirectly lost: 0 bytes in 0 blocks
==6634== possibly lost: 0 bytes in 0 blocks
==6634== still reachable: 3,448 bytes in 181 blocks
==6634== suppressed: 0 bytes in 0 blocks
==6634== Reachable blocks (those to which a pointer was found) are not shown.
==6634== To see them, rerun with: --leak-check=full --show-reachable=yes
==6634==
==6634== For counts of detected and suppressed errors, rerun with: -v
==6634== Use --track-origins=yes to see where uninitialised values come from
==6634== ERROR SUMMARY: 16120 errors from 2 contexts (suppressed: 6 from 6)
Segmentation fault (core dumped)
check on queue function:
int checkonqueue(int * q, int value)
{
int i = 0;
for(i=0;i < max;i++)
{
if(q[i] == value)
{
return 1;
}
}
return 0;
}
line 160 is the line of the if condition
Related
I have an error of Segmentation Fault caused by line
*head = malloc(sizeof(struct node)+1);'
I'm pretty sure that I used the construct node and the malloc in the same way in others cases where all worked fine.
The program print here1 and then a Core Dump happen.
this is my code:
struct node {
//int val ;
struct node * next;
unsigned char string[];
} ;
void init_list(struct node ** head) {
printf("here1 \n");
fflush(stdout);
*head = malloc(sizeof(struct node)+1);
printf("here2\n");
fflush(stdout);
if(!(*head)){
printf("error malloc \n");
fflush(stdout);
return ;
}
//(*head) -> val = -1;
(*head) -> next = NULL;
((*head) -> string)[0]= '\0';
return ;
}
int main(void) {
struct node ** head;
init_list(head) ;
printf("hereee\n");
fflush(stdout) ;
fini_list(head);
return 1;
}
This is what Valgrind returns me:
==6688== Memcheck, a memory error detector
==6688== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6688== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==6688== Command: ./ex3
==6688==
heree
==6688== Invalid write of size 8
==6688== at 0x109267: init_list (ex3.c:43)
==6688== by 0x1092E9: main (ex3.c:66)
==6688== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==6688==
==6688==
==6688== Process terminating with default action of signal 11 (SIGSEGV)
==6688== Access not within mapped region at address 0x0
==6688== at 0x109267: init_list (ex3.c:43)
==6688== by 0x1092E9: main (ex3.c:66)
==6688== If you believe this happened as a result of a stack
==6688== overflow in your program's main thread (unlikely but
==6688== possible), you can try to increase the size of the
==6688== main thread stack using the --main-stacksize= flag.
==6688== The main thread stack size used in this run was 8388608 .
==6688==
==6688== HEAP SUMMARY:
==6688== in use at exit: 9 bytes in 1 blocks
==6688== total heap usage: 2 allocs, 1 frees, 1,033 bytes allocated
==6688==
==6688== LEAK SUMMARY:
==6688== definitely lost: 9 bytes in 1 blocks
==6688== indirectly lost: 0 bytes in 0 blocks
==6688== possibly lost: 0 bytes in 0 blocks
==6688== still reachable: 0 bytes in 0 blocks
==6688== suppressed: 0 bytes in 0 blocks
==6688== Rerun with --leak-check=full to see details of leaked memory
==6688==
==6688== For lists of detected and suppressed errors, rerun with: -s
==6688== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
What I can't really understand is that I have used the same construct other programs and it that cases all worked fine.
So , what happens in the previous case ??
You pass an uninitialized pointer to your function:
int main(void) {
struct node ** head;
init_list(head) ;
As a result, head does not contain a valid address and dereferencing it in *head = ... causes your crash. It is not related to mallocat all.
That is not the way how this function should be used. You cannot pass the new pointer to the caller this way.
Try this instead:
int main(void) {
struct node *head = NULL;
init_list(&head) ;
I using GNU MP to great an array of multi-precision integers. If I initialize, use and clean, all the main function, it works perfect, with no error. I've tried to create auxiliary functions to manage repetitive jobs: initialize an array and clean an array. Unfortunately it did not end up well, I'm getting a memory leak. I made a small example bellow reproducing the error behaviour:
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
mpz_t *start(int n)
{
mpz_t *c = malloc(n*sizeof(mpz_t));
for (int i=0; i<n; i++) mpz_init (c[i]);
for (int i=0; i<n; i++) mpz_set_ui (c[i], 1);
}
void finish(mpz_t *x, int n)
{
for (int i=0; i<n; i++) mpz_clear (x[i]);
free(x);
}
int main(int argc, char *argv[])
{
int n = 10;
mpz_t *c = start(n);
finish(c, n);
return 0;
}
The result of running the program is:
Segmentation fault (core dumped)
Bellow follows Valgrind analysis log:
==18807== Memcheck, a memory error detector
==18807== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==18807== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==18807== Command: ./test
==18807==
==18807== Invalid read of size 4
==18807== at 0x4887C60: __gmpz_clear (in /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.2)
==18807== by 0x10928B: finish (test.c:14)
==18807== by 0x1092DF: main (test.c:22)
==18807== Address 0xa is not stack'd, malloc'd or (recently) free'd
==18807==
==18807==
==18807== Process terminating with default action of signal 11 (SIGSEGV)
==18807== Access not within mapped region at address 0xA
==18807== at 0x4887C60: __gmpz_clear (in /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.2)
==18807== by 0x10928B: finish (test.c:14)
==18807== by 0x1092DF: main (test.c:22)
==18807== If you believe this happened as a result of a stack
==18807== overflow in your program's main thread (unlikely but
==18807== possible), you can try to increase the size of the
==18807== main thread stack using the --main-stacksize= flag.
==18807== The main thread stack size used in this run was 8388608.
==18807==
==18807== HEAP SUMMARY:
==18807== in use at exit: 240 bytes in 11 blocks
==18807== total heap usage: 11 allocs, 0 frees, 240 bytes allocated
==18807==
==18807== 240 (160 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==18807== at 0x483A7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==18807== by 0x1091E8: start (test.c:7)
==18807== by 0x1092CA: main (test.c:21)
==18807==
==18807== LEAK SUMMARY:
==18807== definitely lost: 160 bytes in 1 blocks
==18807== indirectly lost: 80 bytes in 10 blocks
==18807== possibly lost: 0 bytes in 0 blocks
==18807== still reachable: 0 bytes in 0 blocks
==18807== suppressed: 0 bytes in 0 blocks
==18807==
==18807== For lists of detected and suppressed errors, rerun with: -s
==18807== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
What is wrong? How to fix it?
Flowing off the end of a value-returning function (except main) without a return statement is undefined behavior.
In your example, mpz_t *start(int n) doesn't actually return anything, which leads to UB.
I am trying to implement the function int *cpy_array(int v[], int size), which copies the array in another and returns the new array as pointer. I also have to watch out for error cases and use dynamic memory.
Ok i know that malloc returns 0 when there is nout enough memory available. I was wondering if there might be any other possible errors as well which I missed out. Then I have to implement free() in the case of succsess as well as in error case.
I tried to implement something like:
if (!w[i]) {
for (k = 0; k < i; ++k)
free(w[k]);
return 0;
}
But there was always an error with this.
In file included from hot.c:2:
C:/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/i686-w64-mingw32/include/stdlib.h:502:27: note: expected 'void *' but argument is of type 'int'"
void __cdecl free(void *_Memory);
And I am not sure why to free() the new array or should the old array be freed? I tried to free it with pointer in my function, but didnt work either and dont think it should be in the main?
Here is the original code:
int *cpy_array(int v[], int size);
int main(void)
{
int size;
size = 4;
int myArray[4] = {1234};
if (*cpy_array(myArray, size) == 0)
{
printf("No memory available.");
}else{
printf("The new Array: %i", *cpy_array(myArray, size));
}
return 0;
}
int *cpy_array(int v[], int size)
{
int i;
int *a = malloc(size * sizeof(int));
if(*a == 0)
return 0;
for (i = 0; i < size; i++)
{
a[i] = v[i];
}
return a;
}
In your first code snippet, you incorrectly deallocated the array of integers w. You can't free single integers in that array, but what you need to do is simply type in:
free(w);
That will free the entire array.
You can also see from the text of the error - note: expected 'void *' but argument is of type 'int'" void __cdecl free(void *_Memory), that the program expected a pointer to the array and not an integer.
You can't free the old array, because it's statically created and the memory for it allocated at the start of the program and it will be freed at the end of the function in which it was defined by the program itself, so you don't need to worry about that. Whereas it's your job to free the dynamically created arrays such as the one you created through the cpy_array(int v[], int size) function.
More on the difference between static and dynamic allocation, you can look up here:
Difference between static memory allocation and dynamic memory allocation
This part of code, wouldn't proparly print the array (you will just print the first number of the array), and also you are calling the function twice, which is excessive and should be done only once for the same array.
if (*cpy_array(myArray, size) == 0)
{
printf("No memory available.");
}else{
printf("The new Array: %i", *cpy_array(myArray, size));
}
You could easify fix these problems by defining a pointer which could store the return value of the function, so you don't have to call it twice and then to correctly print the array use a for loop:
int * copiedArray = cpy_array(myArray, size);
if (copiedArray == NULL)
{
printf("No memory available.");
}else{
printf("The new Array: ");
for (int i = 0; i < size; i++)
printf("%i ", copiedArray[i]);
}
I noticed that you are checking whether a pointer is pointing to something or not incorrectly. Once in main:
if (*cpy_array(myArray, size) == 0)
And once in the cpy_array(int v[], int size) function:
if(*a == 0)
This will not work because you are dereferencing the pointer and checking whether the value to which it is pointing is zero. What you want to do is check the value of the pointer itself. If that is NULL then the allocation didn't work:
if (cpy_array(myArray, size) == NULL)
and
if(a == NULL)
You should use NULL instead of zero because you are explicitly stating that you are checking a value of a pointer, and NULL may not be equal to zero on every machine.
More on that topic here:
What is the difference between NULL, '\0' and 0
To detect you problems concerning the memory use valgrind, If I do that gives :
==10947== Memcheck, a memory error detector
==10947== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==10947== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==10947== Command: ./a.out
==10947==
==10947== Conditional jump or move depends on uninitialised value(s)
==10947== at 0x10548: cpy_array (c.c:25)
==10947== by 0x104B3: main (c.c:11)
==10947==
==10947== Invalid read of size 4
==10947== at 0x104B8: main (c.c:11)
==10947== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==10947==
==10947==
==10947== Process terminating with default action of signal 11 (SIGSEGV)
==10947== Access not within mapped region at address 0x0
==10947== at 0x104B8: main (c.c:11)
==10947== If you believe this happened as a result of a stack
==10947== overflow in your program's main thread (unlikely but
==10947== possible), you can try to increase the size of the
==10947== main thread stack using the --main-stacksize= flag.
==10947== The main thread stack size used in this run was 8388608.
==10947==
==10947== HEAP SUMMARY:
==10947== in use at exit: 16 bytes in 1 blocks
==10947== total heap usage: 1 allocs, 0 frees, 16 bytes allocated
==10947==
==10947== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1
==10947== at 0x4847568: malloc (vg_replace_malloc.c:299)
==10947== by 0x10533: cpy_array (c.c:24)
==10947== by 0x104B3: main (c.c:11)
==10947==
==10947== LEAK SUMMARY:
==10947== definitely lost: 16 bytes in 1 blocks
==10947== indirectly lost: 0 bytes in 0 blocks
==10947== possibly lost: 0 bytes in 0 blocks
==10947== still reachable: 0 bytes in 0 blocks
==10947== suppressed: 0 bytes in 0 blocks
==10947==
==10947== For counts of detected and suppressed errors, rerun with: -v
==10947== Use --track-origins=yes to see where uninitialised values come from
==10947== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 6 from 3)
The "Conditional jump or move depends on uninitialised value(s)" comes from the *a in if(*a == 0) and "Invalid read of size 4 ..." because you dereference 0 because of the return 0;
after changing if(*a == 0) to if(a == 0) to solve the two previous problems that condition is (a priori) false and _valgrind says :
==11116== Memcheck, a memory error detector
==11116== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==11116== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==11116== Command: ./a.out
==11116==
Mein neuer Array enthaelt folgende Zeichen: 1==11116==
==11116== HEAP SUMMARY:
==11116== in use at exit: 32 bytes in 2 blocks
==11116== total heap usage: 3 allocs, 1 frees, 1,056 bytes allocated
==11116==
==11116== 16 bytes in 1 blocks are definitely lost in loss record 1 of 2
==11116== at 0x4847568: malloc (vg_replace_malloc.c:299)
==11116== by 0x10523: cpy_array (c.c:24)
==11116== by 0x104A3: main (c.c:11)
==11116==
==11116== 16 bytes in 1 blocks are definitely lost in loss record 2 of 2
==11116== at 0x4847568: malloc (vg_replace_malloc.c:299)
==11116== by 0x10523: cpy_array (c.c:24)
==11116== by 0x104CF: main (c.c:15)
==11116==
==11116== LEAK SUMMARY:
==11116== definitely lost: 32 bytes in 2 blocks
==11116== indirectly lost: 0 bytes in 0 blocks
==11116== possibly lost: 0 bytes in 0 blocks
==11116== still reachable: 0 bytes in 0 blocks
==11116== suppressed: 0 bytes in 0 blocks
==11116==
==11116== For counts of detected and suppressed errors, rerun with: -v
==11116== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 3)
so yes you have memory leaks because you lost 2 times the allocation return by cpy_array
you need to have something like :
int * v = cpy_array(myArray, size);
if (*v == 0)
{
printf("Speicher kann nicht freigegeben werden.");
}else{
printf("Mein neuer Array enthaelt folgende Zeichen: %i",
*v);
}
free(v);
Doing that correction valgrind signals nothing :
valgrind --leak-check=full ./a.out
==11224== Memcheck, a memory error detector
==11224== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==11224== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==11224== Command: ./a.out
==11224==
Mein neuer Array enthaelt folgende Zeichen: 1==11224==
==11224== HEAP SUMMARY:
==11224== in use at exit: 0 bytes in 0 blocks
==11224== total heap usage: 2 allocs, 2 frees, 1,040 bytes allocated
==11224==
==11224== All heap blocks were freed -- no leaks are possible
==11224==
==11224== For counts of detected and suppressed errors, rerun with: -v
==11224== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
I encourage you to
compile with all warning detection, like gcc -g -Wall -pedantic ...
when you have problem use valgrind and/or debugger
even you do not see a problem run anyway under valgrind, so problems can be hidden
this is not the correct way to initialize the array
int myArray[4] = {1234};
write
int myArray[4] = { 1,2,3,4 };
or simply
int myArray[] = { 1,2,3,4 };
Calling the function cpy_array .. when you write
if (*cpy_array(myArray, size) == 0)
Is not correct, why? because what if the function returns NULL, then you are dereferencing NULL
In your function cpy_array you are dereferencing a, that is not correct, instead compare the pointer
if ( a == NULL)
and use the standard constant NULL for a null pointer instead of 0 since it may not be 0 on all platforms.
Context
I want to use Intel's MKL library to create an array and then do various stuff with it. However, it segfaults on mkl_malloc.
Problem
I am trying to run the following program. On running it, I get a segfault at the specified line. The problem is with mkl_malloc. What can I do to fix this? What is going on?
#include "mkl_types.h"
#include "mkl_spblas.h"
#include <stddef.h> // For NULL
#include <stdio.h>
// Find reason for segfault
int main() {
MKL_INT m=2000, k=1000;
double *A_dense;
// Allocate memory to matrix
A_dense = (double *)mkl_malloc( m*k*sizeof( double ), 128); // Fails here.
if (A_dense == NULL) {
printf("ERROR: Can't allocate memory for matrices. Aborting... \n\n");
mkl_free(A_dense);
return 1;
}
mkl_free(A_dense);
return 0;
}
Compile with:
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/intel/mkl/lib/intel65
$ gcc -m64 -I/opt/intel/mkl/include -L/opt/intel/mkl/lib/intel64 -lmkl_rt -lpthread -lm test_alloc.c -o test
test
test_alloc.c: In function 'main':
test_alloc.c:13:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
The output I get with valgrind is thus:
$ valgrind --leak-check=full ./test
==69680== Memcheck, a memory error detector
==69680== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==69680== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==69680== Command: ./test
==69680==
==69680== Invalid read of size 8
==69680== at 0x868D1D1: MKL_MALLOC (in /opt/intel/composerxe-2011.5.220/mkl/lib/intel64/libmkl_intel_lp64.so)
==69680== by 0x4F56B1C: MKL_MALLOC (in /opt/intel/composerxe-2011.5.220/mkl/lib/intel64/libmkl_rt.so)
==69680== by 0x4006B0: main (in /export/home/myuser/devel/part_distances/lib/test)
==69680== Address 0xf42400 is not stack'd, malloc'd or (recently) free'd
==69680==
==69680==
==69680== Process terminating with default action of signal 11 (SIGSEGV)
==69680== Access not within mapped region at address 0xF42400
==69680== at 0x868D1D1: MKL_MALLOC (in /opt/intel/composerxe-2011.5.220/mkl/lib/intel64/libmkl_intel_lp64.so)
==69680== by 0x4F56B1C: MKL_MALLOC (in /opt/intel/composerxe-2011.5.220/mkl/lib/intel64/libmkl_rt.so)
==69680== by 0x4006B0: main (in /export/home/myuser/devel/part_distances/lib/test)
==69680== If you believe this happened as a result of a stack
==69680== overflow in your program's main thread (unlikely but
==69680== possible), you can try to increase the size of the
==69680== main thread stack using the --main-stacksize= flag.
==69680== The main thread stack size used in this run was 8388608.
==69680==
==69680== HEAP SUMMARY:
==69680== in use at exit: 4,603 bytes in 19 blocks
==69680== total heap usage: 19 allocs, 0 frees, 4,603 bytes allocated
==69680==
==69680== LEAK SUMMARY:
==69680== definitely lost: 0 bytes in 0 blocks
==69680== indirectly lost: 0 bytes in 0 blocks
==69680== possibly lost: 0 bytes in 0 blocks
==69680== still reachable: 4,603 bytes in 19 blocks
==69680== suppressed: 0 bytes in 0 blocks
==69680== Reachable blocks (those to which a pointer was found) are not shown.
==69680== To see them, rerun with: --leak-check=full --show-reachable=yes
==69680==
==69680== For counts of detected and suppressed errors, rerun with: -v
==69680== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)
Segmentation fault
Alternatives
I am using Single Dynamic Linking (SDL) because I can't get static linking to work.
The documentation suggests including mkl.h.
So change:
#include "mkl_types.h"
#include "mkl_spblas.h"
to
#include "mkl.h"
You get a scary warning:
test_alloc.c:13:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
I assume this is from the code line that does the call to mkl_malloc():
A_dense = (double *)mkl_malloc( m*k*sizeof( double ), 128);
This implies that you're missing the header, since otherwise there would be no "integer" here.
Also, you should never cast a void * like that in C, as I might have mentioned before on this site.
I am running breadth first search and bellman ford algorithms on large sets of graph data in the format (u, v, weight) from an input file.
I initialize in the breadth first search, that all vertex's should be marked 0 for unvisited.
Later in the program, since I am calling BFS after every time I add an edge, rather than at the end of the program all together (it's part of a research project on bellman ford and BFS even though it doesn't quite make sense) I reinitialize the vertex array to unvisited.
However, I am getting a segmentation fault when I run larger sets it seems when I reinitialize the vertex array. I make the assumption about larger sets because I have a few smaller sets of test data going from 8 vertices, to 10, then at 100 and larger it fails.
Here is how I initialize at the very beginning of the program:
for(i=0;i<numberVertices;i++)
{
vertexArray[i].v = i+1;
vertexArray[i].adj = NULL;
vertexArray[i].marked = 0;
if(i==0)
{
vertexArray[i].weight = 0;
}
else{
vertexArray[i].weight = 10000;
}
}
And here is how I reinitialize at the end of a while loop set to break when it reaches the end of the file directly after BFS finishes:
BFS(q, u)
for(i=0;i<numberVertices;i++)
{
vertexArray[i].marked = 0;
}
Like I said it seems to work for smaller data sets, but I do not understand why when I reinitialize it seems to seg fault.
Please let me know your thoughts and suggestions!
Valgrind Output On An Example Case:
==6634== Memcheck, a memory error detector
==6634== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==6634== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==6634== Command: ./a.out 100S.txt
==6634==
==6634== Conditional jump or move depends on uninitialised value(s)
==6634== at 0x400C13: checkonqueue (bfs.c:160)
==6634== by 0x400B7F: BFS (bfs.c:142)
==6634== by 0x40094A: main (bfs.c:103)
==6634==
==6634== Invalid write of size 4
==6634== at 0x40096E: main (bfs.c:107)
==6634== Address 0x4d00000041 is not stack'd, malloc'd or (recently) free'd
==6634==
==6634==
==6634== Process terminating with default action of signal 11 (SIGSEGV)
==6634== Access not within mapped region at address 0x4D00000041
==6634== at 0x40096E: main (bfs.c:107)
==6634== If you believe this happened as a result of a stack
==6634== overflow in your program's main thread (unlikely but
==6634== possible), you can try to increase the size of the
==6634== main thread stack using the --main-stacksize= flag.
==6634== The main thread stack size used in this run was 10485760.
==6634==
==6634== HEAP SUMMARY:
==6634== in use at exit: 3,448 bytes in 181 blocks
==6634== total heap usage: 181 allocs, 0 frees, 3,448 bytes allocated
==6634==
==6634== LEAK SUMMARY:
==6634== definitely lost: 0 bytes in 0 blocks
==6634== indirectly lost: 0 bytes in 0 blocks
==6634== possibly lost: 0 bytes in 0 blocks
==6634== still reachable: 3,448 bytes in 181 blocks
==6634== suppressed: 0 bytes in 0 blocks
==6634== Reachable blocks (those to which a pointer was found) are not shown.
==6634== To see them, rerun with: --leak-check=full --show-reachable=yes
==6634==
==6634== For counts of detected and suppressed errors, rerun with: -v
==6634== Use --track-origins=yes to see where uninitialised values come from
==6634== ERROR SUMMARY: 16120 errors from 2 contexts (suppressed: 6 from 6)
Segmentation fault (core dumped)
check on queue function:
int checkonqueue(int * q, int value)
{
int i = 0;
for(i=0;i < max;i++)
{
if(q[i] == value)
{
return 1;
}
}
return 0;
}
line 160 is the line of the if condition