473,789 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc realloc and pointers

Hi all,

I m relatively new to C. I have few queries related to malloc():

1. When we perform malloc(), the memory allocated dynamically comes from
the heap area of the process in concern. Well, we then say that the heap
has shrinked. my query is: Is it that the heap physically does not
shrink but the perticular nodes are marked 'ALLOCATED' and for
subsequent calls to malloc() the memory manager remembers them and does
not reference them?

2. With realloc(), if some pointer 'ptr' is pointing initially to a
perticular position in a buffer (char *buffer) then on performing a
realloc() on this buffer, what will be 'ptr' pointing to?

3. whats the maximum memory size that we can allocate dynamically by
calling malloc() ?

4. Is it valid in C to typecast a pointer? eg. code snippet... of
course int is 16 bit and long is 32 bit.
int *variable, value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;
*((long*)variab le)++ = value;

thanx in advance

Nov 29 '07
22 1983
"K. Jennings" <kj*******@resu rgence.netwrite s:
[...]
>ravi wrote:
>>2. With realloc(), if some pointer 'ptr' is pointing initially to a
perticular position in a buffer (char *buffer) then on performing a
realloc() on this buffer, what will be 'ptr' pointing to?
[snip]
What happens when ptr is not one returned by a previous
invocation to malloc(), calloc() or realloc()?
Undefined behavior.

