Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!mips!spool.mu.edu!think.com!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!hp4nl!targon!andre From: andre@targon.UUCP (andre) Newsgroups: comp.unix.wizards Subject: Re: Pointers in Shared Memory Keywords: pointers, shared memory Message-ID: <1696@targon.UUCP> Date: 23 Apr 91 20:57:53 GMT References: <2417@taurus.BITNET> Organization: Siemens Nixdorf Informationsystems BV.,SWZ, Vianen, the Netherlands. Lines: 52 In article <2417@taurus.BITNET> writes: >I have some doubts concerning the use of pointers inside shared memory >obtained with SysV IPC functions. >My intention is to have a linked list in shared memory. For that >I run a test program that showed me that is not possible to store >a pointer obtained from malloc in shared memory, also I saw that >is not possible to store pointers to the user address space. Well... it is posible, put meaningless the pointer then points to a part of somebody else's data (even if your program has that address in its memory map. and as you know, you need shared memory for this kind of thing. >However, it is possible to store pointers INSIDE the shared memory, >and also to another shared memory segment. Again... this is possible, but BEWARE ! if you let the kernel (together with the kernel varaible SHMBRK) choose the address to start the segment, it will be likely in different places for different non-trivial programs. Remember, malloc() changes the sbrk() value relative to where the kernel starts your shared memory segment. If you want to keep linked lists in shared memory, you must keep the list elements also in shared memory and in stead of pointers you must use offsets to the beginning of the shared memory. If you only have to point to the list elements you can build your shared memory like: struct info { int num_elsms; int first_elem }; struct elem { char data[MAX]; int next}; struct info *list_info = (address of shared mem); struct elem *list_data = (address of shared mem) + sizeof struct info; now if you want to follow the list you use: { struct elem *ep; for (ep = list_data + list_info->first_elem; ep != list_data ; ep = list_data + ep->next) { printf("Data is %s\n",ep->data); } } -- The mail| AAA DDDD It's not the kill, but the thrill of the chase. demon...| AA AAvv vvDD DD Ketchup is a vegetable. hits!.@&| AAAAAAAvv vvDD DD {nixbur|nixtor}!adalen.via --more--| AAA AAAvvvDDDDDD Andre van Dalen, uunet!hp4nl!targon!andre