* Antwort: Re: -EFAULT during freeing a pointer to a structure
@ 2004-10-08 9:03 p.boehm
2004-10-08 11:31 ` Jan-Benedict Glaw
0 siblings, 1 reply; 2+ messages in thread
From: p.boehm @ 2004-10-08 9:03 UTC (permalink / raw)
To: Suciu Flavius; +Cc: linux-c-programming
Hi,
while allocating memory I didn't have an -EFAULT. So it couldn't be an vector limit
problem. It's while freeing! (see 'NOTE:' for special cases)
My problem-decription from mail at Sep 05:
>If I allocate memory for entry-structure with malloc() I get an -EFAULT
> a) if I try to free the first allocated structure
> b) if I try to free the entry-structure
>
>example output:
>
>[0] allocated.
>[1] allocated.
>...
>[7] allocated.
>[7] freed.
>[6] freed.
>...
>[1] freed.
>Segmentation Fault
>If I use otherwise '&'-operator to get the address of structure I don't get
>this error. This error is normally produced if I try to access a invalid
>memory-area or address. But here I try to free a cleanly allocated area!
>Why it is so?
I've solved that by taking an even value for MAXNUM. But I doesn't understand
why the error could happened.
NOTE: There are two errors:
1) using &-operator for ptr there is no seg.fault
#define MAXNUM 7
struct abc ptt, *ptr;
ptr=&ptt;
[-] free(ptr);
2) using multiple of 4 in MAXNUM there is no seg.fault
#define MAXNUM 8
struct abc *ptr;
ptr=malloc(sizeof(struct abc));
Take this short example:
(it's written from mind, hope there are no spelling mistakes)
# --------------------------------------------------------- #
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNUM 7
struct xy {
int x;
char *y;
};
struct abc {
struct xy *next[MAXNUM];
};
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));
while(index++<MAXNUM) {
init_abc(ptr,index);
printf("ptr->next[index] = %p\n", ptr->next[index]);
}
while(index-->0) {
free_abc(ptr,index);
printf("ptr->next[index] = %p\n", ptr->next[index]);
}
free(ptr);
return 0;
}
# ---------------------------------------------------------------------- #
Thanks in advance.
pb
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Antwort: Re: -EFAULT during freeing a pointer to a structure
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
0 siblings, 0 replies; 2+ messages in thread
From: Jan-Benedict Glaw @ 2004-10-08 11:31 UTC (permalink / raw)
To: linux-c-programming
[-- 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 --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2004-10-08 11:31 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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).