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

  • SEARCH
  • Home
  • 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 8132893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:27:15+00:00 2026-06-06T09:27:15+00:00

when calling PQconnectdb in the main program all run very well, but if i

  • 0

when calling PQconnectdb in the main program all run very well, but if i call it inside a function a seg fault appears.
here the code that run

#include <stdio.h>
#include <stdlib.h>
#include <postgresql/libpq-fe.h>



#define PG_HOST    "127.0.0.1"
#define PG_USER    "postgres"
#define PG_DB      "postgres"
#define PG_PASS    "postgres"
#define PG_PORT    5432



static void
exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}


int main( void )

{

char       conninfo[250];
PGconn     *conn = NULL;
PGresult   *pgres = NULL;

sprintf(conninfo, "user=%s password=%s dbname=%s hostaddr=%s port=%d", PG_USER, PG_PASS, PG_DB, PG_HOST, PG_PORT);
conn = PQconnectdb(conninfo);

if (PQstatus(conn) != CONNECTION_OK)
{
    fprintf(stderr, "ERROR: Connection to database failed: %s", PQerrorMessage(conn));
    exit_nicely(conn);
}


PQfinish(conn);

return 0;
}

this code run very well.

but when i put PQconnect inside a function, the program will generate a seg fault

int connect(char* conninfo, PGconn* conn)
{

conn = PQconnectdb(conninfo);

if (PQstatus(conn) != CONNECTION_OK)
{
    fprintf(stderr, "ERROR: Connection to database failed: %s", PQerrorMessage(conn));
    exit_nicely(conn);
}

return 1;
}




int main( void )

{

char       conninfo[250];
PGconn     *conn = NULL;
PGresult   *pgres = NULL;

sprintf(conninfo, "user=%s password=%s dbname=%s hostaddr=%s port=%d", PG_USER, PG_PASS, PG_DB, PG_HOST, PG_PORT);

connect(conninfo, conn);
if(!conn)
 fprintf(stderr, "conn is null.\n");


PQfinish(conn);

return 0;

}

herein the crash stack

(gdb) where
#0  __strlen_sse2 () at ../sysdeps/x86_64/multiarch/../strlen.S:32
#1  0x00007ffff7893086 in __GI___strdup (s=0x7 <Address 0x7 out of bounds>) at strdup.c:42
#2  0x00007ffff7bbbd87 in ?? () from /usr/lib/libpq.so.5
#3  0x00007ffff7bbc2a5 in ?? () from /usr/lib/libpq.so.5
#4  0x00007ffff7bbe389 in PQconnectStart () from /usr/lib/libpq.so.5
#5  0x00007ffff7bbe416 in PQconnectdb () from /usr/lib/libpq.so.5
#6  0x0000000000400912 in connect (conninfo=0x7 <Address 0x7 out of bounds>, conn=0x60a630) at pqconnect.c:25
#7  0x00007ffff7bbcadb in PQconnectPoll () from /usr/lib/libpq.so.5
#8  0x00007ffff7bbd77e in ?? () from /usr/lib/libpq.so.5
#9  0x00007ffff7bbe3b4 in PQconnectStart () from /usr/lib/libpq.so.5
#10 0x00007ffff7bbe416 in PQconnectdb () from /usr/lib/libpq.so.5
#11 0x0000000000400912 in connect (conninfo=0x7fffffffe600 "user=btel_user password=JwN5K9e18PsTb dbname=ULIC hostaddr=127.0.0.1 port=5432", conn=0x0) at pqconnect.c:25

#12 0x00000000004009e3 in main () at pqconnect.c:49

When i declare my function connect as static, the seg fault error doesn’t occur but the returned pointer for the variable conn is NULL

WHY? 🙁

  • 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-06-06T09:27:17+00:00Added an answer on June 6, 2026 at 9:27 am

    I think you have a mixup between your connect and the standard library connect. Your connect takes precedence, so when PQconnectdb tries to call connect, things would go bad.
    Try to rename the function.
    Making connect static also prevents the mixup, which explains why the crash is removed.

    Also, you pass the conn parameter to connect incorrectly. It’s passed by value, so the variable in main isn’t changed, and stays NULL.
    You need to pass it by reference.

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

Sidebar

Related Questions

Calling the ajax called URL works well without ajax eg. http://localhost/ci/controller/method/param_value . But using
When calling an ASP.Net PageMethod, we call it as follows: function doSomething(htmlElement) { PageMethods.GetText(onSuccess,
Calling MyPanel.Panel1.Hide(); or MyPanel.Panel2.Hide(); simply hides the controls inside the panel... but I want
Calling Thread.join blocks the current (main) thread. However not calling join results in all
Calling a function from the MasterPage in a Page is quite straigt forward but
I calling multiple function on page load in my asp.net web application. All function
Calling a Lua function from C is fairly straight forward but is there a
After calling fork , the current process will call exit(0) . But the child
Calling all Oracle Gurus! I am in the process of clustering a well tested
Calling setBackgroundDrawable before showing the PopupWindow works fine. But when I call it after

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.