Segfaults - again

This is a discussion on Segfaults - again within the C Programming forums, part of the General Programming Boards category; Hi, I just had a go at starting on a program. It makes 2 points which seems to work until ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Segfaults - again

    Hi, I just had a go at starting on a program. It makes 2 points which seems to work until I try and print the data at which point it segfaults. Heres my main:
    Code:
    #include <stdio.h>	
    #include "vector.h"
    
    int main()
    {
    	Polygon *line;
    	NewLine(line, 10, 10, 20, 20);
    	PrintPoints(line);
    	DeletePolygon(line);
    	return 0;
    }
    vector.h:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define true 1
    #define false 0
    
    #ifndef Uint8
    	#define Uint8 unsigned char
    #endif 
    #ifndef Uint32
    	#define Uint32 unsigned long
    #endif 
    
    
    #define POLYGON_IS_SPLINE 128
    #define POLYGON_SPLINE_IS_CUBIC 64		//Must be spline to be cubic
    #define POLYGON_IS_CLOSED 32
    #define POLYGON_IS_FILLED 16 			//Must be closed to be filled and have size > 2
    
    typedef struct
    {
    	int x, y;	  // Point position
    	float vx, vy; // Independant point velocity 
    }Point;
    
    typedef struct
    {
    	Uint8 flags;  
    	Uint32 size;
    	Point *point; // List of points
    	float vx, vy; // Polygon velocity
    }Polygon;
    
    int DeletePolygon(Polygon *p);
    int NewPoint(Point *p, int x, int y);
    int NewLine(Polygon *p, int x1, int y1, int x2, int y2);
    void PrintPoints(Polygon *poly);
    and vector.c:
    Code:
    #include "vector.h"
    
    int NewPoint(Point *p, int x, int y)
    {
    	p = malloc(sizeof(Point));
    	if(p == NULL) return false;	
    	p->x = x; p->y = y;
    	return true;
    }
    
    
    int NewLine(Polygon *p, int x1, int y1, int x2, int y2)
    {
    	p = malloc(sizeof(Polygon));
    	if(p == NULL) return false;
    	p->size = 2;	
    	p->flags = 0;
    	p->point = malloc(sizeof(int*)*2);
    	Point *temp = p->point;
    	if( NewPoint(temp, x1, y1) == false) 
    	{
    		free(p);
    		return false;
    	}
    	temp++;
    	if( NewPoint(temp, x2, y2) == false)
    	{
    		free((--temp));
    		free(p);
    		return false;
    	}
    	return true;
    }
    
    void PrintPoints(Polygon *poly)
    {
    	Point *p = poly->point;
    	for(int i=0; i<poly->size; i++, p++)
    		printf("%c(%d, %d)\n", 'A'+i, p->x, p->y);
    }
    
    
    int DeletePolygon(Polygon *p)
    {
    	Point *temp = p->point;
    	for(int i=0; i < p->size; i++, temp++)
    		free(temp);	
    	return true;
    }
    Its segfauting somewhere in the red, but that could be because I havent created my structs propery or something. What am I doing wrong?

    Cheers.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yeah, this is because of a basic concept I think you already know, but just fail to apply here.

    Code:
    Polygon *line;
    The line points nowhere in particular. You are trying to allocate it with your next line:

    Code:
    int NewLine(Polygon *p, int x1, int y1, int x2, int y2);
    Problem is that memory allocation in NewLine() will NOT be reflected back in main(). This is the same principle as if you did this:

    Code:
    ....
    int x = 5;
    
    setZero(x);
    
    /* x will still be 5, even if setZero() sets its argument to 0 */
    If you want changes in a variable to be reflected in the calling function, you must pass its address. This means you must pass a Polygon **. So your code should be like this:

    Code:
    Polygon *line;
    NewLine(&line, 10, 10, 20, 20);
    Code:
    int NewLine(Polygon **p, int x1, int y1, int x2, int y2)
    {
    	*p = malloc(sizeof(Polygon));
    	if(*p == NULL) return false;
    	...

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Whoops, yeah I have been here before, lol. Thanks MacGuyver; I added the extra pointy bit and it works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy() segfaults
    By Kaiser in forum C++ Programming
    Replies: 8
    Last Post: 09-29-2007, 07:17 PM
  2. Segfaults
    By mike_g in forum C Programming
    Replies: 3
    Last Post: 08-16-2007, 12:47 PM
  3. Progress bar test segfaults
    By w00tw00tkab00t in forum C Programming
    Replies: 7
    Last Post: 07-01-2006, 10:38 AM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 10:03 AM
  5. STRTOK SegFaults!
    By DarthKoRn in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 03:29 PM

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