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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:54:34+00:00 2026-05-27T13:54:34+00:00

I need several global pointers to be shared among a few files – the

  • 0

I need several global pointers to be shared among a few files – the pointers are essentially arrays of double whose lengths are only determined at runtime.

I include here the pieces of the code that caused the issue. This is not the exact code, but it illustrates all the points precisely:

foo.h

#ifndef FOOH
#define FOOH

/* ------------------
COMMON VARIABLES
---------------------*/

// create_bundles.c
extern double *all_bundle;

/* ------------------
COMMON FUNCTIONS
---------------------*/

// create_bundles.c
void create_bundles(int num_firm);

// memory_allocation.c
void allocate_memory(int num_firm, int num_bundle);
void clean_memory(void);
#endif

create_bundles.c

#include "foo.h"
extern double *all_bundle;

void create_bundles(int num_firm) {
int i;
    for (i = 0; i < num_firm; i++) {
        all_bundle[i] = 1
    }

memory_allocation.c

#include "foo.h"

// create_bundles.c
double *all_bundle = NULL;

void allocate_memory(int num_firm, int num_bundle) {
    all_bundle = calloc(num_bundle * num_firm, sizeof(double));
}
void clean_memory(void) {
    free(all_bundle);
}

main.c

#include "foo.h"
void main(int num_firm, int num_bundle) {
    allocate_memory(num_firm, num_bundle);
    create_bundles(num_firm);
    clean_memory();
} 

What happened is that if I print out all_bundle[i] it’ll all be 0, and then it’ll give me a segmentation error.

Why the error and how to fix it?

  • 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-27T13:54:34+00:00Added an answer on May 27, 2026 at 1:54 pm

    The problem is not in global pointer, but something else. Keep looking for the problem in your common code. I hope you are trying to print contents of all_bundle array before calling clean_memory. I have edited your code a little bit and it works great without any segmentation errors and prints 1.0000. Here it is, take a look:
    foo.h:

    #ifndef FOOH
    #define FOOH
    
    // create_bundles.c
    extern double *all_bundle;
    
    // create_bundles.c
    void create_bundles(int num_firm);
    
    // memory_allocation.c
    void allocate_memory(int num_firm, int num_bundle);
    void clean_memory(void);
    
    #endif
    

    memory_allocation.c:

    #include <stdlib.h>
    
    #include "foo.h"
    
    double *all_bundle = 0;
    
    void allocate_memory(int num_firm, int num_bundle) {
        all_bundle = calloc(num_bundle * num_firm, sizeof(double));
    }
    void clean_memory(void) {
        free(all_bundle);
    }
    

    create_bundles.c:

    #include "foo.h"
    
    void create_bundles(int num_firm) {
        int i;
        for (i = 0; i < num_firm; i++) {
            all_bundle[i] = 1;
        }
    }
    

    main.c:

    #include <stdio.h>
    #include "foo.h"
    
    int main(int argc, char *argv[]) {
        allocate_memory(100, 1);
        create_bundles(100);
    
        {
            int i;
            for(i = 0; i < 100; ++i)
                printf("%f\n", all_bundle[i]);
        }
    
        clean_memory();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert several Java classes to C#, but I have faced few
I need to download several files via http in Python. The most obvious way
I need to copy several tables from one DB to another in SQL Server
I need to store several date values in a database field. These values will
I need to create several applications that all share a Microsoft SQL Server database.
I need to convert several million dates stored as wide strings into boost dates
I need to build several Rails applications for one client. I would like to
I need to multiply several 1000s digits long integers as efficiently as possible in
I need to encryption several pieces of text in a file along side unencrypted
I need to create several Divs dynamically, and I need to add onmouseover event

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.