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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:16:49+00:00 2026-05-22T20:16:49+00:00

#inlcude <stdio.h> #inlcude <stdlib.h> #inlcude <string.h> int main() { char *buff = (char*)malloc(sizeof(char) *

  • 0
#inlcude <stdio.h>
#inlcude <stdlib.h>
#inlcude <string.h>

int main() {
    char *buff = (char*)malloc(sizeof(char) * 5);
    char *str = "abcdefghijklmnopqrstuvwxyz";

    memcpy (buff, str, strlen(str));

    while(*buff) {
        printf("%c" , *buff++);
    }

    printf("\n");

    return 0;
}

this code prints the whole string “abc…xyz”. but “buff” has no enough memory to hold that string. how memcpy() works? does it use realloc() ?

  • 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-22T20:16:49+00:00Added an answer on May 22, 2026 at 8:16 pm

    Your code has Undefined Behavior. To answer your question, NO, memcpy doesn’t use realloc.
    sizeof(buf) should be adequate to accomodate strlen(str). Anything less is a crash.

    The output might be printed as it’s a small program, but in real big code it will cause hard to debug errors. Change your code to,

    const char* const str = "abcdefghijklmnopqrstuvwxyz";
    char* const buff = (char*)malloc(strlen(str) + 1);
    

    Also, don’t do *buff++ because you will loose the memory record (what you allocated). After malloc() one should do free(buff) once the memory usage is over, else it’s a memory leak.

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

Sidebar

Related Questions

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *a = Hello ;
# include <stdio.h> # include <stdbool.h> # include <string.h> # include <stdlib.h> int main
#include <stdio.h> #include<stdlib.h> #define LIST.H onus; int main () { char *p,*s; printf( LIST.H
Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const
I have written a function to implement memcpy #include <stdio.h> #include <stdlib.h> #include <string.h>
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<stdbool.h> char** parser(char *message) { char a[9][256]; char* tmp =message; bool
sorry i modified the code now: #include <stdio.h> #include <stdlib.h> #include <string.h> void main()
I have the following code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> char
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
Can anyone explain to me why this isn't working? #include <stdio.h> #include <stdlib.h> char

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.