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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:51:55+00:00 2026-05-31T23:51:55+00:00

My program/process exited abnormally? I doubt something is wrong with the following code: There

  • 0

My program/process exited abnormally? I doubt something is wrong with the following code:

There are two typedef TYPE1, TYPE2. (TYPE1’s size is bigger than TYPE2)

TYPE1 var1;
TYPE2 var2;
....
....var2 has been assigned.
....
memset(&var1, 0, sizeof(var1));
memcpy(&var1, (TYPE1 *)&var2, sizeof(TYPE1));
printf("....");
  1. Cast var2 may cause accessing the illegal memory which may lead to segmentation fault? I thought it may mess up the data following the var2 in memory, but cannot make segmentation fault? I found my program exit here, since the following printf() did not print anything.

  2. Cast will ruin the data following var2 in memory even if it is only read (var2 is just source of memcpy().

  3. what happens if I change it like memcpy(&var1, (TYPE1 *)&var2, sizeof(TYPE2));

Thanks.

  • 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-31T23:51:57+00:00Added an answer on May 31, 2026 at 11:51 pm

    So first: you don’t need to cast &var2 to TYPE1* when using memcpy. memcpy expects void*, and any pointer type can be cast to void*.

    The problem here is, when TYPE1 is bigger than TYPE2 and you are copying the data from var2 to var1 using sizeof(TYPE1) as the size, memcpy will eventually read from beyond the memory occupied by var2. As a consequence, you are accessing memory you are not allowed to access. Hence, a segmentation fault is the best you could have gotten there, since that error might also have be gone unnoticed for quite some time until it really could have caused trouble (undefined behavior is the key phrase here).

    When you copy only sizeof(TYPE2) bytes from var2 to var1, you are ok in terms of memory access (as long as TYPE1 is really bigger than TYPE2).

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

Sidebar

Related Questions

With win32api, I want that the following program creates two process and creates a
Will the following program cause any problem during compiling and execution process? class A{
I launch the following command line (process) from a Windows VC++ 6 program using
I'm trying to write a program to process the BSD-style process accounting file under
I wanted to know How OS actually makes a program in to process. what
My program uses wx.ProgressDialog to give feedback on a process that is in multiple
I am trying to process an uploaded file in a Perl program, using CGI::Application.
I have a program which needs to invoke a process to perform an operation
I have a simple Tray icon program that opens a site using System.Diagnostics.Process.Start(URL) And
If a Java program is started, it get's in the system process-monitor the name

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.