Unix Programming - Issue with shmdt

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2006 > Issue with shmdt





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Issue with shmdt
kimi

2006-10-14, 7:33 am

Hi,
I have a simple issue with using shmdt.

I have a shared memory segment I allocate and attempt to attach to:

/* other code left out. */

#include <sys/shm.h>

int shmid;
int *shmPtr;

/* Allocate memory. */
shmid = shmget(shmKey, 1024, IPC_CREAT | 0666);

/* Attach to shared memory. */
shmPtr = (int *)shmid(sh, 0, 0);

/* Attempt to detach from shared memory. */
shmdt(shmPtr);

When I compile, I get the following:

shmemory.cpp:84: passing `int *' as argument 1 of `shmdt(char *)'

I attempted to cast several different ways as a workaround:

shmdt(int *)(shmPtr); /* wrong */
shmdt((int *)shmPtr);
(int *)shmdt(shmPtr);

Same results each way.

If I look at the man page for shmdt, I think I undersand the reason:
====
void *shmat(int shmid, const void *shmaddr, int shmflg);

Default
int shmdt(char *shmaddr);

Standard conforming
int shmdt(const void *shmaddr);
====

That's okay and I can understand why it's not compiling, but I've seen
other programs compile okay using shmdt on a similar int *.

Is there a workaround?

I am new to UNIX programming and am looking. If there's any good books
anyone could suggest would be appreciated.

Kim

loic-dev@gmx.net

2006-10-14, 7:38 pm

Good Night,

> I am new to UNIX programming and am looking. If there's any good books
> anyone could suggest would be appreciated.


I think, what you really need is a good book about C programming. I
would recommend:
"The C programming Language, 2nd Edition" from Kernighan & Ritchie.

Cheers,
Loic.

Geoff Clare

2006-10-17, 1:25 pm

"kimi" <testcpp@gmail.com> wrote, on Sat, 14 Oct 2006:

> int *shmPtr;


> shmdt(shmPtr);
>
> When I compile, I get the following:
>
> shmemory.cpp:84: passing `int *' as argument 1 of `shmdt(char *)'


> If I look at the man page for shmdt, I think I undersand the reason:
> ====
> void *shmat(int shmid, const void *shmaddr, int shmflg);
>
> Default
> int shmdt(char *shmaddr);
>
> Standard conforming
> int shmdt(const void *shmaddr);
> ====
>
> That's okay and I can understand why it's not compiling, but I've seen
> other programs compile okay using shmdt on a similar int *.


It seems your system provides two different declarations of shmdt()
and the default one is an old one to support legacy code. The
compilations that work on other systems will be because they are
providing the standard declaration.

> Is there a workaround?


Either enable the standard declaration on your system (probably by
compiling with -D_XOPEN_SOURCE=500 or 600) or change your code to
use the cast that the old declaration needs:

shmdt((char *)shmPtr);

--
Geoff Clare <netnews@gclare.org.uk>

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com