From: Jan-Benedict Glaw <jbglaw@lug-owl.de>
To: linux-c-programming@vger.kernel.org
Subject: Re: Antwort: Re: -EFAULT during freeing a pointer to a structure
Date: Fri, 8 Oct 2004 13:31:01 +0200 [thread overview]
Message-ID: <20041008113101.GK5033@lug-owl.de> (raw)
In-Reply-To: <OFFFC2B2B9.78606041-ONC1256F27.002EEFD1@bln.d-trust.de>
[-- Attachment #1: Type: text/plain, Size: 2604 bytes --]
On Fri, 2004-10-08 11:03:55 +0200, p.boehm@d-trust.net <p.boehm@d-trust.net>
wrote in message <OFFFC2B2B9.78606041-ONC1256F27.002EEFD1@bln.d-trust.de>:
> (it's written from mind, hope there are no spelling mistakes)
There's one, but not that bad...
> # --------------------------------------------------------- #
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #define MAXNUM 7
>
> struct xy {
> int x;
> char *y;
> };
>
> struct abc {
> struct xy *next[MAXNUM];
So here you're creating an array of pointers to "struct xy". This array
has got MAXNUM elements, this is currently 7. Note that the correct
array subscripts are from 0 to 6!
> };
>
> void init_abc(struct abc *pt, int index) {
> pt->next[index]=malloc(sizeof(struct xy));
> memset(pt->next[index],0,sizeof(struct xy));
> }
>
> void free_abc(struct abc *pt, int index) {
> free(pt->next[index]);
> pt->next[index]=NULL;
> }
>
> int main(int argc, char *argv[]) {
> struct abc *ptr;
> int index=-1;
>
> ptr=malloc(sizeof(struct abc));
> memset(ptr, 0, sizeof((struct abc));
Here's a '(' too much, that's the typo.
>
> while(index++<MAXNUM) {
> init_abc(ptr,index);
> printf("ptr->next[index] = %p\n", ptr->next[index]);
> }
...and now, think about this loop. Think hard. Got the point? It's
executed for index=0 to index=7! That's one too much! I suggest you
better write it like
for (index = 0; index < MAXNUM; index++) {
init_abc (ptr, index);
printf ("ptr->next[%d] = %p\n", index, ptr->next[index]);
}
> while(index-->0) {
> free_abc(ptr,index);
> printf("ptr->next[index] = %p\n", ptr->next[index]);
> }
And I'd write this as
for (index = MAXNUM - 1; index >= 0; index--) {
free_abc (ptr, index);
printf ("ptr->next[%d] = %p\n", index, ptr->next[index]);
}
Additionally, I've also made it print out it's index it's actually
working on. If you had done that on the first hand, you'd for sure seen
your error in no time:-)
> free(ptr);
> return 0;
> }
>
> # ---------------------------------------------------------------------- #
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
prev parent reply other threads:[~2004-10-08 11:31 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-10-08 9:03 Antwort: Re: -EFAULT during freeing a pointer to a structure p.boehm
2004-10-08 11:31 ` Jan-Benedict Glaw [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20041008113101.GK5033@lug-owl.de \
--to=jbglaw@lug-owl.de \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).