Segmentation Fault

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
50
51
52
53
54
55
56
#include <iostream>
#include <cstdlib>
using namespace std;

void move_rings (int n, int src, int dest, int other);
int testSign (int number);
int newNumber ();


int main (int argc, const char * argv[])
{
    int n; //Ring stack height
    
    cout << "Enter the height of the stack" << endl;
    cin >> n;
    
    testSign (n);
    
    move_rings(n, 1, 3, 2); //Move stack 1 to stack 3
    //system("PAUSE");
    return 0;
}

void move_rings (int n, int src, int dest, int other)
{
    if (n == 1) {
        cout << "Move from " << src << " to " << dest << endl;
    }
    else {
        move_rings(n-1, src, other, dest);
        cout << "Move from " << src << " to " << dest << endl;
        move_rings(n-1, other, dest, src);
    }
}

int testSign (int number)
{
    if (number >= 0) {
        return number;
    }
    else {
        cout << "The number of stacks must be POSITIVE" << endl;
        return newNumber();
        
    }
}

int newNumber ()
{
    int newStackNumber;
    
    cout << "Enter a POSITIVE number of stacks" << endl;
    cin >> newStackNumber;
    
    return newStackNumber;
}


Alright, I am just working my way through a C++ beginner's guide by a Mr. Overland. I was attempting to edit the code to check for +/- value of a number and if negative, request the individual to re-enter a new positive number. However, when I enter a negative number, and the program request a positive number in response, I get the following error:

sharedlibrary apply-load-rules all
Warning: the current language does not match this frame.
warning: Unable to restore previously selected frame.
No memory available to program now: unsafe to call malloc
warning: Unable to restore previously selected frame.
(gdb)


I am using xCode, most current version. Any guidance or help is appreciated. I believe the error is related to a memory error from segmentation fault research.

Thanks.
closed account (zb0S216C)
Since you don't explicitly allocate memory, the cause may fall down to the programs stack. When a variable is constructed, the variable is allocated on the stack, which makes the stack grow. It seems as though the system doesn't have the required memory to create n amount of variables, nor can it create a stack frame for each function when it's invoked.

When the malloc( ) warning was given, it indicated that the system may not have enough system memory to perform the allocation. malloc( ) is nervous about such a case so it warns you.

Wazzak
Last edited on
Your response has been very insightful. However, I want know, would I be able to bypass this warning/ problem by using pointers, in place of constructing new variables, and then returning them, etc?

Thanks again. :)
closed account (zb0S216C)
Dynamically allocating variables is a no-go since you're low on memory. As a possible solution, you could set each parameter of each function to a reference. References disallow copies of the passed argument. Instead, they refer to the address of the argument that was assigned to it. By eliminating the copy procedure, you effectively reduce the stack size, thus, reducing the system memory usage. This can be done with pointers also, but you have to explicitly pass the address of the argument by using the address-of operator. You see, when a parameter is declared in the same way as int argc, it creates copies of the argument it was given. For example, consider this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Function( int Param )
{
    Param = 100;
}

int main( )
{
    int A( 10 );
    std::cout << "Before: " << A << std::endl;

    Function( A );
    std::cout << "After: " << A << std::endl;

    return 0;
}

This code will not modify the content of A since A was copied during the invocation of Function( ). When the function was running, Param held the copy of A. When Param was assigned to the value 100, A's clone received the value of 100. int usually reserves 4 bytes of memory. When a copied is made, an additional 4 bytes are reserved on the stack for the copy. If we used references, we avoid the copy procedure altogether. This example demonstrates this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Function( int &Param )
{
    Param = 100;
}

int main( )
{
    int A( 10 );
    std::cout << "Before: " << A << std::endl;

    Function( A );
    std::cout << "After: " << A << std::endl;

    return 0;
}

This code is identical to the previous code, except for one minor detail: the reference parameter. When A was passed to Function( ), A was not copied, which means 4 byte less (copy).

The latter won't reduce the memory by a great deal, but it's definitely an improvement.

Wazzak
Last edited on
Topic archived. No new replies allowed.