473,549 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Querry (Newbe)


Dear All

As I was going through the Recent replies on the realloc(),
I got some question and my annalysis on that, so regarding on these
please guide me where I fail on the theoritical and practical
Knowledge. I am not able to read all the thread in the replies as
due to some problem in the web server.

Point 1.

If we do the realloc then it means that we have allocated the
extended memory for the current memory, for which we have
reallocated it. Means I need not to free the previous memory
which I extendend to realloc if compiler allocates memory
(extended memory) from the place where intial memory was allocated.

And we need to free if the memory is allocated by the
(realloc)in the new region.

so the key is to always free the memory when you reallocate
the memory by realloc fucntion.

How much I am correct on the Point 1 ?

Point 2.

what is the diffrence between the calloc() and malloc()
As far As I know the basic diffrence is that
1. malloc takes 1 argumnets while calloc takes two
2. malloc initialise the memory with the garbage values while
calloc initialise it with 0 (Zero)
3. malloc allocates continious memeory i.e one Block while
calloc alloactes into the Block
calloc (100, 2) ,means two block of 100 memoty alloaction.

apart from the above is any more diffrence between them ??

Point 3.

This may be looks off topic to you but I have one thing to ask
is there any diffrence between the malloc and new ??

Point 4.

why we need to derefrence the pointers once we are are done with our
work; I am not aware of garbage collection, And where I can find the
memory leak into the program ?

Thanks In Advance
Regards
Ranjeet

Nov 14 '05 #1
5 1566


ra***********@g mail.com wrote:
Dear All

As I was going through the Recent replies on the realloc(),
I got some question and my annalysis on that, so regarding on these
please guide me where I fail on the theoritical and practical
Knowledge. I am not able to read all the thread in the replies as
due to some problem in the web server.

Point 1.

If we do the realloc then it means that we have allocated the
extended memory for the current memory, for which we have
reallocated it. Means I need not to free the previous memory
which I extendend to realloc if compiler allocates memory
(extended memory) from the place where intial memory was allocated.

And we need to free if the memory is allocated by the
(realloc)in the new region.

so the key is to always free the memory when you reallocate
the memory by realloc fucntion.

How much I am correct on the Point 1 ?
If realloc() copies the old memory to a new region,
realloc() releases the old region. You must not try to
release it yourself.
Point 2.

what is the diffrence between the calloc() and malloc()
As far As I know the basic diffrence is that
1. malloc takes 1 argumnets while calloc takes two
2. malloc initialise the memory with the garbage values while
calloc initialise it with 0 (Zero)
3. malloc allocates continious memeory i.e one Block while
calloc alloactes into the Block
calloc (100, 2) ,means two block of 100 memoty alloaction.

apart from the above is any more diffrence between them ??
You are correct on 1 and 2. For 3, there is no difference:
calloc() allocates one contiguous block, just as malloc() does.
Point 3.

This may be looks off topic to you but I have one thing to ask
is there any diffrence between the malloc and new ??
Yes: malloc() is a Standard library function, and `new'
is an identifier that you can use in your program for any
purpose you like. (There is a Different Language in which
`new' has special significance, but the Different Language
is off-topic here.)
Point 4.

why we need to derefrence the pointers once we are are done with our
work; I am not aware of garbage collection, And where I can find the
memory leak into the program ?


I'm sorry, but I do not understand this question or
questions.

--
Er*********@sun .com

Nov 14 '05 #2
ra***********@g mail.com writes:
[snip]
Point 2.

what is the diffrence between the calloc() and malloc()
As far As I know the basic diffrence is that
1. malloc takes 1 argumnets while calloc takes two
2. malloc initialise the memory with the garbage values while
calloc initialise it with 0 (Zero)
3. malloc allocates continious memeory i.e one Block while
calloc alloactes into the Block
calloc (100, 2) ,means two block of 100 memoty alloaction.

apart from the above is any more diffrence between them ??


Eric Sosman has already written most of what I would have, but I'd
like to make one additional point.

calloc initializes the allocated block to all-bits-zero. This is not
necessarily useful. If the allocated block is to be used as an array
of unsigned char, you're guaranteed that the elements of the array
will have the value 0. If it's to be used as an array of some other
integer type, you're *practically* guaranteed the same thing; the
standard doesn't explicitly state that all-bits-zero is a valid
representation for the value 0, but I think there's been a more recent
ruling from the committee that it is.

