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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:46:30+00:00 2026-05-24T15:46:30+00:00

int main () { char *destination; char source[10] = jigarpatel; destination = (char*) malloc(5);

  • 0
int main ()
{
    char *destination;
    char source[10] = "jigarpatel";
    destination = (char*) malloc(5);
    memcpy(destination, source, 10);
    printf("%s and size is %d", destination, strlen(destination));
    free(destination);
    return 0;
}

Output:

 jigarpatel and size is 10

Question:

Here I have allocated just 5 bytes to destination, but the destination length is 10, why is this?

Where are the other bytes stored?

Is it safe in embedded system? Any chances of a crash or a segmentation fault?

How can I detect this type of mistake?

Another Question :

see i am writing one library where user asks for needed memory and library says allocate 10 bytes & then user malloc 10 bytes & pass its pointer to library. now library store some data there…now see if library said to allocate 10 bytes but user has allocated only 5 bytes & give that pointer to library then how can i detect that user hasnt malloc suffecient memory.

  • 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-24T15:46:31+00:00Added an answer on May 24, 2026 at 3:46 pm

    This sometimes happens to work but it’s definitely not safe on any system. You are writing past the end of what malloc is giving you. From the viewpoint of C it’s illegal, but from the viewpoint of the OS it might be ok (that memory might be paged with adequate permissions).

    Another problem is that if you later call malloc again, it might give you some memory including those 5 bytes that you are using without asking. This should provide some interesting debugging sessions.

    here i have just allocated only 5 bytes to destination then still why
    destination length is 10.?

    The destination has only 5 bytes allocated, but because of the way malloc works on your system, this doesn’t cause an invalid write, since it happens to be inside a valid page.

    where other bytes are stored.?

    Right after the first 5, for now.

    Is it safe in embedded system.? any chances to crash or segmention
    fault.?

    It’s unsafe in any system. Many chances of crashes.

    how can i detect this type of mistakes.?

    Using valgrind or any memory debugger. A gentle introduction to valgrind can be found here.

    ¹
    For example, on Linux (Glibc), small (~64 bytes) malloc requests are served from a small list of preallocated pages called “fastbins”. Each fastbin has a fixed size, and hence using an allocated fastbin up to that size would not trigger a segmentation violation. More details on how this happens can be found here, for a more rigorous treatment of the topic you might refer to the malloc source code.

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

Sidebar

Related Questions

#include<cstdio> #include<stdlib.h> int main() { char* ptr=NULL; printf(%s,ptr); return 0; } It prints (null)
#include <stdio.h> int main() { char temp[1024]; if(getchar() != 'y') { printf(no options\n); return
Consider the following code: int main() { char* s = (char*) malloc(sizeof(char)*10); s=hello; free(s);
int main(void) { char tmp, arr[100]; int i, k; printf(Enter a string: ); scanf_s(%s,
int main ( ) { char C[] = Hello World; write(0,C,sizeof(C)); return 0; }
This code: int main(char[][] args) { MyObject obj; obj.x; return 0; } gives me:
#include<stdio.h> int main() { char *p=mystring; return 0; } String literal mystring, where will
#include <stdio.h> int main () { char loop='y'; while(loop != 'n') { printf(loop? );
int main() { char *s1, *sTemp; s1 = (char*)malloc(sizeof(char)*7); *(s1 + 0) = 'a';
I wrote following program int main () { char a=0xf; a=a+1; printf(%c\n,a); } the

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.