(This is partly a test of news-server.san.rr.c om.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 30 '07 #11
On 29 Nov, 21:44, Peter Nilsson <ai...@acay.com .auwrote:
ravi <nos...@nospam. invalidwrote:
I m relatively new to C.

Then why ask questions on how it's implemented? You're
trying to run before you can crawl.
in theory this is the right answer but some people need
a concrete implementation to hang their thoughts on.

E1: "but you don't need to know the dtails of how that works!"
E2: "yes I do!"
E1: "you wouldn't expect to know the dtails of how the compiler
worked would you?"
E2: "well yes I would actually"
"I understood once I realised it's all shift registers!"
an explanation of the functioning of a digital switch,
that I felt lacked detail.
I have few queries related to malloc():
1. When we perform malloc(), the memory allocated
dynamically comes from the heap area of the process in
concern.

If you say so. The language standard does not specify how
malloc is implemented, merely how it must operate.
again, an outline sketch of an implementation can help.

Well, we then say that the heap has shrinked. my query
is: Is it that the heap physically does not shrink but
the perticular nodes are marked 'ALLOCATED' and for
subsequent calls to malloc() the memory manager
remembers them and does not reference them?
typically, yes (actually pretty well certainly, yes!)

<snip>

--
Nick Keighley
Nov 30 '07 #12
CBFalconer wrote, On 30/11/07 00:42:
ravi wrote:
<snip>
>3. whats the maximum memory size that we can allocate dynamically by
calling malloc() ?

Depends on the system. All you can find out is that a malloc
(etc.) request is fulfilled or rejected.
<snip>

You also know that you cannot allocate more than (size_t)-1 bytes with a
single call to malloc.
--
Flash Gordon
Nov 30 '07 #13

"Nick Keighley" <ni************ ******@hotmail. comwrote in message
news:93******** *************** ***********@s12 g2000prg.google groups.com...
On 29 Nov, 21:44, Peter Nilsson <ai...@acay.com .auwrote:
>ravi <nos...@nospam. invalidwrote:
I m relatively new to C.

Then why ask questions on how it's implemented? You're
trying to run before you can crawl.

in theory this is the right answer but some people need
a concrete implementation to hang their thoughts on.
yes.

in the college where I am going, this has sometimes been referred to as a
difference between platonic and aristotelean thought processes...

E1: "but you don't need to know the dtails of how that works!"
E2: "yes I do!"
E1: "you wouldn't expect to know the dtails of how the compiler
worked would you?"
E2: "well yes I would actually"
"I understood once I realised it's all shift registers!"
an explanation of the functioning of a digital switch,
that I felt lacked detail.

I originally partly learned C by examining lots of assembler output from the
compiler (at first I had found assembler easier to understand).

so, this was my early years:
I started with basic, later moving to assembler.
I then more or less moved from assembler to C.

back in my early days of learning C, there were more than a few things that
I learned from the wolfenstien source as well...

look at source, try to figure out how to imitate it...
then I moved to 32 bit archs, as over the years 16-bit was gradually
replaced by 32-bit, and dos by windows and linux. for a long time after
this, I continued on with a faulty understanding of all that was going on in
assembler land, until more recently when I have gotten much more heavily
involved with assembler again (compiler writing does this...).

and, now, for me much of a lifetime later, we are again seeing a transition,
from 32 to 64 bits...
and I watch as those of us born in the 80s are becomming 'old', being
rapidly replaced by those from the 90s...
a few years back, people born in the 80s were still young, and people born
in the 90s were little kids...

time passes and the distant past becomes ever more detached, as my life
quickly escaped me and I become old and pointless, in a life quickly going
nowhere...

and such...

>
I have few queries related to malloc():
1. When we perform malloc(), the memory allocated
dynamically comes from the heap area of the process in
concern.

If you say so. The language standard does not specify how
malloc is implemented, merely how it must operate.

again, an outline sketch of an implementation can help.
yes.

>
Well, we then say that the heap has shrinked. my query
is: Is it that the heap physically does not shrink but
the perticular nodes are marked 'ALLOCATED' and for
subsequent calls to malloc() the memory manager
remembers them and does not reference them?

typically, yes (actually pretty well certainly, yes!)

<snip>

--
Nick Keighley

Dec 1 '07 #14
cr88192 wrote:

<snip>
I originally partly learned C by examining lots of assembler output
from the compiler (at first I had found assembler easier to
understand).

so, this was my early years:
I started with basic, later moving to assembler.
I then more or less moved from assembler to C.
It was a slightly different sequence for me. My first languages were
BASIC (BASICA) and Pascal. Then I moved on to C and now more recently,
I have also been dabbling in assembler (x86_32).

I also know a fair bit of Java, Perl and shell (sh).

I prefer C or assembler for hobby programming. To hack out a quick
prototype I usually use Perl or bash. Of course BASIC and Pascal have
fallen by the wayside...
back in my early days of learning C, there were more than a few things
that I learned from the wolfenstien source as well...
My approach is a bit different. I read up on a concept and then try my
hand at several "demos" that exercise that. For a long time before
finding this group I did not really pay any attention to portability -
my code used to make heavy use of compiler extensions. Now of course, I
try to keep as much of my code as possible portable.
look at source, try to figure out how to imitate it...
This approach has generally not worked that well for me. I prefer the
textbook and try-it-and-see approach. But to each his own I guess.

<snip>
and, now, for me much of a lifetime later, we are again seeing a
transition, from 32 to 64 bits...
Yes. For the moment I'm still staying with x86_32. The prospect of
learning yet another bunch of instructions and rules is wearysome, but
sooner rather than later I'll have to crack open the x86_64 manuals.
Sometimes I wish Herbert's 68000 series had made it instead of Intel's
IA-32. It'd have so much easier on the grey matter...

<snip>

Dec 1 '07 #15

"santosh" <sa*********@gm ail.comwrote in message
news:fi******** **@registered.m otzarella.org.. .
cr88192 wrote:

<snip>
>I originally partly learned C by examining lots of assembler output
from the compiler (at first I had found assembler easier to
understand).

so, this was my early years:
I started with basic, later moving to assembler.
I then more or less moved from assembler to C.

It was a slightly different sequence for me. My first languages were
BASIC (BASICA) and Pascal. Then I moved on to C and now more recently,
I have also been dabbling in assembler (x86_32).
yeah.
I started out with QBasic...

but, in time one runs into some limitations with basic, and assembler was in
part an escape.
I started learning C as well, but it was an indirect path.

all this was starting partyway through elem (I think mostly starting around
3rd and 4th grade), where I started transitioning to messing with C and
transitioning mostly to assembler sometime around 6th grade.

by 8th grade, I was writing stuff almost purely in C.

I didn't really start making "progress" on things until about the middle or
end of highschool, with the first noteworthy project being a crude OS
(32-bit protected mode, ...). eventually this project died for looking too
much like "yet another unix" (but horribly buggy with lacking driver
support).

and, time has moved on...

many years have gone by since then (ok, it is still a "one hand" number, but
I still feel old and useless...). everything seems so long ago, my life
quickly escaping from me...
by now I should have been presumably independent, with a job and female
interest and so on...
but, it is lame, I am still stuck going to college and females are not
interested in someone like me...
they seemingly all find someone already, yet I am still alone...

I also know a fair bit of Java, Perl and shell (sh).
never really learned Java in much detail (I learned enough to give me a
strong distaste for OO for a number of years).

Perl, never really liked or felt much need for.
'sh', too minor of a thing to really consider much of.

I prefer C or assembler for hobby programming. To hack out a quick
prototype I usually use Perl or bash. Of course BASIC and Pascal have
fallen by the wayside...
yes. I haven't done much of anything in basic for many years.

>back in my early days of learning C, there were more than a few things
that I learned from the wolfenstien source as well...

My approach is a bit different. I read up on a concept and then try my
hand at several "demos" that exercise that. For a long time before
finding this group I did not really pay any attention to portability -
my code used to make heavy use of compiler extensions. Now of course, I
try to keep as much of my code as possible portable.
I primarily learned things by studying and immitating things.
so, it was wolfenstien, doom, quake, and the linux kernel, mostly...

moving past that, I switched primarily to independent projects...

>look at source, try to figure out how to imitate it...

This approach has generally not worked that well for me. I prefer the
textbook and try-it-and-see approach. But to each his own I guess.
books are hard to come by.
parents never really much liked buying books for me (or, at least, ones that
were actually useful).

they were good at getting total introductory/newb books, but I don't really
need books with a bunch of stuff I already know.

<snip>
>and, now, for me much of a lifetime later, we are again seeing a
transition, from 32 to 64 bits...

Yes. For the moment I'm still staying with x86_32. The prospect of
learning yet another bunch of instructions and rules is wearysome, but
sooner rather than later I'll have to crack open the x86_64 manuals.
Sometimes I wish Herbert's 68000 series had made it instead of Intel's
IA-32. It'd have so much easier on the grey matter...
yes, ok.

well, 16-bits was fading when I was back in elem, watching the change in
progress, as the distant past transitions into the present.

I remember when I was back in HS or so and heard about x86-64 (I was like,
cool, a 64 bit chip that there is actually reason to use...), but it took so
long in comming out, me getting one, and am still waiting for good OS
support, ...
so, the world has changed some since when I was young...
slowly but surely the world has changed.

<snip>

Dec 1 '07 #16
cr88192 wrote:
>
.... snip ...
>
and I watch as those of us born in the 80s are becomming 'old',
being rapidly replaced by those from the 90s... a few years back,
people born in the 80s were still young, and people born in the
90s were little kids...

time passes and the distant past becomes ever more detached, as
my life quickly escaped me and I become old and pointless, in a
life quickly going nowhere...
Now try the effect of being born in the early '30s. :-)

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Dec 1 '07 #17

"CBFalconer " <cb********@yah oo.comwrote in message
news:47******** *******@yahoo.c om...
cr88192 wrote:
>>
... snip ...
>>
and I watch as those of us born in the 80s are becomming 'old',
being rapidly replaced by those from the 90s... a few years back,
people born in the 80s were still young, and people born in the
90s were little kids...

time passes and the distant past becomes ever more detached, as
my life quickly escaped me and I become old and pointless, in a
life quickly going nowhere...

Now try the effect of being born in the early '30s. :-)
your case, or hypothetical?.. .
I guess the changes for someone who has lived so long would be far greater
than what I have seen in my 2+ decades of existence (of which I can remember
maybe about 12-15 years of it, and of which I have been coding most of
this...).

well, it will soon be my birthday.
another year older, and I have little to show for it...
well, I wrote a C compiler this year, and wrote a few other things, maybe at
least worth something...

but, in time, my life will end, and I wonder if I will ever have much of
anything to show for having existed.
what of all the people younger than me possible making more impressive
achievements?.. .
one gets overrun and one gets replaced...

like middle or high schoolers or such who can pull off maybe 200 or 300
kloc/year or more?...
maybe they can write impressive stuff, or make interesting discoveries?...

it has been much of a year, and my compiler has not even broken 100 kloc
yet.

taking my average rate calculated earlier this year, and estimating for a
full year, it comes out to about 100kloc/year, which is probably rather
lame...

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Dec 1 '07 #18
cr88192 wrote:
>
"CBFalconer " <cb********@yah oo.comwrote in message
news:47******** *******@yahoo.c om...
>cr88192 wrote:
>>>
... snip ...
>>>
and I watch as those of us born in the 80s are becomming 'old',
being rapidly replaced by those from the 90s... a few years back,
people born in the 80s were still young, and people born in the
90s were little kids...

time passes and the distant past becomes ever more detached, as
my life quickly escaped me and I become old and pointless, in a
life quickly going nowhere...

Now try the effect of being born in the early '30s. :-)

your case, or hypothetical?.. .
<snip>
but, in time, my life will end, and I wonder if I will ever have much
of anything to show for having existed.
what of all the people younger than me possible making more impressive
achievements?.. .
one gets overrun and one gets replaced...

like middle or high schoolers or such who can pull off maybe 200 or
300 kloc/year or more?...
maybe they can write impressive stuff, or make interesting
discoveries?...

it has been much of a year, and my compiler has not even broken 100
kloc yet.

taking my average rate calculated earlier this year, and estimating
for a full year, it comes out to about 100kloc/year, which is probably
rather lame...
Believe it or not you still seem to be _far_ above average with regards
to programming, for someone of such youth. It _very_ impressive for
someone of 25 years of age to have written multiple compilers and GCs.

Dec 1 '07 #19
cr88192 wrote:
>
.... snip ...
>
I remember when I was back in HS or so and heard about x86-64 (I
was like, cool, a 64 bit chip that there is actually reason to
use...), but it took so long in comming out, me getting one, and
am still waiting for good OS support, ...
Well, it looks like you are using Microsoft junk, and they will
never produce a good OS. I suspect that moving to Linux will
rapidly satisfy you.
</BLAST>

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 1 '07 #20

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

Similar topics

9
4031
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the construct ptr = realloc(ptr, size); *ptr = malloc(string_length); strncpy(ptr, src, string_length); to call realloc() multiple times. This should be ok, right?
10
9042
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i think!!). But I want to use malloc. Each string is at most 50 characters long, and there may be zero to thousands of lines. How do I actually start the array? I have seen char **array etc. At first I tried char *array but I think that gives 50...
7
2217
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ http://www.eskimo.com/~scs/C-faq/faq.html but it doesn't seem to answer my questions... So, I've made an example behind, with some included questions...
50
2871
by: Joseph Casey | last post by:
Greetings. I have read that the mistake of calling free(some_ptr) twice on malloc(some_data) can cause program malfunction. Why is this? With thanks. Joseph Casey.
27
1940
by: ncf | last post by:
Hi all. In another topic, I was informed that I had to dynamically allocate memory instead of just trying to expand on a list. (I'm trying to learn C, and have a strong background in PHP and Python) In light of that, I have been trying to learn malloc, realloc, and free, but to no avail. But for some reason, I'm getting segfaults right and left, and to be honest, I am not having any luck at all really in finding out why it isn't...
21
470
by: ramu | last post by:
Hi, Will the memory allocated by malloc and realloc be contiguous? regards
82
31159
by: quiberon2 | last post by:
Hi, Sorry if it might be a stupid question but what should returns malloc(0) ? void *ptr = malloc(0); I am running gcc 3.3.5 and a non-null address is returned. ( in the compiler that I am currently implementing, NULL is returned. Is it wrong ?)
17
1722
by: Christopher Benson-Manica | last post by:
Some recent posts got me thinking about how one might have dealt with simplistic malloc() implementations which might return NULL for a 64K request but might accept two 32K requests or four 16K requests. (I'm assuming, perhaps incorrectly, that quality modern implementations will coalesce free space as necessary and if possible to satisfy requests.) I came up with the following (compilable but untested) first cut at code to try a...
35
5688
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure aren't in my C books. Interesting functions by they're OT here but this raises to me a question. If a function returns a pointer to a void and and as it's first parameter a pointer to a void. Then should that function's first parameter accept a string...
0
9511
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5417
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.