473,382 Members | 1,349 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,382 software developers and data experts.

Getting segmentation fault while initialising nested structure variable

Dear Friends,
I am not able to assign value into struct addrinfo variable through struct sort_result structure variable.
Expand|Select|Wrap|Line Numbers
  1. struct addrinfo
  2. {
  3.    int ai_flags; /* Input flags. */
  4.  
  5.    int ai_family; /* Protocol family for socket. */
  6.  
  7.    int ai_socktype; /* Socket type. */
  8.  
  9.    int ai_protocol; /* Protocol for socket. */
  10.  
  11.    socklen_t ai_addrlen; /* Length of socket address. */
  12.  
  13.    struct sockaddr *ai_addr; /* Socket address for socket. */
  14.  
  15.    char ai_canonname; /* Canonical name for service location. */
  16.  
  17.    struct addrinfo *ai_next; /* Pointer to next in list. */
  18. };
  19.  
  20.  
  21. struct sort_result
  22. {
  23.     struct addrinfo *dest_addr;
  24.  
  25.     /* Using sockaddr_storage is for now overkill. We only support IPv4 and IPv6 so far. If this changes at some point we can adjust the type here. */
  26.  
  27.     struct sockaddr_in6 source_addr;
  28.  
  29.     uint8_t source_addr_len;
  30.  
  31.     bool got_source_addr;
  32.  
  33.     uint8_t source_addr_flags;
  34.  
  35.     uint8_t prefixlen;
  36.  
  37.     uint32_t index;
  38.  
  39.     int32_t native;
  40. };
  41.  
  42. int main ()
  43. {
  44.     struct sort_result a1;
  45.     cout<<"before"<<endl;
  46.  
  47.     memset(&a1, 0, sizeof (struct sort_result));
  48.  
  49.     memset(&a1.dest_addr, 0, sizeof (struct addrinfo));
  50.  
  51.     cout<<"after"<<endl;
  52.     cout<<"Index"<<a1.index<<endl;
  53.  
  54. /** not able to assign value into struct addrinfo variable through struct sort_result structure variable **/
  55.  here-->>>    a1.dest_addr->ai_family = AF_UNSPEC; 
  56.  
  57.     cout<<"after initialize"<<endl;
  58.     a1.dest_addr->ai_socktype = SOCK_STREAM;
  59.  
  60.     return 0;
  61. }
Nov 13 '14 #1

✓ answered by weaknessforcats

You never created a struct addrinfo variable. All you did was create a pointer that was never initialized.

Use malloc to create a memory allocation for sizeof(struct addrinfo) and then assign the address returned by malloc to your struct addrinfo pointer.

Don't forget that you need to free this memory when you no longer need it to prevent a leak.

1 2465
weaknessforcats
9,208 Expert Mod 8TB
You never created a struct addrinfo variable. All you did was create a pointer that was never initialized.

Use malloc to create a memory allocation for sizeof(struct addrinfo) and then assign the address returned by malloc to your struct addrinfo pointer.

Don't forget that you need to free this memory when you no longer need it to prevent a leak.
Nov 13 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Vishal Grover | last post by:
Hello Everyone, I am seeing a certain behaviour which I find strange, and am curious to get an explanation to it. I have the following program. #include <iostream> #include <cstdlib> using...
18
by: bumps | last post by:
Guys, I am getting segmentation fault while I am trying to open a file in a small function. int PingSamplesList::saveListElements (char *outputFileString) { PingSampleElement *tmpPtr; int...
19
by: Sameer | last post by:
Hi friends, I am using Mandriva Linux 9.2 and gcc. My source code is, int chunkin ; //no error int i ; for (i=0;i<7225;i++) { chunkin = somedata ;
2
by: ramu | last post by:
Hi, While am trying to run this code am getting segmentation fault. What's wrong with this code? Here am trying to assign a value to the variable b. #define SIZE 10 struct y { int a;...
5
by: doni | last post by:
Hi, I am a beginner to C and I wrote a program that takes arguments and prints it in hex. I am getting a segmentation fault if I dont pass any arguments instead I want it to display the Usage...
2
pawanrpandey
by: pawanrpandey | last post by:
Hi, When I am running my Perl Application, which uses some additional Perl Libraries, getting 'Segmentation Fault (core dumped)' error. I have build the perl again but still the problem is...
1
by: amit2special | last post by:
#include <string.h> #include <stdio.h> #include <stdlib.h> void reverse(int pos, char *str){ int l = strlen(str); if(pos<(l/2)) { char ch; ch...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.