For other types (particularly floating-point and pointer types),
there's no guarantee that all-bits-zero is a valid value. On many
systems, setting a floating-point variable to all-bits-zero will
result in the value 0.0, and setting a pointer variable to
all-bits-zero will result in a null pointer, *but* this is not
guaranteed and you shouldn't assume it. It's likely to work correctly
during testing, then fail mysteriously when you port it to another
implementation and you've forgotten your initial assumptions.

If you want the allocated memory to be initialized to some meaningful
value, you should do it yourself rather than depending on calloc (or
memset, or whatever) to do it for you. There are cases where you can
depend on calloc to do the right thing, but in general it's safest to
assume that it fills the allocated block with garbage -- in which case
you might as well use malloc() rather than calloc().

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
On Tue, 24 May 2005 23:19:02 +0000, Keith Thompson wrote:
ra***********@g mail.com writes:
[snip]
Point 2.

what is the diffrence between the calloc() and malloc() As far As I
know the basic diffrence is that 1. malloc takes 1 argumnets while
calloc takes two 2. malloc initialise the memory with the garbage
values while
calloc initialise it with 0 (Zero)
3. malloc allocates continious memeory i.e one Block while
calloc alloactes into the Block
calloc (100, 2) ,means two block of 100 memoty alloaction.

apart from the above is any more diffrence between them ??


Eric Sosman has already written most of what I would have, but I'd like
to make one additional point.

calloc initializes the allocated block to all-bits-zero. This is not
necessarily useful. If the allocated block is to be used as an array of
unsigned char, you're guaranteed that the elements of the array will
have the value 0. If it's to be used as an array of some other integer
type, you're *practically* guaranteed the same thing; the standard
doesn't explicitly state that all-bits-zero is a valid representation
for the value 0, but I think there's been a more recent ruling from the
committee that it is.


Indeed, Technical Corrigendum 2 of the C99 Standard says this in item #9:

Page 39, 6.2.6.2
Append to paragraph 5:
For any integer type, the object representation where all the bits are
zero shall be a representation of the value zero in that type.

Rob Gamble
Nov 14 '05 #4
On Tue, 24 May 2005 18:11:24 -0400, Eric Sosman wrote:
ra***********@g mail.com wrote:

[snip]
Point 4.

why we need to derefrence the pointers once we are are done with our
work; I am not aware of garbage collection, And where I can find the
memory leak into the program ?


I'm sorry, but I do not understand this question or
questions.


I can come up with two interpretations here.

The first is: "Why can't we dereference pointers that point to memory that
has been freed since there is no garbage collection system to re-use the
memory?"

The second is: "Why do we have to assign a NULL value to a pointer that
points to free'd memory?"

My answer to the former would be: Because the Standard says so. Some
implementations may, for example, return memory back to the operating
system. In any case, no guarantees are made by the Standard regarding the
free'd memory except that attempting to access it invokes undefined
behavior.

My answer to the latter would be: You don't have to but some consider it
good practice as it may help clarify code and improve debugging efforts.

If neither of the interpretations is correct, the OP should rephrase the
question.

Rob Gamble
Nov 14 '05 #5
In article <pa************ *************** *@gmail.com>,
Robert Gamble <rg*******@gmai l.com> wrote:
I can come up with two interpretations here. The first is: "Why can't we dereference pointers that point to memory that
has been freed since there is no garbage collection system to re-use the
memory?" My answer to the former would be: Because the Standard says so. Some
implementation s may, for example, return memory back to the operating
system. In any case, no guarantees are made by the Standard regarding the
free'd memory except that attempting to access it invokes undefined
behavior.


Adding a bit to that:

a) Having a garbage collection system of some sort is perfectly valid
and fairly common.

b) It is common for the memory allocator to use the space occupied by
the freed memory in order to store information needed by the allocator.
For example, the allocator might store the address of the next free
chunk of memory, so that it can chase through the list in order to find
available memory;

c) The C standard only allows C programmers to access within objects,
and once memory is freed it is no longer part of an object; thus
accessing that memory will have undefined results;

d) For the -typical- C allocator, returning memory back to the
operating system is fairly unusual, as it typically is too much overhead
to figure out whether there might be memory further along that is still
in use; this fits in with the traditional Unix "memory growth is
always after the last allocated memory" paradigm.

