473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

memory leakage problem

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.;

Oct 30 '06 #1
2 1376
In article <11**********************@e3g2000cwe.googlegroups. com>,
chets <ga***************@gmail.comwrote:
Can anyone tell me what is the difference between:-
*p=q; and p=&q; in C
*p=q means to take the current value of q, do any necessary type
or value conversions, and store the result in the memory
pointed to by the pointer p, which must have already been given a value
(i.e., must already point to storage.)

p=&q means to take the address of q and store it in the pointer variable
p, thus setting p to point to an object (i.e., q). p does not need to
have a value beforehand, and any value it had before is ignored.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Oct 30 '06 #2
chets wrote:
Hi all
Can anyone tell me what is the difference between:-

*p=q; and p=&q; in C
*p = q; /* the value of q is copied into the memory location
referred to by the value of p. */

In *p = q, if the value of p was not already a valid pointer, then the
expression causes undefined behaviour. The value of p remains unchanged.

p = &q; /* the address of q is copied into the pointer variable p */

In p = &q, it doesn't matter whether the value of p was already a valid
pointer or not. Its old value is not used but is replaced.

The above explanation applies whenever the expressions are valid. That
is, when the type of p is 'pointer to' the type of q.
where declaration is like this
char **p;
char *q;
These declarations are OK. Of course, p must be set to a valid pointer
first, if you want to use *p = 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.
You can't free memory in one program that was allocated in a different
program. But you can do it in separate translation units (C files) that
are linked together into a single program.
if I do p=&q; than it works.
You need to provide a complete example program to demonstrate what's not
working.

This works:

/* foo.c */

#include <stdlib.h>

extern char **p;

void do_free(void)
{
free(*p); /* free q through *p */
free(p); /* free p itself */
}
/* bar.c */

#include <stdlib.h>

char **p;

int main(void)
{
char *q = malloc(sizeof *q);
p = malloc(sizeof *p);
if(!p || !q) exit(EXIT_FAILURE);

*p = q;

do_free();

return 0;
}

--
Simon.
Oct 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: frustrated | last post by:
Before I begin, I must first make the following disclaimer: Although I have considerable programming experience, I do not consider myself by any means to be an expert C++ programmer. The following...
2
by: Sambucus | last post by:
Hi group! I am using C++ and java with JNI to get some text in a RICHEDIT to my java program. I do so by accessing a C++ method every second. It all works fine except that it leaks memory every...
7
by: andylcx | last post by:
hi all: I have a code like below, is there any serious memory leakage in my code. I am confusion now but no idea how to fix it. In the member function of class A, I create a new object of class B...
10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
18
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
1
by: Gaël | last post by:
Hi everybody! I have a really big problem with ASP.NET application. I noticed that the w3wp.exe memory size, increase with the time and the use of my website. When it raise a certain value, w3wp...
0
by: kiran kumar | last post by:
Hi All, I am working on embedded python on C these days. I feel there is a memory leakage in this code. I have used our own memory pool and all the python code will use the heap from this memory...
14
by: madhawi | last post by:
i want to know that on what situation memory leakage happan and what is the solution to solve the problem of memory leakage.
11
by: prpradip | last post by:
I have an ImageList (_imageList). In _imageList I have put large numbers of Icons. Now what I need is to get Handle of all Icons that I put in _imageList, so that I can destroy (DestoryIcon) them...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.