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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:34:31+00:00 2026-06-12T05:34:31+00:00

char test[]={abcde}; char* test1={xyz}; memcpy(test+5,test1,3); printf(%s,test); I’m trying to grasp how exactly memcpy works

  • 0
    char test[]={"abcde"};
    char* test1={"xyz"};
    memcpy(test+5,test1,3);
    printf("%s",test);

I’m trying to grasp how exactly memcpy works and this is the example I’ve written so far.
This gives output as abcdexyz&vjunkcharacters

and the following message.

*** stack smashing detected ***: ./testcode terminated
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(__fortify_fail+0x45)[0xb7656dd5]
/lib/i386-linux-gnu/libc.so.6(+0xffd8a)[0xb7656d8a]
./testcode[0x8048797]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75704d3]
./testcode[0x80483a1]

What are the reasons behind this situation?

  • 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-12T05:34:33+00:00Added an answer on June 12, 2026 at 5:34 am

    Root Cause:

    char test[]={"abcde"};
    

    Allocates enough memory space to store 5 characters only.

    memcpy(test+5,test1,3);  
    

    Copies the data pointed by test1 beyond the allocated memory space.
    Technically, writing beyond the bounds of an allocated memory in this fashion is Undefined Behavior, which means anything can happen.

    What actually happens?

    What actually happens here is memcpy copies characters beyond the allocated memory thus overwritting the NULL terminator which marks ends of your character array test.
    Further, printf reads the contents from starting address of test till it encounters a random NULL thus printing out junk characters.

    Solution:
    You should ensure that destination buffer has enough memory allocated before you perform the memcpy. Since you intend to copy 3 characters, Your destination buffer test should be atleast:

    5 + 3 + 1 byte for NULL terminator = 9 bytes
    

    You can simply use:

    char test[9]="abcde";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please look at the following code: char* test ( ) { char word[20]; printf
void test() { unsigned char c; c = (~0)>>1 ; printf(c is %u\n,c); }
char *test = hello; test = change_test(world); printf(%s,test); char* change_test(char *n){ printf(change: %s,n); return
so I execute this code #include <cstdlib> #include <iostream> int main() { char *test
I m confused about this issue: Const Char* Test() { String a = anything;
The example below gets the following compiled error: test.cpp: In function ‘int main(int, char**)’:
int main(int argc, char** argv) { char *test[5][20]; char *input[20]; int i; for(i=0;i<5;i++){ printf(enter>
I have a struct with many char array like this (and it works) :
for example when I wrote: Char[] test = new Char[3] {a,b,c}; test[2] = null;
Below is one of my C functiosn: void test(char * ptr) { ptr[0] =

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.