e) It is acceptable behaviour within the standard for
the memory allocator to allocate in distinct virtual memory "pages" and to
keep track of the use of the page, and to return the virtual page to
the operating system when the page is no longer in use. This may
result in holes in the logical address space, but that's irrelevant
because the standard places tight constraints on accessing outside of
valid existing objects. This has been implemented on real systems.

There are advantages and disadvantages to each of (d) and (e).

f) On some real systems (e.g., the DEC VAX), each "pointer" is
not just an address but a key to a memory descriptor, with hardware
or software protection that -prevents- you from accessing out of bounds.
When you have freed a pointer, the memory descriptor may thereafter
be garbage or may be specifically set to a descriptor that will cause
a fault.

g) Keep in mind that a pointer is not necessarily a number as such.
You should not think of it as strictly a byte (or word) count from the
beginning of RAM. A pointer may be a semi-opaque value with no inherent
meaning, with a look-up table of some sort determining what the
pointer refers to. If you look at what you can legally do with
pointers, you will find that it is even possible for "the same pointer"
to refer to different parts of memory in different code sections -- so
once you have free()'d a pointer, there is no certainty at all about
-what- it refers to!
--
"[...] it's all part of one's right to be publicly stupid." -- Dave Smey
Nov 14 '05 #6

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

Similar topics

4
3022
by: Eric Kincl | last post by:
Hey everyone, I know this isn't a SQL group, but I'm on my colleges news server and they don't offer one. Hopefully someone here will be able to help me. I have a database that is normalized with a many-to-one ratio. Basically I have one table, "concerts", another table "bands_at_concerts", and a final table, "bands". Concerts has a key...
2
3188
by: Eric Kincl | last post by:
Hello, I have an array of data in PHP. I would like to insert each member of the array into it's own row in SQL. The array is of variable length, so it would have to be dynamic code. How would I go about this? Would I stick the SQL querry generation and actual querry into a while loop? This would generate a lot of traffic between the SQL...
1
1649
by: Sebastien GIRAUD | last post by:
Hello, First let say that am french and that i'll try to write the best english i can... I'm a python newbe and have the following problem : I try to create 2 threads in a server program and they seems to block each other. I use something like this : start thread waiting for connexion with : server.threadserver = threading.Thread(target =...
0
1599
by: Costa Lino | last post by:
Hi All, I have a DataSet with xml file and I want to make a querry like this DataView dv = new DataView(mytable); dv.RowFilter = " Impression < ( MaxImpressions) "; Impression et MaxImpressions is collumns in my table the same querry access database is working but her is return 0
5
1839
by: Clownfish | last post by:
OK, I'm having a brain freeze. I have a table like this: Office Name Phone ---------------------------------- SG Larry 555-1212 SG Moe 553-4444 SG Curly 666-8888 PO Ren 222-9999
17
2691
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not pattern1 print >>orcfilename, line I am pretty sure my code isn't close to what I want. I need to be able
1
1740
Steve Kiss
by: Steve Kiss | last post by:
Hi. I am developping a site for which one of the pages uses querry strings to pass some parameters. I can use the querry strings if I call the page from a plain html anchor. However, when I add the URL to the sitemap I get the following error: The 'url' property had a malformed URL This is the offending URL:...
1
1487
by: nj2md | last post by:
Can some one assist with a querry. I need to know the code to querry a database to find the number of female and males that make over 50K a year and how to get capital gains and loses from the same database. Finally the code to count by occupation each country in the database. I Thanks in advance. I hope that this is enough data to assist...
0
1266
by: getmeidea | last post by:
I have the following tables, 1> employee_master(emp_id int primary key, emp_name varchar(100)); 2> employee_salary_payment(salary_rid int primary key, emp_id int, sal_date date, paid_amt int); The tables, employee_master and employee_salary_payment have one to many relation. I need to list the salary payment done for the employee having id...
2
1448
by: dipalichavan82 | last post by:
i came across a article, where it was mentioned if we want a dynamic querry to fire then use parameterized querry e.g. string inputcity=textbox.text; SqlCommand cmd = new SqlCommand("select * from Customers where city= '" + inputCity + " ' "; Don't ever build a query this way! as this leads to hacking. instaed do it like this:
0
7521
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7720
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. ...
1
7473
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
7810
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
6044
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
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
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
1061
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.