Solved

why malloc() fail!

Posted on 1998-12-14
15
401 Views
Last Modified: 2010-04-15
My program is:

int      CreateMsg(MsgData)
char      **MsgData;
{
      short int       MsgLen,RefID,MsgType;
      int            RegNum=0;
      int            Len,i,CurLen=0;
      char            tmp;
      
      MsgType=0x0107;
      MsgLen=100;

      RefID=0x0501;

      *MsgData=(char *)realloc(*MsgData,MsgLen);
      if(!*MsgData)
      {
            fprintf(stdout,"\nOut of memory \n");
            return(0);
      }
         .
         .
         .

      return(MsgLen);
}

when run to the line of 'realloc' it always report:
'Memory fault(coredump)'

Help Me !Please!
0
Comment
Question by:wpy
15 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 10 total points
ID: 1255131
You are getting the error most likely because MsgData is uninitialised or the thing it points to has not been malloced - you cannot realloc a memory block that has not been malloced.

Cheers,

Raymond.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1255132
>> you cannot realloc a memory block that has not been malloced.
Partially true.  You can realloc() a NULL pointer.  Works just like malloc().
0
 

Author Comment

by:wpy
ID: 1255133
I'v  malloced it !
but it not work .Pelease Help me!
 
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 84

Expert Comment

by:ozo
ID: 1255134
How did you malloc it?  How do you call CreateMsg?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255135
please show the call to CreateMsg including the code for malloc

check to see if the char* pointer whose address you pass to CreateMsg hasn't alreday been freed.  Or that you aren't passing a NULL (instead of pointer to NULL).  I'd include a check that MsgData != NULL and *MsgData != NULL at the start of your function.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255136
please show the call to CreateMsg including the code for malloc

check to see if the char* pointer whose address you pass to CreateMsg hasn't alreday been freed.  Or that you aren't passing a NULL (instead of pointer to NULL).  I'd include a check that MsgData != NULL and *MsgData != NULL at the start of your function.
0
 

Author Comment

by:wpy
ID: 1255137
My program is:

    int CreateMsg(MsgData)
    char **MsgData;
    {
    short int MsgLen,RefID,MsgType;
    int RegNum=0;
    int Len,i,CurLen=0;
    char tmp;

    MsgType=0x0107;
    MsgLen=100;

    RefID=0x0501;

    *MsgData=(char *)realloc(*MsgData,MsgLen);
    if(!*MsgData)
    {
    fprintf(stdout,"\nOut of memory \n");
    return(0);
    }
             .
             .
             .

    return(MsgLen);
    }


Tthe call to CreateMsg is :

Msg=(char *)malloc(sizeof(char));
if(!Msg)
{
      printf("\nOut of memory");
      return(1);
}

MsgLen=CreateMsg(&Msg);

I need the CreateMsg return ' MsgLen' and the created 'Msg'  same time!
 
0
 
LVL 3

Expert Comment

by:elfie
ID: 1255138
> Msg=(char *)malloc(sizeof(char));

your sizeof(char) should bee sizeof(char*), because
*Msg is of type (char*), and not char.

Your problem is that you are trying to assign a pointer (normally 4 bytes long) to an allocated space of only 1 byte.
0
 
LVL 1

Expert Comment

by:szetoa
ID: 1255139
I have copied your code, added two statements, compiled
and run with MS VC++ on a PC running Windows NT and it
was just fine.  The code is enclosed below.  Are you sure
the core dumped happened at the realloc()?
========================================================
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

int CreateMsg( char **MsgData )
{
    short int MsgLen, RefID, MsgType;
    int RegNum=0;
    int CurLen=0;

    MsgType = 0x0107;
    MsgLen = 100;
    RefID = 0x0501;

    *MsgData = ( char * )realloc( *MsgData, MsgLen );

    if( ! *MsgData )
    {
        fprintf( stdout, "\nOut of memory\n" );
        return( 0 );
    }

    return( MsgLen );
}

main()
{
    char *Msg;
    int MsgLen;

    Msg = ( char * )malloc( sizeof( char ));

    if( ! Msg )
    {
        printf( "Out of memory\n" );
        return( 1 );
    }

    MsgLen = CreateMsg( & Msg );
    strcpy( Msg, "hello world" );
    printf( "%s\n", Msg );
}
==========================================

Good luck.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255140
sizeof(char) was fine.  Please ignore the comment above about sizeof(char) needing to be sizeof(char*).

0
 

Author Comment

by:wpy
ID: 1255141
Thank very much sir!  I'm so sorry  for my mistake that has bother you so much!
Last nigth ,I find the  error in the 'Msg' declaration ,which is :
char            Msg+NULL;
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255142
aha .. you declared Msg as a 'char' instead of a 'cahr*' .. is that correct?  It is odd that the compiler did not generate a warning error for this.  If your compiler has options for warning levels you should ALWAYS compile your code with the maximum level of warnings.  Such a mistake should have been picked up.

0
 

Author Comment

by:wpy
ID: 1255143
Teach me about the compiler 's  options for warning levels please!
Thanks!!!

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255144
I don't know which compiler you have ... check your compiler's documentation.

If it is Visual C++ then I _can_ help (because that is what I work with)

0
 

Author Comment

by:wpy
ID: 1255145
Thanks!

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question