Jump to content

segfault hell

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
blackeye

blackeye

    Newbie

  • Members
  • Pip
  • 1 posts
Hey all, I've been working on this project that is supposed to dynamically allocate an array of pointers to structures then sort and print them. However I've been having a hell of a time finding out where I'm segfaulting. it runs through the loop find for about the first 4 times but then is spazzes out!


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "Funcs.h"





int main()

{

 info **stru;

 info charlie;

 int el=0;

 int allo=0;

 int count=0;

  while(*charlie.Name!='}'){

 charlie=getstuff(&allo,&el);

 

 Add(&charlie,stru,&allo,&el);


}

 love(stru, &el);

 system("PAUSE");	

  return 0;

}






 typedef struct info{      

 char *Name;        

 char *Address;

 char *Loc;

 int zip;               

}info;


void love(info**,int*);

info getstuff(int*, int*);

void Add(info*, info**, int*, int*);




#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include"Funcs.h"



 void love(info **zips,int *length){

      info thing=**zips;

      // printf("%s\n",thing.Name);

       int c=0;

       char *n;

       char *a;

       char *l;

       int z;

       

       for(c=0;c<=(*length);c++){

       thing=*zips[c];

       n=thing.Name;

       a=thing.Address;

       l=thing.Loc;

       z=thing.zip;

      //printf("%s \n",n);

     // printf("%s \n",a);

      //printf("%s \n",l);

      //printf("%d \n",z);                       

       }

       

       }

info getstuff(int *allocated,int *element){

     info Anon;

     Anon.Name=malloc(40*(sizeof(char)));

     Anon.Address=malloc(60*(sizeof(char)));

     Anon.Loc=malloc(40*(sizeof(char)));

     

     fgets(Anon.Name,60,stdin);

     if(strlen(Anon.Name)<3)

     fgets(Anon.Name,60,stdin);

     fgets(Anon.Address,60,stdin);

     fgets(Anon.Loc,60,stdin);

     scanf("%d",&Anon.zip);

                           return Anon;

     }

     

void pushstuff(int n, info **list){

     

     

     }

void Add( info *object, info **thearray, int *alloc, int *ele )

    

{

	printf("%s \n %s \n %s \n %d ",*object);

	if ( (*alloc) <= 50)

	{

		(*alloc)++;  

		

	}

	else

	{

		printf( "50 max! \n" );

		return;

	}

	

	

 

	*thearray = (info*)realloc(*thearray,(*alloc) * (sizeof(info)));

    

	printf("ELEMENT %d \n",(*ele));

	printf("ARRAY %d \n",*thearray[*ele]);

	*thearray[(*ele)] = *object;

	

	

	

	(*ele)++;

	

	

	

	

}



thanks in advance!

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
You give your functions bizarre names, just like my CS instructors. :)

You've got problems galore, my friend. You never allocate memory to any of your pointers. I don't see a single malloc call in there. When you declare a pointer, you need to make it point somewhere. It doesn't allocate itself.

Wrong way:
int *i; //not pointing anywhere right now...

*i = 5;  //BOOM - segfault

Right way:
#include <stdlib.h>
int *i;

//allocate memory...
i = (int *)malloc(sizeof(int));
*i = 5;
//do some stuff...
free(i);  //free when done