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.

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!

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.