Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

why malloc() fail!

Posted on 1998-12-14
15
Medium Priority
?
438 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 20 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

963 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