malloc and free

K

Kamus of Kadizhar

When is malloc'd memory free'd by default?

Let's say I have two arrays:

#define MAXLEVEL 10
char *letters_array[MAXLEVEL];
char **keyMap;

and later I do something like:

void somefunc(xxxx) {

keyMap = malloc((sizeof (char *))*MAXLEVEL);
for (i = 0; i < MAXLEVEL; i++) {
letters_array = malloc(1000);
keyMap = malloc(1000);
}
}

How do I free the allocated memory?

Do I need to loop through like so:
void someotherfunc(xxxx) {
for (i = 0; i < MAXLEVEL; i++) {
free(letters_array);
free(keyMap);
}
free(keyMap);
}

or will the memory be freed when execution passes outside of the scope
where letters_array and keyMap are defined? Or is that when I start
leaking memory?

--Kamus
 
C

Christopher Benson-Manica

Kamus of Kadizhar said:
When is malloc'd memory free'd by default?

Never. If you don't do it, it isn't freed (at least not before the
program exits).
Do I need to loop through like so:
Yes.

void someotherfunc(xxxx) {
for (i = 0; i < MAXLEVEL; i++) {
free(letters_array);
free(keyMap);
}
free(keyMap);
}

or will the memory be freed when execution passes outside of the scope
where letters_array and keyMap are defined? Or is that when I start
leaking memory?

It's when you start leaking memory.
 
E

E. Robert Tisdale

Kamus said:
Let's say I have two arrays:

#define MAXLEVEL 10
char *letters_array[MAXLEVEL];
char **keyMap;

and later I do something like:

void somefunc(void) {
keyMap = (char**)malloc((sizeof(char*))*MAXLEVEL);
keyMap[0] = (char*)malloc(1000*MAXLEVEL);
letters_array[0] = (char*)malloc(1000*MAXLEVEL);
for (i = 1; i < MAXLEVEL; ++i) {
letters_array = letters_array[i-1] + 1000;
keyMap = keyMap[i-1] + 1000;
}
}

How do I free the allocated memory?
Do I need to loop through like so:

void someotherfunc(void) {

free(letters_array[0]);
free(keyMap[0]);
free(keyMap);
}

No loop is required.
Or will the memory be freed when execution passes outside of the scope
where letters_array and keyMap are defined?
No!

Or is that when I start leaking memory?

Yes.
 
R

Robert Harris

Kamus said:
When is malloc'd memory free'd by default?

Let's say I have two arrays:

#define MAXLEVEL 10
char *letters_array[MAXLEVEL];
char **keyMap;

and later I do something like:

void somefunc(xxxx) {

keyMap = malloc((sizeof (char *))*MAXLEVEL);
for (i = 0; i < MAXLEVEL; i++) {
letters_array = malloc(1000);
keyMap = malloc(1000);
}
}

How do I free the allocated memory?

Do I need to loop through like so:
void someotherfunc(xxxx) {
for (i = 0; i < MAXLEVEL; i++) {
free(letters_array);
free(keyMap);
}
free(keyMap);
}

Yes.
 
J

Jack Klein

Kamus said:
Let's say I have two arrays:

#define MAXLEVEL 10
char *letters_array[MAXLEVEL];
char **keyMap;

and later I do something like:

void somefunc(void) {
keyMap = (char**)malloc((sizeof(char*))*MAXLEVEL);

There you go again, brain dead, deliberately misquoting other people's
posts without warning or notice.

The OP's post DID NOT CONTAIN the line directly above these comments
of mine, despite the fact that you left the quote indicator in place.

In face what the OP actually posted is this:
keyMap = malloc((sizeof (char *))*MAXLEVEL);

It's one thing to disagree with the majority of posters here. It is
quite another to deliberately misquote other people. You are
intellectually dishonest.

In general, you are a lying sack of excrement and a troll. I have
resisted filtering you because occasionally you post something mildly
amusing, and very, very occasionally you post something correct, most
likely by accident.

But enough already, welcome to my kill file.

*plonk*
 
C

CBFalconer

Jack said:
Kamus said:
Let's say I have two arrays:

#define MAXLEVEL 10
char *letters_array[MAXLEVEL];
char **keyMap;

and later I do something like:

void somefunc(void) {
keyMap = (char**)malloc((sizeof(char*))*MAXLEVEL);

There you go again, brain dead, deliberately misquoting other
people's posts without warning or notice.

The OP's post DID NOT CONTAIN the line directly above these comments
of mine, despite the fact that you left the quote indicator in place.

In face what the OP actually posted is this:
keyMap = malloc((sizeof (char *))*MAXLEVEL);

It's one thing to disagree with the majority of posters here. It
is quite another to deliberately misquote other people. You are
intellectually dishonest.

In general, you are a lying sack of excrement and a troll. I have
resisted filtering you because occasionally you post something
mildly amusing, and very, very occasionally you post something
correct, most likely by accident.

But enough already, welcome to my kill file.

*plonk*

I have no disagreement with your evaluation, however I think it is
necessary to keep reading Tisdales trolls simply to protect
against such cowardly pernicious alterations. I never find them
amusing, however.

I wonder if complaints to nasa.gov would be worthwhile? With all
the criticism they have been receiving they certainly don't need
Trollsdale to appear to be their spokesman. I just have
hesitations about putting anyones livlihood in danger, even it.
 
C

Christopher Benson-Manica

CBFalconer said:
I wonder if complaints to nasa.gov would be worthwhile? With all
the criticism they have been receiving they certainly don't need
Trollsdale to appear to be their spokesman. I just have
hesitations about putting anyones livlihood in danger, even it.

There are two possibilities here - either ERT really does work for
NASA, in which case one might prefer that he no longer have anything
to do with such major undertakings as space shuttle launches, or he is
masquerading as their employee, which I would think would absolve
anyone who complained of all guilt if ERT lost his job as a result.

You'd be doing Usenet a service, IMHO.
 
C

Christian Bau

Christopher Benson-Manica said:
There are two possibilities here - either ERT really does work for
NASA, in which case one might prefer that he no longer have anything
to do with such major undertakings as space shuttle launches, or he is
masquerading as their employee, which I would think would absolve
anyone who complained of all guilt if ERT lost his job as a result.

You'd be doing Usenet a service, IMHO.

And some jobless janitor might be pleased about a new position at NASA...

Seriously, I would think that trying to find someone's employer and
notifying them would be quite nasty behavior. On the other hand, someone
working for a competitor of my company posted some stuff on one of the
newsgroups that I read which gave me more insight into their development
processes than his managers probably would have liked, so nobody should
think that they are anonymous.
 
C

CBFalconer

Christian said:
And some jobless janitor might be pleased about a new position at
NASA...

Seriously, I would think that trying to find someone's employer and
notifying them would be quite nasty behavior. On the other hand,
someone working for a competitor of my company posted some stuff on
one of the newsgroups that I read which gave me more insight into
their development processes than his managers probably would have
liked, so nobody should think that they are anonymous.

I agree with you, which is why I am so hesitant to do anything of
the sort. It only counts if he really is a jpl employee in the
first place. On the other hand, he has been pulling all sorts of
mean spirited evil tricks, besides abysmal ignorance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top