473,569 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1389
In article <11************ **********@e3g2 000cwe.googlegr oups.com>,
chets <ga************ ***@gmail.comwr ote:
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_FAILU RE);

*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
3726
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 may be nothing more than a relection of my ignorance. If what I describe is not an actual bug, I would be very appreciative if you could briefly...
2
1223
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 call I make to the C++ method. Can anyone please help me with this problem? I am not sure wether the leakage is in the C++ or java code. But i...
7
1769
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 like below: void A::function() { B *newobject = new B; newobject->....; newobject->....; //did some action here
10
2760
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: "At a certain point in the code you may be unsure if a particular block is no longer needed. If you free() this piece of memory, but continue to...
18
2246
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
1
3784
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 crashes and restart just after. My application is on a WebServer2003. So I have to resolve 2 problem : -Why the memory size of the w3wp increase...
0
1353
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 pool. RunScript(pScriptName,pFuncName,...) { PyEval_AcquireLock() threadState = Py_NewInterpreter(); PyThreadState_Swap(threadState);
14
2324
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
2280
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 all and release the memory because it's causing Memory leakage.
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.