last day (14 days later) » 

7:26 PM
-2
Q: Segmentation fault in a program that reverses a dynamically allocated array

R.miaI was doing a test and the online test engine showing segmentation error, which is confusing because with no further details, and I checked the pointer no NULL and they work pretty fine, but don't how array here works. Because when debugging, everything is fine, until I try to cout/print out the ...

 
Here's a tip: what's the size of a[len];? No, it's not what you entered from the terminal. The array gets created before len is initialized.
 
Please refrain asking questions about online code judge engines here. It's very unlikely that anyone could tell you where you failed from their test cases, as these aren't disclosed usually. Even if what you tested was running at your local environment, you may have missed to test some edge cases which are applied in the online challenge. Be creative and try to find them. Additionally there's probably no value for such questions in the long term, other than cheating the online contest, and nothing is learned.
The right tool to solve such problems is your debugger. You should step through your code line-by-line before asking on Stack Overflow. For more help, please read How to debug small programs (by Eric Lippert). At a minimum, you should [edit] your question to include a Minimal, Complete, and Verifiable example that reproduces your problem, along with the observations you made in the debugger.
 
@πάνταῥεῖ Let's be fair to OP, he can run this literally a thousand times without getting a crash. I think if he could construct a minimal complete and verifiable example, he wouldn't be asking this question in the first place. Crashing his program requires automated input of large number of integers.
C'mon, this is not an offtopic! How the heck do you expect people to learn if you close honest questions with decent amount of effort behind them?
 
@dasblinkenlight where do you see decent amount of effort?
 
@Slava The effort is obvious In OP's code. As far as I can tell, OP hasn't copy-pasted this from some place, he wrote this thing himself, and he did a good job, apart from a not so obvious mistake.
@Slava Why do you think OP has not run this in a debugger? I'm sure he did, and it ran just fine. It's just that the thought of entering a million numbers has never crossed his mind :-)
 
7:26 PM
Hi, thanks guys for spending time here! I really got helped a lot. I was just keep coding in computer and can't really find what's wrong, because when I debug, there is no certain clue, I know there is something wrong around the array initialization. and the debugger only tells me the code crushes around a.
 
@dasblinkenlight how to deal with variable size arrays is written probably in every basic textbook of C++
 
I don't have a coding friend who can help me on coding, and the only help I know is from stackoverflow, so I posted the question here, hope I can discuss with somebody. I think discussion will motivate me more than just coding in front an computer .
 
@R.mia for your code it is pretty simple - never use uninitialized variables. It is strange that g++ does not report warning in this case
 
and a conversation can reduce the time of useless searching, because I know there is somebody with a lot of experience on stackoverflow. I am here to learn .
 
@R.mia discussion is good, but if you want to program on a language you better read textbook first.
 
7:26 PM
I didn't see any error with g++, I use visual studio in windows. not linux. by the way the online test engine don't show any error and they only show it's a segmentation error.
 
@Slava Judging from OP's comments, he is not up to operator new in his book yet. This assignment is challenging at his level, but the problem in his code is not in the area of the main challenge of the task that he is solving. Anyway, I don't think the community has treated this question fairly.
 
Why use std::swap() but not std::reverse()? They are in the same algorithm library.
 
@RemyLebeau Hi, thanks for your remind, I just didn't even think about vector. If use vector, actually save a lot of trouble compared with array.
 
@R.mia I didn't say anything about std::vector. I mentioned the std::reverse() algorithm, which can be used with anything that supports bidirectional iteration, which includes static arrays.
 
@RemyLebeau yes, I am using the reverse function in std, but it has to be a vector, dynamic array can't process begin() and end() function, which is needed in the reverse() algorithm.
 
7:26 PM
@R.mia clearly you don't understand how iterators actually work. You can use std::reverse() with arrays, both static and dynamic. For example you can use std::reverse(a, a+len) with your dynamic array.
 
@RemyLebeau Have you tried? If you tried then, you have the right to say everything about the question but not about me..... I was running them till very late. aNd I am telling you that reverse(a, a+len) is not working in dynamic array. Show me your prove that it's working......
 
@R.mia: here you go, proof that std::reverse(a, a+len) works with both dynamic and static arrays: ideone.com/ErZbM1
 
Thank you, I don't know what's the error, but right now, it seems pretty fine. thanks.
 

last day (14 days later) »