linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* 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

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).