Solved

why malloc() fail!

Posted on 1998-12-14
15
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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 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

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!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

734 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