TIP: Click on subject to list as thread! ANSI
echo: cis.os9.68000.osk
to: ALL
from: David George 72240,134
date: 1993-08-06 16:59:49
subject: #malloc() problem

#: 18510 S12/OS9/68000 (OSK)
    06-Aug-93  16:59:49
Sb: #malloc() problem
Fm: David George 72240,134
To: ALL

I've got a weird one for someone.  It concerns malloc() (no surprise right?).
I have used malloc() a lot and it works ok in most of my programs, but...
In this certain program malloc() doesn't malloc.
Here is a code segment:
char *ptr;
if ((ptr = (char *)malloc(sizeof(char)*80)) == NULL) {
  fprintf(stderr, "can't malloc 80 bytes\n");
  exit(ERROR);
}

If I print the address of ptr before the call to malloc and after malloc,
the address is the same.  In all of my other programs where this works,
the address is different (as it should be).  You would think that if the
malloc failed it would return NULL (at least the docs say say).
I have also replaced NULL with (char *) 0.  I have also written it this way:

ptr = (char *)malloc(sizeof(char)*80);
if (ptr == NULL) {
.
.
}

I have tried it without the (char *) in front of malloc() and I have also
put char *malloc(); in beginning of code.

Has anyone run across this problem?
David George

There is 1 Reply.

SOURCE: compuserve via textfiles.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.