C + Malloc

L

lasek

Hi...i'm writing from Rome...and i don't know english very well so...
Only simple question, which is the difference between those two parts of
code.

[FIRST]
int *pInt=NULL;
int iVar=10;
pInt=&iVar;

[SECOND]
int *pInt=NULL;
int iVar=10;
pInt=(int*)malloc(1*sizeof(int));
pInt=&iVar;


I really need to allocate memory before assign an address to a pointer
variable?.
Thus because i know that declaration not allocate memory for pointer
variable.
Is correct ?.

Thanks all..
 
S

Serge Paccalin

Le jeudi 14 octobre 2004 à 11:45, lasek a écrit dans comp.lang.c :
Hi...i'm writing from Rome...and i don't know english very well so...
Only simple question, which is the difference between those two parts of
code.

The second piece of code has a bug called a memory leak.
[FIRST]
int *pInt=NULL;
int iVar=10;
pInt=&iVar;

[SECOND]
int *pInt=NULL;
int iVar=10;
pInt=(int*)malloc(1*sizeof(int));
pInt=&iVar;

I really need to allocate memory before assign an address to a pointer
variable?.

Yes but calling malloc() is only one way of allocating memory. Defining
a variable is another way.

[FIRST]
int *pInt=NULL;
int iVar=10;
pInt=&iVar;

You allocated a small piece of memory when you defined iVar. Then you
put the address of that memory into pInt. This code is correct. Note
that iVar will go away when you leave the current block of code.

[SECOND]
int *pInt=NULL;
int iVar=10;
pInt=(int*)malloc(1*sizeof(int));
pInt=&iVar;

You allocated a small piece of memory when you defined iVar. Then you
dynamically allocated another small piece of memory by calling malloc()
and put its address into pInt. Finally you put the address of iVar into
pInt *and lost the only copy of the address of the dynamically allocated
memory; you need to free it by calling free(), but now you can't because
you don't have its address anymore...
Thus because i know that declaration not allocate memory for pointer
variable.

Each time you define a variable, you allocate memory for the variable.
If it's a pointer, you allocate memory for the pointer itself but not
the memory you want to point to.


--
___________ 14/10/2004 12:02:35
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
S

S.Tobias

lasek said:
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).
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

lasek said:
Hi...i'm writing from Rome...and i don't know english very well so...
Only simple question, which is the difference between those two parts of
code.

[FIRST]
int *pInt=NULL;
int iVar=10;
pInt=&iVar;

[SECOND]
int *pInt=NULL;
int iVar=10;
pInt=(int*)malloc(1*sizeof(int));
pInt=&iVar;


I really need to allocate memory before assign an address to a pointer
variable?.
No. with malloc above, you get some memory allocated and the pInt
points to that memory.
then you make pInt point to iVar, which already have storage, and the
pointer to the storage allocated with malloc is lost(creating a memory
leak)
Thus because i know that declaration not allocate memory for pointer
variable.
Is correct ?.
It is, but there is no need for allocating storage for it if you make it
point to something which already have storage.
 
R

Richard Bos

lasek said:
Hi...i'm writing from Rome...and i don't know english very well so...

Sono certo che parlai miglior Inglese che io parlo Italiano.
[FIRST]
int *pInt=NULL;
int iVar=10;
pInt=&iVar;

This is correct.
[SECOND]
int *pInt=NULL;
int iVar=10;
pInt=(int*)malloc(1*sizeof(int));

No need to cast malloc(). This is more solid:

pInt=malloc(n * sizeof *pInt);

pInt=&iVar;

Ah, and this is wrong.
I really need to allocate memory before assign an address to a pointer
variable?.

No. Quite the contrary, in fact. You need to _either_ allocate memory
using malloc() or similar functions, _or_ assign the pointer to the
address of an existing object.
By doing both, you have in fact created a memory leak. You've allocated
memory using malloc(). You have _one_ pointer containing this memory's
address: pInt. And in the next line, you scribble over that address, and
pInt now contains the address of iVar, rather than the address of the
allocated block. Now the problem is: how are you going to free that
block of memory? You've lost its address; what are you going to pass to
free()?
Thus because i know that declaration not allocate memory for pointer
variable.

