CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2013
    Posts
    3

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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)
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error in file operation in C

    Quote Originally Posted by D1982 View Post
    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
    Last edited by Paul McKenzie; June 4th, 2013 at 07:13 PM.

  5. #5
    Join Date
    Jun 2013
    Posts
    3

    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");
    }

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error in file operation in C

    Quote Originally Posted by D1982 View Post
    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
    Last edited by Paul McKenzie; June 6th, 2013 at 12:14 PM.

  7. #7
    Join Date
    Jun 2013
    Posts
    3

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured