Solved

have a question about malloc

Posted on 2001-06-14
6
182 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now