accessing member in struct = segmentation

This is a discussion on accessing member in struct = segmentation within the C Programming forums, part of the General Programming Boards category; Here is the struct: Code: struct circle_t { int x, y, r; int color; int xs, ys; int dx, dy; ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    34

    accessing member in struct = segmentation

    Here is the struct:

    Code:
    struct circle_t {
        int x, y, r;           
        int color;          
        int xs, ys;          
        int dx, dy;          
    };
    function protocol

    Code:
    struct circle_t * circle_init(int new_x, int new_y, int new_radius, int new_color, int new_xs, int new_ys);
    function call in main function

    Code:
    for (i = 0; i < 10; i++)  
                circles[i] = circle_init(rand() % SCREEN_W, rand() % SCREEN_H, rand() % 25, rand() % 256, rand() % 5, rand() % 5);
    function definition

    Code:
    struct circle_t * circ_init;
    Code:
    circ_init->x = new_x;
    program works up to here

    As soon as it goes to this line:

    Code:
    circ_init->y = new_y;
    I get a segmentation fault
    Last edited by subflood; 11-16-2004 at 05:17 PM.
    ~flood

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Make sure you alloc enough memory.
    Code:
    struct circle_t * circle_init(int new_x, int new_y, int new_radius, int new_color, int new_xs, int new_ys){
        struct circle_t *ptr=malloc(sizeof(struct circle_t));
        ptr->x=new_x;
    /*..........*/
        return ptr;
    }
    It seems that you're doing this:
    Code:
    struct circle_t *ptr=malloc(sizeof(struct circle_t*));
    Since traditionally now-a-days pointers are 32bits long so as ints in most compiler implementations, x will be in you memory block but y will not.
    Last edited by xErath; 11-16-2004 at 05:19 PM.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    34
    I always thought that when you declare a variable of a type struct it will automatically allocate the memory.
    ~flood

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Did you initialize circ_init?

    You defined circ_init to be a pointer to structure circle_t. In order for your pointer to exist, you have to assign memory to it. Since this is the C area, you can use malloc() and free() to assign and free a memory block to your pointer.

    A pointer is a variables that contains the address of a variable.

    The function malloc() is within the C standard, and can be included into your program by including the header file stdlib.h. This function simply requests the allocation of a block of size bytes of memory. In some cases, malloc() returns a block in the void * data type. In order for your compiler to recognize the block to your structure circle_t you can type cast malloc(). It all depends on the situation. You can check out the FAQ for more info on that, or read a funny story about it.

    You could simply allocate memory to your structure as the following:
    Code:
    struct circle_t *circ_init;
    // Allocate
    circ_init = (struct circle_t *) malloc (sizeof(struct circle_t));
    // Free
    free(circ_init);
    If you have further questions, please feel free to ask.

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    34
    I just looked throught my book about C and it was there, I can't believe I forgot this.

    thanks guys
    ~flood

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by subflood
    I always thought that when you declare a variable of a type struct it will automatically allocate the memory.
    This is your structure 32 bytes long
    Code:
    struct circle_t circle1;
    circle1.x = ...;
    circle1.y = ...;
    /*....*/
    A pointer 4 bytes long to a struct
    Code:
    struct circle_t *circle1;
    circle1 = malloc(sizeof(struct circle_t));
    circle1->x = ...;
    circle1->y = ...;
    /*....*/
    The pointed memory block IS 32 bytes long.

    Note that type size vary with compiler implementation, but the stated sizes are the most used now-a-days by compilers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  4. Replies: 2
    Last Post: 04-22-2008, 01:07 PM
  5. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM

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