Solved

why malloc() fail!

Posted on 1998-12-14
15
399 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now