473,320 Members | 1,707 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,320 software developers and data experts.

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 1554


ra***********@gmail.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***********@gmail.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_Keith) 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***********@gmail.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***********@gmail.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*******@gmail.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
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.


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
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...
2
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...
1
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...
0
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...
5
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 ...
17
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...
1
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...
1
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...
0
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); ...
2
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.