473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1623
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
7
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
7
by: enki | last post by:
What I want to do is create sprites and put them in a vector. I know that this specific example dosn't realy fit in this group but my sprite are ponters. vector<*sprite>spvec; sprite *...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
7
by: Markus Pitha | last post by:
Hello, I still have massive problems with handling with pointers when I use them through methods or much more complicated constructs. Do you have any good and helpful links which describe these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.