Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What is malloc and how is it used in C programming?

Status
Not open for further replies.

vimal2010

Banned
Joined
Sep 14, 2005
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
0
what is malloc?why do we use them?whats their importance in C programming?
 

Re: malloc??

malloc (memory allocation) is used to dynamically allocate memory at run time. Possible uses for this function are:
Read records of an unknown length.
Read an unknown number of database records.

The simplest way to reserve memory is to code something like:

void main()
{
char string[1000];

strcpy (string, "Some text");
}

The example above has two problems:

If the data is less than 1000 bytes we are wasting memory.
If the data is greater than 1000 bytes the program is going to crash.
The 1000 bytes are reserved throught out the life of the program. If this was a long running program that rarely used the memory, it would again be wasteful.
malloc allows us to allocate exactly the correct amount of memory and with the use of free only for the time it is required.


--------------------------------------------------------------------------------

Library: stdlib.h

Prototype: void *malloc(size_t size);

Syntax: char * String;

String = (char *) malloc(1000);


--------------------------------------------------------------------------------

Looking at the example syntax above, 1000 bytes are reserved and the pointer String points to the first byte. The 1000 bytes are NOT initialized by malloc. If the memory is NOT available, a NULL pointer is returned. Please note, the cast cast is required to return a pointer of the correct type.

leomecma
 

malloc??

void main() is an error. Use int main(void).

You can also change the size of the allocated space with realloc().

To avoid program crash due to strcpy() of unknown length string, use strncpy().

The (char*) cast is not required, and generally not recommended.
 

Re: malloc??

Echo47 I think the use of strcpy() or strncpy() aren't the question, just idea of malloc use is important. About void main(), void main (void) depends of the compiler (some give an warning without void) and int main(void) is used generaly for C++ and some compilers introduce automatically int main(int argc,char*argv[]) on project. I think it isn't the problem too. About cast I removed it example of an book ... but I agree which desnecessary but I don't see problems of use, I think the autor use it for clean ....

Can you explain to me why void main() and cast can be unsafe?

leomecma
 

Re: malloc??

>>>void main() is an error. Use int main(void).

i think there is no major diff btw ther two statements and i depends on the compiler..
when ever main() ,is written it is understood that there are no arguments and hence main(void) is unnecessary..
and void main() and int main(void) , here if you are using the second one
we need to mention the statement "return 0;" at the end of the program..
else there may be chance of returning someother integer.. so its better to use
void main() in all cases
regards
 

malloc??

The ANSI C standard describes only two forms for main:
int main(void) { /* ... */ }
int main(int argc, char *argv[]) { /* ... */ }

Here's what the comp.lang.c FAQ says about void main():
https://www.eskimo.com/~scs/C-faq/q11.12.html
https://www.eskimo.com/~scs/C-faq/q11.14.html
https://www.eskimo.com/~scs/C-faq/q11.15.html

Oops, I didn't mean that it's wrong to cast malloc's return value. It is correct, but redundant:
https://www.eskimo.com/~scs/C-faq/q7.7.html
Most programmers don't use it anymore because it just adds clutter.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top