Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

have a question about malloc

Posted on 2001-06-14
6
Medium Priority
?
194 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
[X]
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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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.
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.

670 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