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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:27:08+00:00 2026-06-14T16:27:08+00:00

Suppose I have a char pointer which points to some string in memory. and

  • 0

Suppose I have a char pointer which points to some string in memory.
and suppose I want to copy that string to some other place in memory.

void cpy(char **dst, char *src)
{
    *dst = (char *) realloc(*dst, strlen(src) + 1);
    memcpy(*dst, src, strlen(src) + 1);
}

(Assume memory allocation is successful, and src is not NULL)
What if I call this function like this:

char *str = malloc(14);
memcpy(str,"hello, world!", 14);
cpy(&str,str+7);

Now I would expect srt to point to the string "world!" (Which it does in my tests).
But what concerns me is that in this call to cpy, *dst and src actually point to different locations of the same string. And, when calling realloc on *dst, it’s possible that this memory will be freed. But in the next line I’m trying to copy from that place with memcpy.

So the questions is: Is there something wrong with it?
Or to put it another way – is it okay to free memory, and use it immediately afterwards?

Thanks.

Note: The example was updated so that realloc is called on memory that was obtained with malloc.

  • 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-14T16:27:10+00:00Added an answer on June 14, 2026 at 4:27 pm

    Everything is wrong with this. It is outright undefined behaviour to call realloc on a pointer that was not obtained with malloc etc.

    As @Daniel Fischer points out, it is also undefined behaviour to use memcpy on overlapping regions of memory (in which case you should use memmove), so you have to be careful.


    Update: After your substantial edit, the question is quite different. It is now equivalent to this condensed version:

    char * s = malloc(14);
    strcpy(s, "hello, world!");
    
    char * p = realloc(s, 14);
    
    memcpy(p, s, 14);
    

    This is also undefined behaviour, because you are not allowed to access the memory pointed to by s anymore after a successful realloc, and you’re not allowed to access the memory pointed to by p after an unsuccessful realloc.

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

Sidebar

Related Questions

Suppose I have a string char* str . I print it to the buffer
I have C function that takes string pointer as a parameter. This functions returns
Suppose I have the memory address as a string representation (say 0x27cd10). How can
Suppose I have a pointer to image data which has 3 channels (RGB), 8
Suppose I have a utility function like this - public static boolean isABlankSpace(char c)
Suppose I have a pure virtual method in the base interface that returns to
Suppose I have a static method of my class that returns an object of
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a process that is updating a record and encounters a record
How do I assign a string to a character pointer in C ? Suppose

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.