470,841 Members | 1,089 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

problem with pointers in c. and dynamic memory allocation

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.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include <curses.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <sys/socket.h>
  6. #include <resolv.h>
  7. #include <netdb.h>
  8. #include <netinet/in.h>
  9.  
  10. struct packet
  11. {
  12. char arr1[188];
  13. };
  14. struct packet pckt;
  15. int main()
  16. {
  17. FILE *fp;
  18. char arr[90];
  19. char *ptr;
  20. char *ptr2;
  21. int i=0;
  22. struct sockaddr_in addr;
  23. struct hostent *hname;
  24. struct protoent *proto;
  25. int val=1,sd;
  26. int a=0;
  27.  
  28. proto = getprotobyname("OSPFIGP");
  29. hname = gethostbyname("192.168.15.1");
  30.  
  31. bzero(&addr, sizeof(addr));
  32. addr.sin_family = hname->h_addrtype;
  33. addr.sin_port = 0;
  34. addr.sin_addr.s_addr = *(long*)hname->h_addr;
  35.  
  36. sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
  37.  
  38. if ( sd < 0 )
  39. {
  40. perror("socket");
  41. return;
  42. }
  43.  
  44.  
  45. fp=fopen("node.c","r");
  46. while(!feof(fp))
  47. {
  48. if((arr[a++]=getc(fp))=='\n')
  49. {
  50. printf("\\n encountered\n");
  51. }
  52.  
  53. //
  54. printf("the conetebts are %s\n",arr);
  55. //a=strcmp(arr,"127.0.0.1");
  56.  
  57.  
  58.  
  59. }
  60. ptr2=arr;
  61. printf("\n\n hi ther\n");
  62. strcpy(pckt.arr1,arr);
  63. printf("\n\nnooo\n");
  64. fclose(fp);
  65. while(pckt.arr1[i]!='\0')
  66. {
  67. printf("%c",pckt.arr1[i]);
  68. i++;
  69. }
  70.  
  71. printf("\nthe outside of loop are\n %s\nlent
  72. %d\n",pckt.arr1,strlen(arr));
  73. ptr=(char *)realloc(ptr,(strlen(arr)+1));
  74. if(ptr==NULL)
  75. printf("\nits friggin NULL \n");
  76. else
  77. {
  78. printf("inside else");
  79. a=0;
  80. i=0;
  81. do
  82. {
  83. ptr[a++]=arr[i++];
  84. }while(a!=strlen(arr));
  85. i=0;
  86. while(ptr[i]!='\0')
  87. {
  88. printf("\nthe last printy are\n %s\n",ptr[i]);
  89. i++;
  90. }
  91. }
  92.  
  93. free(ptr);
  94.  
  95. return 0;
  96. }
  97.  
  98.  
  99.  
May 23 '06 #1
1 1513
ma********@gmail.com 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.

May 23 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by luigi | last post: by
7 posts views Thread by Excluded_Middle | last post: by
7 posts views Thread by enki | last post: by
11 posts views Thread by toton | last post: by
22 posts views Thread by sam_cit | last post: by
7 posts views Thread by Markus Pitha | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.