1

As always appreciate your time and help! Not sure what's going on here, but it's probably something obvious so I thought I'd have someone else take a look.

This is my code. Right now run finds the address of a node with a specified name as a string. Then returns the address of this node object. Sometimes it works fine (mostly) but other runs give me a segmentation fault. Can anyone see why?

class ManipulateArray
{
    public:
    Node* formArray();
    Node* findMemAddress(string);
    private:
    GetFileInfo g;
};

Node* ManipulateArray::formArray()
{
    Node *list = new Node[48];
    string null = "*";

    for(int i = 0; i<48; i++)
    {
        for(int j = 0; j<6; j++)
        {   
        string info = g.returnInfo(i,j);

            switch(j)
                {
            case 0:
            list[i].setNodeName(info);
            break;
            case 1:
            if(info.compare(null) == 0)
            {list[i].getAttachedNode(j) = 0;}
            break;
                }
        }
    }   

    return list;
}

Node* ManipulateArray::findMemAddress(string nodeName)
{
    Node* memoryAddress = 0;
    Node* temp;
    for(int i = 0; i<48; i++)
    {
        string name = temp[i].getNodeName();
        temp = formArray();

        if(name.compare(nodeName) == 0)
        {
            memoryAddress = &temp[i];
            break;
        }
    }
    return memoryAddress;
}


int main()
{
    ManipulateArray z;
    //Node *start;
    //Node *finish;

    Node* start;
    start = z.findMemAddress("F1");
    cout << start->getNodeName();
}
Fraser
  • 74,704
  • 20
  • 238
  • 215
TheNodeCommode
  • 93
  • 1
  • 2
  • 8
  • 1
    This is your problem: http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope. The reason it's inconsistent is that it causes undefined behaviour. – chris Nov 10 '12 at 00:37
  • can you determine on which line the seg fault happens? Do you have access to a debugger? Have you tried commenting in/out lines of code to see what seems to work/what crashes? – Doug T. Nov 10 '12 at 00:38
  • Yes I have. The problem is in the findMemAddress() and caused when I call it in main. – TheNodeCommode Nov 10 '12 at 00:40
  • 1
    @TheNodeCommode, Yes, the problem is literally the last line (`}`) of `findMemAddress`. The reason is explained in the link I posted. – chris Nov 10 '12 at 00:41
  • 2
    Isn't something missing in the code? the line `string name = temp[i].getNodeName();` is accessing 'temp' which is an uninitialized pointer. – Nik Bougalis Nov 10 '12 at 00:42
  • 2
    @chris : Not quite... `temp` is uninitialized. Returning a pointer from a function is not a problem, returning _the address of a local with automatic storage_ is. – ildjarn Nov 10 '12 at 00:43
  • @both, Ah, good one. I saw the line and presumed it was locally allocated. I only saw the absence of a `new`. – chris Nov 10 '12 at 00:44
  • @chris - Why cannot people format code – Ed Heal Nov 10 '12 at 00:49
  • Ah, so temp needs to be allocated onto the heap instead of the stack? – TheNodeCommode Nov 10 '12 at 00:56
  • Have you tried the debugger to find out what line this occurs? It might not pinpoint the exact line but will give the indication as to the mysterious variable that involved, – Ed Heal Nov 10 '12 at 00:45

1 Answers1

1

'temp' in function Node* ManipulateArray::findMemAddress(string nodeName) haven't been initialized. And in function int main() you should check the value of 'start' before dereference it.Hope it works!

prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42
  • Yes I was just doing that to test output. I appreciate all the help guys. Question though, and I feel like im blowing past the point. Why does temp need to be on the heap? The contents of temp dont change so why would the outcome be different each run? Because it is choosing different memory locations each time? – TheNodeCommode Nov 10 '12 at 01:00
  • [Explanation for heap.](http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – prehistoricpenguin Nov 10 '12 at 01:11
  • Please paste you complete code on sites like [pastebin](http://pastebin.com/) and give your link. – prehistoricpenguin Nov 10 '12 at 01:13