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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:30:26+00:00 2026-06-18T04:30:26+00:00

First off, let me begin by saying I KNOW that this question has been

  • 0

First off, let me begin by saying I KNOW that this question has been answered at some point. It had to have been, but I simply do not know the keywords to find an answer to it.

I am writing a program, and inside of my program I have to create multiple dynamically sized arrays all over the place, and then store them to one grand struct. So if I had to create the same type of dynamically allocated array, I create it in a function (meaning declare it, malloc it, and return it) to my struct field. However, now that I am down the road I think that I made a major flaw. I believe what I did is destroyed by the scope of the function, meaning once the function completes, the original pointer is killed and therefore my struct is pointer to a address that is no longer “mine”. Below is a sample call to the function, hopefully it makes sense without comments.

//Call from main()
current_event->mens_names = create_names_matrix(number_of_couples, 20);

//my "create_names_matrix" function:
char ** create_names_matrix(int num_of_names, int length_of_names){
int i;
char **return_matrix;

return_matrix = malloc(num_of_names * sizeof(char*));
if(return_matrix == NULL){
    panic("return_matrix"); //Panic is a function that checks if malloc failed
}

for(i = 0; i < num_of_names; i++){
    return_matrix[i] = malloc(length_of_names * sizeof(char));
}

if(return_matrix[i-1] == NULL){
    panic("return_matrix"); //Panic is a function that checks if malloc failed
}
return return_matrix;

}

would a solution be to go ahead and declare and dynamically create each field array in the main, and then pass that into each of my “create_xxx_matrix” functions as a parameter to avoid them being killed after the scope of the function is complete?

I know this is a noob question but it has been a while since I worked with pointers & c.

Thanks!

  • 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-18T04:30:27+00:00Added an answer on June 18, 2026 at 4:30 am

    Dynamically allocated pointers are allocated in the heap and not killed by the scope of the function. By scope, the variables allocated in the stack are destroyed once they leave the function.

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

Sidebar

Related Questions

First off, let me start by saying, I know this exact question has been
First off, let me preface this question by saying that my professor is firmly
First off, let me apologize if this has been asked already, but I can’t
First off, I know this has been discussed over and over again . But
First off, let me preface this question by stating that I'm really a pretty
First off, let me start by saying that I am totally new to working
First off, I don't know if this is possible, but let me describe my
First off, let me say that this is not homework (I am an A-Level
First off, let's start by saying I'm a rookie. That being said, I am
First off, let me explain that I am well aware of what this exception

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.