Error in file operation in C
Doubt : Line 441 in the code: fp1=fopen("t0.dat","w");
it is causing the following error :
a.out: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted
The link to my code is:
http://pastebin.ca/2389138
Re: Error in file operation in C
Well, did you look at your post before sending it?
Did you look at it after you had sent it?
Can you read/understand the text in this post?
I don't!
Please, begin with the reading the Announsments (section Information on posting)
and possible tags
Then try to follow the announced policy (including the way you post code snippets, attachments and so on)
Re: Error in file operation in C
Code:
fp1=fopen("t0.dat","w");
How is fp1 defined? Please post a complete working program that demonstrates the problem - formatted and code tags used.
Re: Error in file operation in C
Quote:
Originally Posted by
D1982
Doubt : Line 441 in the code: fp1=fopen("t0.dat","w");
it is causing the following error :
Code:
if((NB=(int **) calloc (1,sizeof(int)))==NULL)
Do you see an inconsistency with what is in red?
Next:
Code:
total_nodes=N1+N2;
l=total_nodes;
// Total number of mesh points is known at this stage therefore data can be assigned to the mesh points
int ind[l],n[l],s[l],e[l],w[l],refinement_status[l]; //????
What 'C' compiler are you using that allows arrays to be declared with a variable number of items? It certainly isn't ANSI C. If it's C99 or some extension, then why would you do this:
Code:
if((*(NB)=(int *) malloc (No_of_NB_Pts*sizeof(int)))==NULL)
{
printf("Insufficient memory. Program terminated\n");
return(0);
}
Why not just do the code below and save yourself the trouble of dynamically allocating memory.
Code:
int NB[No_of_NB_Pts];
The other thing with that code is that it can't be ANSI 'C', since you're declaring variables after an executable statement. That is not allowed in ANSI 'C'.
So it isn't C++ you are writing in, since variable arrays are not allowed, and it isn't K&R ANSI C since variable arrays are not allowed, and you can't declare variables after executable statements. More than likely, your using extensions, or you have a true C99 compiler.
So already we have no idea what you're using -- why not tell us?
Regards,
Paul McKenzie
Re: Error in file operation in C
I could identify the exact problem, it is because of using realloc . I am pasting the relevant part of C program here. I need to use a dynamic array (NB) in my program, when I am using realloc for the first time (line 37 of the code) then it is giving no error but when I am using realloc for the second time (line 48 of the code) it is giving the following error :
a.out: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted
//-------------------------- the C code which I am using is the following-------------------------------
//---------------Standard C librabries are included here-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//-------------------------------------------------------------------------------------
main ()
{
int N=5;
int **NB;
int No_of_NB_Pts;
int k,p0,p1;
No_of_NB_Pts=N;
if((NB=(int **) calloc (1,sizeof(int)))==NULL)
{
printf("Insufficient memory. Program terminated\n");
return(0);
}
if((*(NB)=(int *) malloc (No_of_NB_Pts*sizeof(int)))==NULL)
{
printf("Insufficient memory. Program terminated\n");
return(0);
}
//--------------------------------------- Algorithm----------------------------------------
// The following steps store some values in NB
for(k=0;k<No_of_NB_Pts;k++)
{
NB[0][k]=k+2;
//printf("\n k, NB = %d, %d \t", k, NB[0][k]);
}
// The following steps increase the length of NB and then store the value for the new entry
*NB=(int *)realloc(*NB,sizeof(int)*(No_of_NB_Pts++));
NB[0][No_of_NB_Pts-1]=No_of_NB_Pts+3; // assigning data to new entry
/* for(p0=0;p0<No_of_NB_Pts;p0++)
{
printf(" %d \t", NB[0][p0]);
}*/
// uncommenting the following two lines gives error
*NB=(int *)realloc(*NB,sizeof(int)*(No_of_NB_Pts++));
NB[0][No_of_NB_Pts-1]=No_of_NB_Pts+3; // assigning data to new entry
/* for(p0=0;p0<No_of_NB_Pts;p0++)
{
printf(" %d \t", NB[0][p0]);
}*/
printf("\n end of program \n");
}
Re: Error in file operation in C
Quote:
Originally Posted by
D1982
I could identify the exact problem, it is because of using realloc
More than likely, you caused the problem by a buffer overrun or some sort of memory corruption issue.
Second, you still didn't fix this glaring problem:
Code:
if((NB=(int **) calloc (1,sizeof(int)))==NULL)
Do you understand what's wrong with this line? I already mentioned this in my first post, but you still didn't fix this line. If this line is wrong, then the foundation for the rest of your code crumbles.
Code:
if(( NB=(int **)calloc (1,sizeof(int*)))==NULL)
Do you now see the difference? That one '*' could be the reason you're having all of these problems.
Then you have this:
Code:
*NB=(int *)realloc(*NB,sizeof(int)*(No_of_NB_Pts++));
So what is the value of No_of_NB_Pts supposed to be for the call to realloc? Can you tell by that code in red? Is it supposed to be No_of_NB_Pts+1, or is it No_of_NB_Pts, and then 1 is added after realloc is called? Confusing, isn't it?
You should make the code clear -- do not do ++ in the middle of a function call sequence like that. More than likely, the code is ambiguous, and even if not, it's confusing as heck. Not only that, you've messed up No_of_NB_Pts value if realloc() fails.
For example, the code samples below are clear and to the point:
Code:
*NB=(int *)realloc(*NB,sizeof(int)*(No_of_NB_Pts));
++No_of_NB_Pts;
or
Code:
++No_of_NB_Pts;
*NB=(int *)realloc(*NB,sizeof(int)*(No_of_NB_Pts));
Whichever of the two above, use it. Just separate into two lines of code so that everyone can see what is going on without having to take out the ANSI/ISO specs to figure out what the result could be.
Last, please use code tags when posting code. The code you're posting is practically unreadable without code tags.
Regards,
Paul McKenzie
Re: Error in file operation in C
Dear Paul McKenzie,
Thanks a lot for clarifying things. Doing "++No_of_NB_Pts" before realloc has solved the problem. Could you please suggest me which data structure in C should be used for adaptive mesh refinement ?
Best Regards,
D1982