Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6154295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:14:23+00:00 2026-05-23T20:14:23+00:00

I am getting a segmentation fault on my getaddrinfo call and cannot figure out

  • 0

I am getting a segmentation fault on my getaddrinfo call and cannot figure out why. It happens on both my server and client. Some code (server side) is –

class TcpServer {
public:

    TcpServer(int);
    ~TcpServer();

    void launchServer();

    void communicate();

private:
    const char* port;
    int fd;
    int comm_fd;
};

in tcpserver.cpp-

void TcpServer::launchServer() {
    int status;

    struct addrinfo hints;
    struct addrinfo *servinfo;  //will point to the results

    //store the connecting address and size
    struct sockaddr_storage their_addr;
    socklen_t their_addr_size;

    //socket infoS
    memset(&hints, 0, sizeof hints); //make sure the struct is empty
    hints.ai_family = AF_INET;  //local address
    hints.ai_socktype = SOCK_STREAM; //tcp
    hints.ai_flags = AI_PASSIVE;     //use local-host address

    //get server info, put into servinfo
    if ((status = getaddrinfo("127.0.0.1", port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
    }

in main-

TcpServer server(4950);
server.launchServer();

The int passed to the constructor is casted to a const char* for port.

When I run gdb, it gives me a backtrace of –

#0  0xb7dca737 in getaddrinfo (name=0x8054824 "127.0.0.1", 
    service=0x1356 <Address 0x1356 out of bounds>, hints=0xbffff20c, 
    pai=0xbffff234) at ../sysdeps/posix/getaddrinfo.c:2080
#1  0x08050f79 in TcpServer::launchServer (this=0xbffff304) at tcpserver.cpp:25
#2  0x0804eae9 in main (argc=1, args=0xbffff3f4) at mainserver.cpp:47

So “Address 0x1356 out of bounds” makes me believe something is wrong with port, but I do not know what could be wrong. If anyone can point out something wrong I would be grateful. Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:14:23+00:00Added an answer on May 23, 2026 at 8:14 pm
    getaddrinfo("127.0.0.1", port, &hints, &servinfo)
                              ^
    

    That should be a char *. I am guessing you are passing an integer and forcing the library to access an invalid address.

    EDIT

    In light of the comment of Blagovest Buyukliev I believe you are doing something like this in the constructor: this->port = (const char*) port.

    You need to use something (snprintf maybe ?) to convert that integer to a char *. Simply casting won’t do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting segmentation fault in this code but i cant figure out why.
I am getting a segmentation fault while running this code. I can't work out
I'm getting a segmentation fault the first time I call malloc() after I protect
I am getting a segmentation fault with the following code after adding structs to
I'm getting a segmentation fault trying to compile the following code. My question is,
I'm getting a Segmentation Fault when trying to call a function that is within
Im working on this bit of code and I keep getting a segmentation fault.
I am getting a segmentation fault error when I run this code. I don't
I'm getting a segmentation fault from SDL_BlitSurface in C++. here's the code mainScreen->draw( 0,
I am getting a segmentation fault in the following code: void print_stack(Node * root)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.