Segmentation fault when calling a function?

Up vote 1 down vote favorite share g+ share fb share tw.

Segmentation fault when calling the Update_Multiplier and gdb debugger shows these: Program received signal SIGSEGV, Segmentation fault. 0x080b74e8 in Update_Multiplier() () double upperbound = 116325; double objective = 1.1707e+07; int main() { Update_Multiplier(); } void Update_Multiplier() { cout = 0) { stepsizeij=(tuning*(UpperBound-Objective))/sqrt((graij*graij)*(grbij*grbij)); LRADumij=LRAij+stepsizeij*graij; LRAij=LRADumij; LRBDumij=LRBij+stepsizeij*grbij; LRBij=LRBDumij; } } } } c++ segmentation-fault link|improve this question edited Jan 22 '10 at 14:58jamessan9,037922 asked Jan 22 '10 at 14:50Emrah Ayanoglu61.

Hmmm, possibly 40MB of stack space required. What's the platform, and do you know how much memory is allocated for the stack? – Charles Bailey Jan 22 '10 at 14:53 I think he's programming one of those computers with 256-bit processors, ~200TB RAM that nobody else knows.

– Eduardo León Jan 22 '10 at 15:00 To be honest, it's pretty surprising that it even gets to segfault. I can't see declarations of cout, endl, noOfNodes, C, noOfCommodity, X, U, Y, Q, B, UpperBound, Objective, sqrt, LRA, LRB. In addition there's no declaration of Update_Multiplier in scope where it's called from main.

– Charles Bailey Jan 22 '10 at 15:04.

I see two suspicious things in your code. First, you are taking too much stack space (about ~40 MB). Second, you are starting the index of the array at 1, where it should be 0: for (int i=1; i.

Heh.. beat me to it. Although the large stack is a possibility, I think this is the most likely scenario. – Randolpho Jan 22 '10 at 14:57 The comments about the loops starting at 1 are also valid, but we don't know the value of noOfNodes.

If it's less than 1,000 then that part at least will be fine. But what if it's greater than 1,000? It looks like the function is just allocating 1,000 x 1,000 arrays with the assumption that noOfNodes will never be that large.

It should use the actual value of noOfNodes and allocate them dynamically from the heap. – Willis Blackburn Jan 22 '10 at 15:01 @Willis Blackburn: Even if noOfNodes is set to the exact number of items in the array, starting at 1 and using I – Willis Blackburn Jan 22 '10 at 15:04 Randolpho: That's why I said "If it's less than 1,000 ..." :-) – Willis Blackburn Jan 22 '10 at 15:05.

At a guess, you have a stack overflow! You cannot reliably create gigantic arrays on the stack. You need to create them dynamically or statically.

If this is uninitialized, it probably has junk data -- which may or may not explain the crash.

You need a stack of at least 40 megabytes to run this function because you're allocating five arrays of one million eight-byte doubles each. Change the function to allocate the double arrays from the heap using new.

1 Ya it is ~40 MB. I first mentioned it is ~5 MB in my post :) – AraK Jan 22 '10 at 14:59.

You should really give us the whole code, e.g. NoOfNodes is not defined anywhere. Just a stab in the dark: are you possibly overflowing C since your indices (i and j) go from 1 to noOfNodes?

First, what Neil said is true. Second, C and C++ arrays start from index zero. If you declare int a100; // 100 elements, from zeroth to ninety-ninth.

Then its elements are a0, a1 ... a99.

I can not see anything wrong with this code as given, BUT: You might have an off-by-one error if noOfNodes is 1000. Remember that Arrays are 0-indexed so you have to access indexes 0 - 999 instead of 1 - 1000 as you are doing.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions