malloc


 
Thread Tools Search this Thread
Top Forums Programming malloc
# 1  
Old 09-09-2002
malloc

hello sir

since by mentioning a integer pointer and storing the integers
by incrementing the pointer value
then what is the purpose of malloc?
u can decalre it as
in t *p;
several integers can be stored by incrementing the value of p,
hence what is the diffrence between this declaration and
malloc function.
this is an basic question according to my knowledge.
please answer this.
# 2  
Old 09-10-2002
When you declare a variable in a language you are telling the compiler to set aside some memory for that variable.

This is important because the program (or process) when it is running needs to know which memory addresses are being used by what. e.g. a variable or a section of the machine code etc. examples of variables are:

int mynumber;
char keypress;
char name[21];

All of these variables will have a fixed length in memory depending on the type of variable you have declared. In theory you should not be able to use the memory assigned to the variables for anything else.

Some languages allow you to define a special kind of variable called a pointer. A pointer is usually just an integer, or number, and its contents refer to an address in memory.

e.g. int *mypointer;

The pointer does not 'do' anything yet you have to define where it points to, e.g.

mypointer = 20;

You may then set the value of the address the pointer points to by some value e.g.

*mypointer = 100;

In this example this means set the value of address location 20 to 100.

When you increment the pointer:

mypointer++;

it would point to address 21 and then the assignment

*mypointer = 198;

would change the value of address location 21.

Arbitarily assigning a pointer to a given location is very risky and you will be lucky to get a program to work like that. The process will be loaded by the Operating System when it runs and at that point the memory areas are set up. Where the data portions are set up (i.e the actual physical addresses used by variables) won't be predicatable. It is more likely you will use an area of memory you are not supposed to.

When your program tries to use memory that is not assigned to it, then the O/S will stop execution. There are several names for this depending on the O/S but a 'Core Dump' is quite common in Unix, or 'General Protection Fault' in Windows.

Having fixed length variables, espcially for arrays, can be limiting. To access memory without being core dumped you will need to ask the Operating System for some more memory. You do this with malloc().

First get a pointer:

int *mypointer;

Then ask for some memory:

mypointer = malloc(100);

The malloc() function will then ask the Operating system for 100 more bytes of memory and tell you which address this memory block starts at. The address, of course, is stored in the mypointer variable.

You may then do things like: *(mypointer + 10) = 10; To reference parts of you memory you have assigned. Be careful though, as you have discovered, you will be able to reference beyond the memory you have assigned. You must write your program in such a way that it does not violate bounds you have created.

When you are done with the memory give it back to the O/S with the free() function. Sometimes programs are written that will continue to malloc memory and never free it. Eventually there will be a limit and the O/S will not be able to provide a program with any more memory. This is a memory leak!

You will want to dynamically allocate memory (use malloc() ) when you don't know how big your variable structures will be when you write your program. e.g. a linked list, a tree or a buffer to store the contents of a file.

I thinks that is enough to be getting on with!
# 3  
Old 09-30-2002
Re: malloc

Here is a simple answer.

int * p;
It just declares a pointer which could point to a integer, but now it isn't initialized, this is to say, it points nothing. If you increase its value, p will point to a undefined value, something may be important or unable to access for you, this will lead to a undesirable result.

malloc is a function to allocate a block of memory to store data.
if you want to access the data, you need to know the address of the memory , the address is called a pointer.


Simply to say, pointer is just a address of a block of memory, but which memory? Then malloc will work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with malloc()

Good day! I'm a newbie in C. I'm trying to get an unlimited input from the user using malloc then printing the inputs after the user presses enter. My code works, but there's a warning that I don't know how to fix. Please help me. Thank you. Here's my code: #include <stdio.h> #include... (6 Replies)
Discussion started by: eracav
6 Replies

2. Programming

malloc vs new speed

Which one is faster among malloc and new? My understanding is that since new also has to call constructors after allocating memory it must be slower than malloc. Am I correct? (1 Reply)
Discussion started by: rupeshkp728
1 Replies

3. Programming

help with malloc [solved]

Hi i found code in google how to malloc an 2D array and i tried that : #include<stdio.h> #include<stdlib.h> int **A; int **B; int main(int argc,char *argv) { printf("name of text : %s\n",argv); //read arrays int i,j; int l,m; int M,n; FILE *fp; fp=fopen(argv,"r"); ... (0 Replies)
Discussion started by: giampoul
0 Replies

4. UNIX for Dummies Questions & Answers

Kmalloc and malloc

Do kmalloc and malloc allocate from same heap ? (3 Replies)
Discussion started by: dragonpoint
3 Replies

5. Programming

malloc vs realloc

Why when using realloc, john is reversed 3 times but not the other 2 names ? But if I use malloc, then the 3 names are reversed correctly ? (but then there is a memory leak) How can I reverse all 3 names without a memory leak ? char *BUFFER = NULL; char *STRREVERSE(const char *STRING) {... (5 Replies)
Discussion started by: cyler
5 Replies

6. UNIX for Advanced & Expert Users

Malloc Implementation in C

Hey Guys Some of my friends have got together and we are trying to write a basic kernel similar to Linux. I am trying to implement the malloc function in C and I am using a doubly linked list as the primary data structure. I need to allocate memory for this link list (duh...) and I don't feel... (2 Replies)
Discussion started by: rbansal2
2 Replies

7. Programming

Malloc implementation in C

Hey Guys I am trying to implement the malloc function for my OS class and I am having a little trouble with it. I would be really grateful if I could get some hints on this problem. So I am using a doubly-linked list as my data structure and I have to allocate memory for it (duh...). The... (1 Reply)
Discussion started by: Gambit_b
1 Replies

8. Programming

malloc()

Some one please explain me what is Dynamic memory allocation and the use of malloc() function.How do we allocate memory dynamically and also the other way? (3 Replies)
Discussion started by: rash123
3 Replies

9. Programming

When to use Malloc?

Hi! I hope this is the correct forum to post the question even if I'm a newbie... I am a C-newbie (and really on the edge to be a C-addict ;) ) and have a question. When should I use malloc? To state it differently, when should I NOT use malloc? For instance, if I have an array of... (5 Replies)
Discussion started by: Tonje
5 Replies

10. Programming

a problem about malloc()

1 . Thanks everyone who read the post. 2 . the programe is that : #include <stdio.h> #include <string.h> void do_it(char *p) { p = (char *) malloc(100); (void )strcpy(p,"1234"); } int main(void) { char *p; do_it(p); (void )printf("p = %s \n",p); (1 Reply)
Discussion started by: chenhao_no1
1 Replies
Login or Register to Ask a Question