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 7711495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:23:23+00:00 2026-06-01T01:23:23+00:00

I got a segmentation fault when invoked a function immediately following a pointer assignment.

  • 0

I got a segmentation fault when invoked a function immediately following a pointer assignment.

typedef struct
{
   DMINT field1;
   DMINT field2;
   DMINT field3;
} MSG1;

typedef struct
{
....
} MSG;

/* MSG is size of 1040 byte, bigger than MSG1 in size */

int main()
{
    MSG     Msg;
    MSG1   *pMsg1;
    int     mid;
    pthread_t  tid;
    ...

    Recv_msg( mid, &Msg);   /* this function does a memcpy to &Msg */
    pMsg1 = (MSG1 *)&Msg;

    //ret = pthread_join(pMsg1->..... );    /* Got Segmentation fault here by GDB*/
    /* even the first argument has nothing to do with pMsg1, SEGV is still received */
     ret = pthread_creat(&tid, NULL, thread_function, NULL); /* Got Segmentation fault here by GDB*/

It works fine if I remove pMsg1 = (MSG1 *)&Msg.
Is it because the two pointers have different sizes?

  • 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-01T01:23:24+00:00Added an answer on June 1, 2026 at 1:23 am

    You can safely convert one struct pointer to another only if one struct lays in the beginning of another (no matter sizes they are, see C std.):

    typedef struct {
      int a;
    } S1;
    typedef struct {
      S1 s1; // <- s1 it the FIRST structure field
      int b;
    } S2;
    S2 s2;
    S1 *s1;
    s1= (S1*)&s2; // <- safe
    

    Otherwise you could get into aligning issues and undefined behavior.

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

Sidebar

Related Questions

I got a strange segmentation fault when executing recv() function of . Here's the
I got segmentation fault for the following code, could someone help me understand why?
I've got a bit problem. My program throws segmentation fault when returning zero in
I got a segmentation fault when I attempted to upload an image to my
To my previous question about segmentation fault ,I got very useful answers.Thanks for those
I want to assign string from command line. But I got segmentation fault. why?
I am programming with cuda in C. I got a segmentation fault with the
I've got a bizarre error here: 882 Segmentation fault ./a.out The segment of code:
GOT error for the following query in MYSQL(version5.1) SELECT year,month,sum(fact_1),sum(fact_2),sum(fact_3),sum(fact_4) from( select year,month,fact_1,fact_2,0 as
Hello every one I have made a small function which takes a pointer to

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.