problem with pointers in c. and dynamic memory allocation

Discussion in 'C Programming' started by manoharyes@gmail.com, May 23, 2006.

  1. Guest

    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;
    }
    
    
    
     
    , May 23, 2006
    #1
    1. Advertising

  2. Bill Pursell Guest

    wrote:
    > 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.
     
    Bill Pursell, May 23, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    439
  2. Replies:
    5
    Views:
    380
    -berlin.de
    Dec 24, 2004
  3. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    756
    Eric Sosman
    Mar 22, 2005
  4. Ken
    Replies:
    24
    Views:
    3,946
    Ben Bacarisse
    Nov 30, 2006
  5. chris
    Replies:
    6
    Views:
    1,031
    chris
    Oct 28, 2005
Loading...

Share This Page