Browsing through a Pointer to Array of Structures

This is a discussion on Browsing through a Pointer to Array of Structures within the C Programming forums, part of the General Programming Boards category; Hello, I'm working on some wacky shared memory using application for Unix. Here is the thing, I use shmget and ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    Browsing through a Pointer to Array of Structures

    Hello, I'm working on some wacky shared memory using application for Unix.

    Here is the thing, I use shmget and shmat to create and attach a shared memory block, but making it an array of structures.

    The structure:
    Code:
    struct object{
       char nick[16];
       pid_t pid;
       key_t queue;
    } *mem, *p;
    SHM declaration:
    Code:
    if( (mem = (struct object*)shmat((key_t)argv[1], (void*)0, 0)) == (void*)-1 )        {
                      shmget((key_t)argv[1], SHMSIZE*sizeof(struct object), IPC_CREAT | 0666);
                      mem = (struct object*)shmat( (key_t)argv[1], (void*)0 , 0);
            }
    And now the problematic part. I'm using this loop to browse through this thing.
    Code:
    for(p = mem; p< &mem[SHMSIZE]); p++)        {
                if(!p->nick)
                {
                    key = ftok(".", randomize());  //randomize just provides a random integer from dev/random
                    msgid = msgget(key, IPC_CREAT);
    
    
                    p->nick = getenv("USERNAME");
                    printf("%s", p->nick);
                    p->pid = getpid();
                    p->queue = msgid;
                }
                printf("lalala\n");
            }
    But this one just doesn't work. Any suggestions please?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Can you run the code in gdb, and give us a more precise diagnostic than "it doesn't work".

    > p->nick = getenv("USERNAME");
    Maybe it's a compile problem, because nick is an array.
    So perhaps use strcpy() here instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    Well I tested the following:

    Code:
    for(p = mem; p< &mem[SHMSIZE]); p++)         {
                 printf("lalala\n");
             }
    also:

    Code:
    for(p = mem; p<=(mem+1023); p++)         {
                 printf("lalala\n");
             }
    neither did anything.

    Ah, the code compiles just fine with -Wall.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    if( (mem = (struct object*)shmat((key_t)argv[1], (void*)0, 0)) == (void*)-1 )        {
                      shmget((key_t)argv[1], SHMSIZE*sizeof(struct object), IPC_CREAT | 0666);
                      mem = (struct object*)shmat( (key_t)argv[1], (void*)0 , 0);
            }
    Shouldn't you first call shmget() to get an id and then use that id as the first parameter for shmat()?
    Code:
    if ((id = shmget(key, size, flags)) != -1)
        mem = (struct object *) shmat(id, NULL, 0);
    Bye, Andreas

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    333
    Quote Originally Posted by ryder052 View Post
    But this one just doesn't work. Any suggestions please?
    The statement if(!p->nick) will never trigger if nick is a character array. You probably mean if(!strlen(p->nick));
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > for(p = mem; p< &mem[SHMSIZE]); p++)
    > Ah, the code compiles just fine with -Wall.
    Well the extra ) in this line would suggest otherwise.

    Coupled with other mistakes (like your assignment and comparison mistakes with an array), I'm just going to wait until you post your real code, and not something you hacked in the post edit window.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed with complex array of pointer to structures
    By zahid990170 in forum C Programming
    Replies: 3
    Last Post: 06-14-2011, 01:46 PM
  2. Passing array of structures by pointer
    By edharvey in forum C Programming
    Replies: 7
    Last Post: 04-13-2011, 02:58 AM
  3. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 10:01 AM
  4. pointer to array of structures
    By strokebow in forum C Programming
    Replies: 14
    Last Post: 12-09-2006, 01:14 PM
  5. A pointer to a member of an array of structures
    By ChwanRen in forum C Programming
    Replies: 3
    Last Post: 05-08-2003, 03:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21