Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

have a question about malloc

Posted on 2001-06-14
6
Medium Priority
?
202 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

773 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