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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:45:12+00:00 2026-06-13T08:45:12+00:00

Possible Duplicate: C Array Instantiation – Stack or Heap Allocation? When dynamically allocating a

  • 0

Possible Duplicate:
C Array Instantiation – Stack or Heap Allocation?

When dynamically allocating a struct containing a char pointer, what happens with the actual char pointer? Where is it stored?

And once the struct is freed, is the char pointer freed along with it?

For example consider the following struct:

struct mix
{
    int a;
    float b;
    char *s;
};

typedef struct mix mix;

And then the following code that allocates memory for it:

int main()
{
    mix *ptr = (mix*)malloc(sizeof(mix));

    ptr->a = 3;
    ptr->b = 4.5f;
    ptr->s = "Hi, there, I'm just a really long string.";

    free(ptr);

    return 0;
}

Is *s allocated on the stack and then freed along with *ptr? I can imagine it is indeed allocated on the stack as it’s not in any way dynamically allocated (unless malloc has some functionality I’m not aware of). And I guess ‘going out of scope’ for *s would be at the point of freeing *ptr. Or have I got it completely wrong? 🙂

Thanks very much!

  • 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-13T08:45:13+00:00Added an answer on June 13, 2026 at 8:45 am

    The space for the char* member named s is allocated on the heap, along with the rest of the members of mix after the call to malloc() (whose return value you do not need to cast). The string literal to which s is assigned is not allocated on the heap or the stack, but is part of the actual binary and has static storage duration. So this:

    ptr->s = "Hi, there, I'm just a really long string.";
    

    assigns the address of the string literal to ptr->s. If you want ptr->s to point to something other than a string literal then you need to malloc() memory for it. And for every malloc() there must be a free() so ptr->s would need to be free()d before ptr is (if ptr->s is pointing to dynamically allocate memory only).

    After the call to free(), dereferencing ptr is undefined behaviour.

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

Sidebar

Related Questions

Possible Duplicate: Is array name a pointer in C? char arr[1024]; arr++ //move arr
Possible Duplicate: PHP : Array key containing special character not found I am comparing
Possible Duplicate: Dynamic array in Stack? How do compilers treat variable length arrays Someone
Possible Duplicate: concatenate char array in C How to concatenate: char *a=abcd; char *b=1234;
Possible Duplicate: C/C++: Array size at run time w/o dynamic allocation is allowed? In
Possible Duplicate: Is array name a pointer in C? Array Type - Rules for
Possible Duplicate: Is array name a pointer in C? C++ Static array vs. Dynamic
Possible Duplicate: Is array name a pointer in C? So, I usually declare arrays
Possible Duplicate: Java Generics: Array containing generics I have a Java class which contains
Possible Duplicate: Very large array on the heap (Visual C++) i need to declare

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.