ARM Technical Support Knowledge Articles

NULL POINTER COMPARE FAILS WITH MALLOC AND CALLOC

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

The pointer NULL comparison in the following code never becomes true. However, the loop should allocate all available memory.

void *p;

while (1)  {
  p = malloc (100);
  if (p == NULL)  return;
}

Is there a mistake in my code?

ANSWER

The malloc function returns a memory specific pointer to xdata. When you convert the pointer to a generic pointer, the pointer value is 0x10000. Therefore, this is not a NULL pointer anymore.

When you define the pointer as a memory typed pointer, the code works:

void xdata *p;

while (1)  {
  p = malloc (100);
  if (p == NULL)  return;
}

SEE ALSO

Article last edited on: 2004-04-27 21:52:11

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential