problem with pointers in c. and dynamic memory allocation

M

manoharyes

hi experts,
i am getting a segmentation fault error in this piece of code..
node.c has some contents.
my intension is to read the contents of a file into an array then..
dynamically allocate memory to a character pointer to store contents of
that file.. i am doing this because the contents of the file may change
for each "read" operation. and i will use this poiter(dynamically
allocated) for sendto() call.
please help..
thnx in advance.
Xoinki.

Code:
#include<stdio.h>
#include <curses.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <netinet/in.h>

struct packet
{
char arr1[188];
};
struct packet pckt;
int main()
{
FILE *fp;
char arr[90];
char *ptr;
char *ptr2;
int i=0;
struct sockaddr_in addr;
struct hostent *hname;
struct protoent *proto;
int val=1,sd;
int a=0;

proto = getprotobyname("OSPFIGP");
	hname = gethostbyname("192.168.15.1");

bzero(&addr, sizeof(addr));
		addr.sin_family = hname->h_addrtype;
		addr.sin_port = 0;
		addr.sin_addr.s_addr = *(long*)hname->h_addr;

	sd = socket(PF_INET, SOCK_RAW, proto->p_proto);

	if ( sd < 0 )
	{
		perror("socket");
		return;
	}


fp=fopen("node.c","r");
while(!feof(fp))
       {
		     if((arr[a++]=getc(fp))=='\n')
		     {
		     printf("\\n encountered\n");
		     }

		//
          printf("the conetebts are %s\n",arr);
                      //a=strcmp(arr,"127.0.0.1");



                  }
		  ptr2=arr;
		  printf("\n\n hi ther\n");
		  strcpy(pckt.arr1,arr);
		  printf("\n\nnooo\n");
		  fclose(fp);
		  while(pckt.arr1[i]!='\0')
		  {
		  printf("%c",pckt.arr1[i]);
		  i++;
		  }

		  printf("\nthe outside of loop are\n %s\nlent
%d\n",pckt.arr1,strlen(arr));
		  ptr=(char *)realloc(ptr,(strlen(arr)+1));
		  if(ptr==NULL)
		  printf("\nits friggin NULL \n");
		  else
		  {
		  printf("inside else");
		  a=0;
		  i=0;
		  do
		  {
		  ptr[a++]=arr[i++];
		  }while(a!=strlen(arr));
		  i=0;
		  while(ptr[i]!='\0')
		  {
		  printf("\nthe last printy are\n %s\n",ptr[i]);
		  i++;
		  }
		  }
		  
		  free(ptr);
		 
	return 0;
}
 
B

Bill Pursell

hi experts,
i am getting a segmentation fault error in this piece of code..
node.c has some contents.


Where is the segfault happening? It's entirely likely
that the file is too large to fit into the 90 character
array that you are reading it into. Also, you definitely
need to check the return value of fopen(). Thirdly,
you really need to work on formatting your code to
make it fit to post. Fourthly, delete the silly debug
printf()'s before you post (in fact, get in the habit
of not using them at all...learn to use a debugger,
and you will become a healty, productive member
of society). Also, don't use // style
commenting in posts, since word wrap makes it
difficult to read.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top