What does malloc() do?
What does malloc() do?
Allocates memory.
dynamically allocates space in memory
used for linked lists
prolly other things too
When is it used, can i have an example?
When you don't know how much memory you will need to allocate when you are writing the program.
Read the thread "Changing char in Pointer String?"
no problem , here some example ....
1. for allocate char ...
in the main function.
2 . to allocate struct in linked list concept ...Code:int main () { int *ptr; ... ... ptr = (int*)malloc (sizeof (100)); // 100 refer to how big the size you should need to have? ... ... free (ptr); // you should free the momory after you using it. return 0; }
Code:typedef struct STRLINK{ char data1; STRLINK *next; } STR; typedef STR *STRPTR; int main () { STRPTR startptr; startptr = (STRPTR)malloc (sizeof(STR)); // the size is accordingly to the structure .. .. free (startptr); return 0; }
remember, from the example 1, if you enter input more than sizeof 100, well... it's alright, dont be so brother, the memory allocate will resize it for you.
-- beely
damonbrinkley:
You could have been a bit more helpful, dont you think??Allocates memory.
Or do you just go out of your way to be rude?
I get it now, malloc() is needed to allocate memory when you dont know how much you are going to need when you write the program, or when the amount of memory needed changes based on a variable.
If you have seen the function malloc in any literature on programming, then it's painfully obvious what it does. Every C book I have ever seen it used in states what it does. This holds especially true in the common "teach yourself" or "learn X" style books. Any, and I do mean any introductory C book, and just as likely, every intermediate / advanced book you will ever encounter covers what malloc does.Originally posted by unixOZ
damonbrinkley:
You could have been a bit more helpful, dont you think??
Or do you just go out of your way to be rude?
Basicly, if you know enough to ask the question, you should already have your answer. Furthermore, since this is the great and wonderful internet, we have resources at our disposal called search engines. Two seconds with a search engine will tell you what any standard ANSI function in C does and how to correctly use it.
A better question would have been: "Why did you need to ask?"
Furthermore, and this should be in the sticky links at the top of the page if it isn't already, every question asked here should be as specific as possible.
I mean seriously, what does malloc do? It allocates memory. Period. That is all it does. Malloc returns a block of allocated memory, whose size is measured in the number of bytes passed to it. That is all. End of story.
So again, what does malloc do? It allocates memory, or fails to do so.
Quzah.
Hope is the first step on the road to disappointment.
I recommend this link:
http://www.tuxedo.org/~esr/faqs/smart-questions.html
OK, lets play a little game. I call it spot the mistake. I ask the questions, someone answers:
- What header file is missing from the example code given here to demonstrate the use of malloc()?
Based on this:
>>ptr = (int*)malloc (sizeof (100));
- Is the cast necessary?
Based on this:
>ptr = (int*)malloc (sizeof (100));
- How many int's can we store in the memory malloc()'d here?
Based on this:
ptr = (int*)malloc (sizeof (999));
- How many int's can we store in the memory malloc()'d here?
Now, a final question:
- What's wrong with this example code?
The question was simply what does malloc do. It does little more than allocate memory, so the answer was accurate, imho.You could have been a bit more helpful, dont you think??
Or do you just go out of your way to be rude?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
0. <stdlib.h>Originally posted by Hammer
OK, lets play a little game. I call it spot the mistake. I ask the questions, someone answers:
- What header file is missing from the example code given here to demonstrate the use of malloc()?
Based on this:
>>ptr = (int*)malloc (sizeof (100));
- Is the cast necessary?
Based on this:
>ptr = (int*)malloc (sizeof (100));
- How many int's can we store in the memory malloc()'d here?
Based on this:
ptr = (int*)malloc (sizeof (999));
- How many int's can we store in the memory malloc()'d here?
Now, a final question:
- What's wrong with this example code?
The question was simply what does malloc do. It does little more than allocate memory, so the answer was accurate, imho.
1. Not that argument again . Of course, it's not nessecary, but some here prefer it, its a stylistic thing.
2. At most 50, but possibly a few or maybe very many less.
3. At most 499, but possibly a few or maybe very many less.
4. Which example code?
hello, internet!
moi: go sit in the corner, face the wall, and don't come back until you're behaving properly
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
did i miss somethingOriginally posted by Salem
::thinks moi will want to revise those answers::
hello, internet!