New to Valgrind, help interpreting?

Can someone help explain what Valgrind is telling me? I have a void function named smallSort2 that takes as parameters the addresses of three int variables and sorts the ints at those addresses in ascending order. Compiles fine but when I run the program I get a segmentation fault.

Message from Valgrind:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
==17383== Use of uninitialised value of size 8
==17383==    at 0x400971: smallSort2(int*, int*, int*) (smallSort2.cpp:26)
==17383==    by 0x4008F6: main (smallSort2.cpp:14)
==17383== 
==17383== 
==17383== Process terminating with default action of signal 11 (SIGSEGV)
==17383==  Bad permissions for mapped region at address 0x400ABD
==17383==    at 0x400971: smallSort2(int*, int*, int*) (smallSort2.cpp:26)
==17383==    by 0x4008F6: main (smallSort2.cpp:14)
==17383== 
==17383== HEAP SUMMARY:
==17383==     in use at exit: 0 bytes in 0 blocks
==17383==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==17383== 
==17383== All heap blocks were freed -- no leaks are possible
==17383== 
==17383== For counts of detected and suppressed errors, rerun with: -v
==17383== Use --track-origins=yes to see where uninitialised values come from
==17383== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 1 from 1)
Segmentation fault


Is the issue in my smallSort function and the error is occurring when main accesses it? Or is the error in main?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

void smallSort2(int*, int*, int*);

int main()
{
	int a = 14;
	int b = -90;
	int c = 2;

	smallSort2(&a, &b, &c);
	cout << a << ", " << b << ", " << c << endl;

	return 0;
}
void smallSort2 (int* a, int* b, int* c)
{
	int *ptr1;
	int *ptr2;
	int *ptr3;
	int temp;

	*ptr1 = *a;
	*ptr2 = *b;
	*ptr3 = *c;

	if (*a > *b)
	{
		temp = *a;
		*a = *b;
		*b = temp;
	}
	if (*a > *c)
	{
		temp = *a;
		*a = *c;
		*c = temp;
	}
	if (*b > *c)
	{
		temp = *b;
		*b = *c;
		*c = temp;
	}
}
Last edited on
1
2
3
4
5
6
7
8
	int *ptr1;
	int *ptr2;
	int *ptr3;
	int temp;

	*ptr1 = *a;
	*ptr2 = *b;
	*ptr3 = *c;


You make 3 pointers that with unspecified values (which is to say they point to arbitrary memory locations) and then you attempt to assign values to those arbitrary memory locations. Don't write to memory that you don't own.
Topic archived. No new replies allowed.