Solved

have a question about malloc

Posted on 2001-06-14
6
185 Views
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) }
memset(p,0,sizeof(int))

thanks
0
Comment
Question by:zunera
6 Comments
 
LVL 5

Accepted Solution

by:
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.
0
 
LVL 3

Expert Comment

by:MDarling
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

regards,
Mike.
0
 
LVL 5

Expert Comment

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

Expert Comment

by:MDarling
ID: 6191223
beat me to it robpitt *s*
0
 
LVL 7

Expert Comment

by:KangaRoo
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:

   if(<expression>)==Null)

But we'll try to work from the inside out

  sizeof(int)
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.
0
 
LVL 1

Expert Comment

by:yabelson
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.

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

685 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