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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:43:57+00:00 2026-05-30T07:43:57+00:00

For those experienced with C, this will be a simple memory allocation/referencing problem: Here

  • 0

For those experienced with C, this will be a simple memory allocation/referencing problem:

Here are my data structures:

struct configsection {
    char *name;
    unsigned int numopts;
    configoption *options;
};
typedef struct configsection configsection;

struct configfile {
    unsigned int numsections;
    configsection *sections;
};
typedef struct configfile configfile;

Here are my routines for initializing a configsection or configfile, and for adding a configsection to a configfile:

// Initialize a configfile structure (0 sections)
void init_file(configfile *cf) {
    cf = malloc(sizeof(configfile));
    cf->numsections = 0;
}
// Initialize a configsection structure with a name (and 0 options)
void init_sec(configsection *sec, char *name) {
    sec = malloc(sizeof(configsection));
    sec->numopts = 0;
    sec->name = name;
    printf("%s\n", sec->name);
}
// Add a section to a configfile
void add_sec(configfile *cf, configsection *sec) {
    // Increase the size indicator by 1
    cf->numsections = cf->numsections + 1;
    // Reallocate the array to accommodate one more item
    cf->sections = realloc(cf->sections, sizeof(configsection)*cf->numsections);
    // Insert the new item
    cf->sections[cf->numsections] = *sec;
}

I believe my problem originates in my init_sec() function. Here is an example:

int main(void) {

// Initialize test configfile
configfile *cf;
init_file(cf);

// Initialize test configsections
configsection *testcs1;
init_sec(testcs1, "Test Section 1");
// Try printing the value that should have just been stored
printf("test name = %s\n", testcs1->name);

Although the printf() in init_sec() successfully prints the name I just stored in the configsection, attempting the same thing in the printf() of main() produces a segmentation fault. Further, addsec() produces a segmentation fault.

  • 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-30T07:43:58+00:00Added an answer on May 30, 2026 at 7:43 am

    This routine should be

    void init_file(configfile **cf) { 
        *cf = malloc(sizeof(configfile)); 
        (*cf)->numsections = 0;
        (*cf)->sections = NULL; // You forgot to initialise this.
    }
    

    i.e. called by init_file(&myconfigfilepointer); so the malloc return value gets passed back.

    Need to do the same trick for init_sec

    This function is incorrect – here is a corrected version

    void add_sec(configfile *cf, configsection *sec) {     
        // Increase the size indicator by 1     
        // Reallocate the array to accommodate one more item     
        cf->sections = realloc(cf->sections, sizeof(configsection)*(1 + cf->numsections));     
        // Insert the new item     
        cf->sections[cf->numsections] = *sec; // Since arrays start at 0     
        cf->numsections = cf->numsections + 1;     
    } 
    

    You then need to adjust the calls in main

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

Sidebar

Related Questions

I’m sure all experienced Python programmers know a simple, elegant way to do this.
Here's a question for those of you with experience in larger projects and API/framework
this is driving me absolutely nuts. I'm not the most experienced with CSS, so
I want to write a simple server application that will take commands from a
I am having the hardest time trying to figure out this (should be) simple
Not terribly experienced using EJB, and I ran into the following problem with which
I'm curious to hear the experiences of those who are currently running their SVN
For those of you with experience with both, what are the major differences? For
Those of us with iPhone apps (released or unreleased) are able to send out
Those dark spinning progress dialogs in the Amazon and Engadget apps - are those

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.