Struct pointer element signal

Discussion in 'Mac Programming' started by Miglu, Mar 2, 2011.

  1. Miglu macrumors member

    Joined:
    Jan 22, 2010
    #1
    I have this struct:
    Code:
    typedef struct node {
    	CGPoint current;
    	CGPoint next;
    } node;
    This is in the implementation:
    Code:
    	CGPoint location;
    	location.x = [theEvent locationInWindow].x;
    	location.y = [theEvent locationInWindow].y;
    	struct node* noud;
    	noud->current = location;
    EXC_BAD_ACCESS occurs at the last line. What is the problem?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    You didn't allocate a node sized area of memory for noud to point to. You need to set it to point to a local node with & or get some memory with malloc.

    -Lee
     
  3. Miglu thread starter macrumors member

    Joined:
    Jan 22, 2010
    #3
    Thanks. Is there a way to create a local node and set noud to point to it in one line?
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    No, but there's a way to do it in two.

    Code:
    struct node myNode;
    struct node *noud = &myNode;
    
    I'm not going to try to compile this, but this might work?

    Code:
    struct node myNode, *noud = &myNode;
    
    I would never do that, but it could work.

    -Lee
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    If you going to create a local node that doesn't live beyond the current function, you don't need a pointer at all.

    Code:
    CGPoint location;
    location.x = [theEvent locationInWindow].x;
    location.y = [theEvent locationInWindow].y;
    struct node noud;
    noud.current = location;
    
    This also has the advantage of not having to allocate memory for noud, and having to remember to free it at some point.
     
  6. SidBala macrumors 6502a

    Joined:
    Jun 27, 2010
    #6
    Just wanted to address the point about using goto instead of loops.

    Gotos(JMP) are actually used internally by the compiler to create loops anyway. So there is no reason you would want to eliminate a loop by using a goto. It is not any faster and lot less readable.

    Of course that doesn't mean that there are no benefits to eliminating loops. But those usually involve removing the iteration itself and unrolling the whole loop.
     
  7. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #7
    By the way, from the name you gave your struct and its members, it seems you are trying to implement a linked list of CGPoints. Note that your "next" member should then not be of type CGPoint but of type pointer-to-node.
     
  8. Miglu thread starter macrumors member

    Joined:
    Jan 22, 2010
    #8
    You are right. I changed next to a node called nextNode.
    I have a new problem with this same implementation. It is off topic, but I will save space on the forum by asking it here:
    Code:
    [nodes addObject: [NSData dataWithBytes:noud length:sizeof(noud)]];
    
    const struct node* noud = [[nodes objectAtIndex: i] bytes];
    Those lines are in different methods. The node's location CGPoint's x an y are not zero in the first line, and the nextNode is not 0x0. However, when I get the bytes back in the second line, y is 0 and nextNode is 0x0. What causes this?
     
  9. Miglu thread starter macrumors member

    Joined:
    Jan 22, 2010
    #9
    Instead of
    Code:
    typedef struct node {
            CGPoint current;
    	struct node* nextNode;
    } node;
    if I do
    Code:
    typedef struct node {
    	struct node* nextNode;
    	CGPoint current;
    } node;
    nextNode is not nulled, but current is. Why is the latter data in this struct always destroyed by NSData?
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    sizeof(noud) is the size of a pointer. You need the sizeof(struct node) or sizeof(*noud).

    -Lee
     

Share This Page