Unix Programming - memory leakage problem

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2006 > memory leakage problem





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author memory leakage problem
chets

2006-10-30, 1:22 am

Hi all
Can anyone tell me what is the difference between:-

*p=q; and p=&q; in C

where declaration is like this
char **p;
char *q;

Because if I do *p=q; in one file after mallocing q and want to free q
in another program, it does not do that, memory leakage.

if I do p=&q; than it works.

So if anyone can tell me how these are internally treated than it can
be helpful.
Thanks in advance.;

Pierre Barbier de Reuille

2006-10-30, 1:22 am

chets a =E9crit :

> Hi all
> Can anyone tell me what is the difference between:-
>
> *p=3Dq; and p=3D&q; in C
>
> where declaration is like this
> char **p;
> char *q;
>
> Because if I do *p=3Dq; in one file after mallocing q and want to free q
> in another program, it does not do that, memory leakage.
>
> if I do p=3D&q; than it works.
>
> So if anyone can tell me how these are internally treated than it can
> be helpful.
> Thanks in advance.;


This is very simply because what you has completly unpredicatble
results. You just take a non-initialised pointer (i.e. with random
value in it), and affect that random position in memory with the value
of another variable. What happens hugely depends on the initial value
of p. Try to modify your program like that:

char **p=3DNULL;
char *q=3DNULL;

Now, *p =3D q should raise a segmentation fault at runtime, while p =3D &q
will work.

Hope I am clear enough as why this is a mistake you're doing ! And
don't forget: always initialise your pointer to something meaningfull
(i.e. NULL unless you have a default object to point to).

Pierre

PS: this question is 100% about C, 0% about Unix, so I crosspost on
comp.lang.c

David Schwartz

2006-10-30, 7:31 pm


chets wrote:
> Hi all
> Can anyone tell me what is the difference between:-
>
> *p=q; and p=&q; in C
>
> where declaration is like this
> char **p;
> char *q;


*p=q; means: Take what p points to and set it equal to the current
value of the variable q.

For this to work, 'p' must point to a valid 'char *'. Otherwise,
changing the contents of what 'p' points to can lead to a crash. (And
if you haven't assigned a value to 'q', what value you put in is
undefined.)

p=&q; mean: Set p equal to the address of the variable q.

This has no special requirements. Since you declared 'p', it exists and
storage has been allocated for it, and of course 'q' always has an
address.

DS

Spoon

2006-10-31, 7:23 am

David Schwartz wrote:

> p=&q; mean: Set p equal to the address of the variable q.
>
> This has no special requirements. Since you declared 'p', it exists
> and storage has been allocated for it, and of course 'q' always has
> an address.


Unless q is declared with the register storage-class specifier,
or q is a bit-field.

^_^
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com