Re: C + Malloc
From: S.Tobias (sNOiSPAMt_at_amu.edu.pl)
Date: 10/14/04
- Next message: pete: "Re: Off Topic: Why is stdprn not defined under Windows?"
- Previous message: Daniel Vallstrom: "Floating-point bit hacking: iterated nextafter() without loop?"
- In reply to: lasek: "C + Malloc"
- Next in thread: QNils_O=2E_Sel=E5sdal=22?=: "Re: C + Malloc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 14 Oct 2004 10:20:00 GMT
lasek <claudio.rossetti@acrm.it> wrote:
> Only simple question, which is the difference between those two parts of
> code.
> [FIRST]
> int *pInt=NULL;
> int iVar=10;
This is a definition, so iVar object is allocated automatically.
> pInt=&iVar;
Correct, pInt points to iVar object.
> [SECOND]
> int *pInt=NULL;
> int iVar=10;
> pInt=(int*)malloc(1*sizeof(int));
Correct, you allocate new memory and assign pInt to it.
> pInt=&iVar;
Correct, but you reassign pInt back to iVar object again.
> I really need to allocate memory before assign an address to a pointer
> variable?.
No, when you define a variable, the memory is allocated automatically.
The variable is the object, and a pointer may point to it.
Simplifying a little, one can say that you can have as many objects
as many variables you have in your program. If you need more objects,
then you need to allocate.
> Thus because i know that declaration not allocate memory for pointer
> variable.
> Is correct ?.
Correct. Pointer definition automatically allocates memory only for
the pointer variable itself (yes, the pointer is also kind of object),
but *not* for what the pointer is supposed to point at (the pointer is
said to be "dangling", until you assign to something valid).
-- Stan Tobias sed 's/[A-Z]//g' to email
- Next message: pete: "Re: Off Topic: Why is stdprn not defined under Windows?"
- Previous message: Daniel Vallstrom: "Floating-point bit hacking: iterated nextafter() without loop?"
- In reply to: lasek: "C + Malloc"
- Next in thread: QNils_O=2E_Sel=E5sdal=22?=: "Re: C + Malloc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|