Well...

int *ptr;

does allocate memory for ptr _itself_, just as

int i;

allocates memory for one int, called i. What it doesn't do is allocate
memory for ptr to point at. But that doesn't mean that you have to
explicitly do so yourself.
You _must_ point ptr at a valid bit of memory before using *ptr; but it
doesn't matter whether you point it at memory you got from malloc(), or
at memory occupied by an existing int, say, at i. Except, of course,
that memory you got from malloc() needs to be free()d, and normal
variables shouldn't.

Richard
 
F

Flash Gordon

Hi...i'm writing from Rome...and i don't know english very well so...

Not a problem. Your English is better than my Roman... ;-)
Only simple question, which is the difference between those two parts
of code.

[FIRST]
int *pInt=NULL;
int iVar=10;
pInt=&iVar;

This is correct.
[SECOND]
int *pInt=NULL;
int iVar=10;
pInt=(int*)malloc(1*sizeof(int));
pInt=&iVar;

This is a memory leak and so bad.
I really need to allocate memory before assign an address to a pointer
variable?.
No.

Thus because i know that declaration not allocate memory for pointer
variable.

Sorry, not with you here.
Is correct ?.

Thanks all..

Your first example is correct, the second is definitely not what you
wanted. You only call malloc what you want some new memory, not when you
want to access memory you already have.

Also, a note on calling malloc when you *do* want it. Calling it like
this would be better:
pInt=malloc(no_of_elements * sizeof *pInt);
As you can see, if you change the type of pInt the above means you don't
need to change the call to malloc.

Also, calling malloc without a prototype in scope (because you forgot to
#include <stdlib.h>) invokes undefined behaviour and on some systems
*will* cause problems. If you don't cast the return value of malloc the
compiler *will* generate a diagnostic (either a warning or an error)
telling you that you got something wrong. The warning may not say
exactly what is wrong, but at least it will tell you that *something* is
wrong.
 
D

Dan Pop

In said:
Hi...i'm writing from Rome...and i don't know english very well so...
Only simple question, which is the difference between those two parts of
code.

[FIRST]
int *pInt=NULL;
int iVar=10;
pInt=&iVar;

[SECOND]
int *pInt=NULL;
int iVar=10;
pInt=(int*)malloc(1*sizeof(int));
pInt=&iVar;

In the second part you're creating a memory leak, because you're losing
the address of the memory block allocated by malloc. It will remain
allocated until program termination, but you can no longer access it,
because you have lost its address.
I really need to allocate memory before assign an address to a pointer
variable?.

How else can you obtain the address to be assigned to the pointer?
Thus because i know that declaration not allocate memory for pointer
variable.
Is correct ?.

Nope. Declaration doesn't allocate memory for anything, but definition
does allocate memory for the pointer variable, which has its own address
like any other variable. Before *assigning a value* to the pointer
variable you need to obtain that value somehow. There are three portable
methods:

1. Use the value of another pointer variable.

2. Use the unary & operator.

3. Use malloc and friends.

A 4th, non-portable method, consists in converting an integer value to a
pointer value, via a cast operator.

Dan
 
C

CBFalconer

lasek said:
Hi...i'm writing from Rome...and i don't know english very well so...
Only simple question, which is the difference between those two parts of
code.

[FIRST]
int *pInt=NULL;
int iVar=10;
pInt=&iVar;

[SECOND]
int *pInt=NULL;
int iVar=10;
pInt=(int*)malloc(1*sizeof(int));
pInt=&iVar;

I really need to allocate memory before assign an address to a pointer
variable?.
Thus because i know that declaration not allocate memory for pointer
variable.
Is correct ?.

No it isn't. The first is correct. The second creates a useless
memory leak. You should also eliminate the useless casts and the
ugly hungarian notation. Blanks around operators are a great aid
to legibility.
 
L

lasek

Thanks all for the answers...in effect, i've forgotten in my mind that
malloc return an address, so the line (ptInt=malloc....) have no meaning
if i readdress the variable pointer with another address (&stuff).

For stdlib i know the headers files but simply not reported in the
textarea.

best regards..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,066
Members
48,770
Latest member
ElysaD

Latest Threads

Top