Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I challenge you to find what the heck is causing my program to break, I've been trying to find a reasoning behind this but I haven't been able to find even the slightest bit.
My program is supposed to read values from a file and depending on what it reads create 2 matrix: first value on the file is rows and second one cols. (Then it's supposed to do some algorithms but that works just fine, it's failing before that)
So here's my problem: I keep getting the following error message when running the program
only
when the rows value is 30. I haven't been able to find a single value that makes this fail other than 30. I want to fix this but I can't find what's causing the program to fail to begin with, I don't know what else to try. I debugged it and the program fails when creating the first matrix after reading all the info from the file.
potter: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)
The funniest thing is the program was working just fine before adding the last function. I must have changed something without noticing and broken the program (I don't have any older versions and removing the function doesn't help)
Here's a link to the hole code and test files if anyone is interested: https://drive.google.com/open?id=1tcKXainj021fwjAAGN3cNdsU50p2Neou
Edit: The program fails in the following part of the code
M=new int * [n+1]
if(M){
for(int i=0; i<=n;i++){
M[i]=new int [T+1];
if(!M[i]){
cerr<<"Error";
exit(-1);
for(int i=0;i<n+1;i++)
for(int j=0;j<T+1;j++)
M[i][j]=-1;
}else{
cerr<<"Error";
exit(-1);
Concretely when trying to do M[i]=new int [T+1]; when i=10 only if n=30
Solved
The problem was a loop going outside of allocated memory beforehand corrupting it and causing the program to fail later.
decisions=new int [n];
for(int i=0;i<=n;i++)
decisions[i]=0;
Honestly I don't know how the program didn't fail more having done something like this. (This was easily solved by changing n to n+1 in decisions=new int [n+1];)
–
–
–
–
–
It's very probable that you've done something that corrupted the malloc data structure. This could be by reusing a freed pointer, overrunning the data you allocated (and thereby mashing the little header malloc uses) or accidentally freeing a pointer that doesn't point to the head of malloc-ed data.
There is a lovely tool called valgrind which is very helpful for this sort of thing.
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.