Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

why malloc() fail!

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
wpy
Asked:
wpy
1 Solution
 
rwilson032697Commented:
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
 
alexoCommented:
>> you cannot realloc a memory block that has not been malloced.
Partially true.  You can realloc() a NULL pointer.  Works just like malloc().
0
 
wpyAuthor Commented:
I'v  malloced it !
but it not work .Pelease Help me!
 
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

 
ozoCommented:
How did you malloc it?  How do you call CreateMsg?
0
 
RONSLOWCommented:
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
 
RONSLOWCommented:
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
 
wpyAuthor Commented:
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
 
elfieCommented:
> 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
 
szetoaCommented:
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
 
RONSLOWCommented:
sizeof(char) was fine.  Please ignore the comment above about sizeof(char) needing to be sizeof(char*).

0
 
wpyAuthor Commented:
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
 
RONSLOWCommented:
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
 
wpyAuthor Commented:
Teach me about the compiler 's  options for warning levels please!
Thanks!!!

0
 
RONSLOWCommented:
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
 
wpyAuthor Commented:
Thanks!

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now