have a question about malloc

Posted on 2001-06-14
Last Modified: 2012-05-04
could you tell me what this code is doing in words because I get lost in b\w.

int *p;
if (p = (int *) malloc (sizeof(int)))==Null)
 { return (Null) }

Question by:zunera
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

Accepted Solution

robpitt earned 50 total points
ID: 6191210
int *p; - declares 'p' as a pointer to integer.

malloc(sizeof(int)) - alloccates block of memory of the size of an int, this returns a void* pointer to the memory if sucessful or NULL if error.

p=(int*) Cast the return value from malloc to void* to type int* (pointer to integer) and copy the value into 'p'

if(p  ==NULL) - If 'p' is NULL then there has been an error allocating the memory, return NULL from the function.
memset(p,0,sizeof(int)) - Otherwise set the new memory pointed to by 'p' to zeros for a total of size of an integer bytes.

Expert Comment

ID: 6191218
it is allocating space for an integer and ...

if successful setting the bytes of the integer to 0 and returning a pointer to it

if unsuccessful it is returning null


Expert Comment

ID: 6191222
PS 3 paragraph should read:
"from malloc __from__ void* to type int"
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!


Expert Comment

ID: 6191223
beat me to it robpitt *s*

Expert Comment

ID: 6191541
>> if (p = (int *) malloc (sizeof(int)))==Null)
This code is trying to confuse all of us :)
When compiled, it will invite the compiler to issue an error message. When evaluating it will look like:


But we'll try to work from the inside out

this expression result in the size in bytes of an integer

  malloc (sizeof(int))
Calls malloc passing the size of an integer as parameter. malloc should respond by allocating memory with at least the requested size. If it fails, it returns NULL, if it succeeds it returns a pointer to the allocated storage. This pointer is of type void*

  (int *) malloc (sizeof(int))
Converts the type of the pointer returned by malloc to int*

Now the explanation can not continue, as there is a missing opening parenthesis, which might be important to the evaluation of the if-expression.

Expert Comment

ID: 6192447
the cod edoes the following:
int *p;//defines a pointer p
if (p = (int *) malloc (sizeof(int)))==Null)//checks if the space asigned for the array is empty
{ return (Null) }//if it is,we return NULL
memset(p,0,sizeof(int))//places the reading pointer in the beginning of the array.


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

631 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