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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:07:48+00:00 2026-06-13T19:07:48+00:00

I have been writing a piece of code for my coursework in electromagnetic simulation

  • 0

I have been writing a piece of code for my coursework in electromagnetic simulation and I have run into a problem. I decided to do a bit extra by expanding the original calculations to really large meshes of up to 10^8 elements, so now I have to use malloc().

So far, so good, but since I prefer to keep my code in libraries and then compile with the inline option of the compiler, I needed a way to pass information between functions. So, I started using structs to keep track of the parameters of the mesh, as well as the pointer to the array of information. I defined the struct the following way:

typedef struct {
    int    height;
    int    width;
    int    bottom; //position of the bottom node
    unsigned int***  dat_ptr;//the pointer to the array with all the data
    } array_info;

Where the triple pointer to an unsigned int is the pointer to a 2D array. I have to do it this way because otherwise it is passed by value and I cannot change it from within the function.

Now, when I try to allocate memory for the struct with the following function:

void create_array(array_info A)//the function accepts struct of type "array_info" as argument
{
    int i;

    unsigned int** array = malloc(sizeof(*array) * A.height);//creates an array of arrays
    for(i = 0; i<A.height; ++i)
    {
        array[i] = malloc(sizeof(**array) * A.width);//creates an array for each row
    }
    *A.dat_ptr=array;//assigns the position of the array to the input pointer
}

I get a segmentation fault upon executing the operation. I cannot see why: sizeof(*A.dat_ptr) is the same as sizeof(array). Thus, in the worst case I should be getting gibberish somewhere down the line, not in the assignment line, right?

  • 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-13T19:07:49+00:00Added an answer on June 13, 2026 at 7:07 pm

    You either need to return the array_info structure (as amended) from the function or (more usually) pass a pointer to the array_info structure into the function so that the changes you make affect the value in the calling function.

    typedef struct
    {
        int    height;
        int    width;
        int    bottom;
        unsigned int **dat_ptr;  // Double pointer, not triple pointer
    } array_info;
    
    void create_array(array_info *A)
    {
        unsigned int **array = malloc(sizeof(*array) * A->height);
        for (int i = 0; i < A->height; ++i)
            array[i] = malloc(sizeof(**array) * A->width);
        A->dat_ptr = array;
    }
    

    I assume you do some checking on the memory allocations somewhere; the logical place is this function, though. Recovery from a failure part way through is fiddly (but necessary if you are going to return from the function rather than exit from the program).

    void create_array(array_info *A)
    {
        unsigned int **array = malloc(sizeof(*array) * A->height);
        if (array != 0)
        {
            for (int i = 0; i < A->height; ++i)
            {
                 if ((array[i] = malloc(sizeof(**array) * A->width)) == 0)
                 {
                     for (int j = 0; j < i; j++)
                          free(array[j]);
                     free(array);
                     array = 0;
                     break;
                 }
            }
        }
        A->dat_ptr = array;
    }
    

    The calling function knows that the function failed if the dat_ptr member is null on return from create_array(). It might be better to provide a success/failure return value.

    I’m using C99, so the calling code might be:

    array_info array = { .height = 10, .width = 20, .dat_ptr = 0 };
    create_array(&array);
    if (array->dat_ptr == 0)
        ...error handling...
    

    Note that the code in create_array() might need to check for a null pointer, for negative or zero width or height. I’m not clear what the bottom element should contain, so I left it uninitialized, which gives me half an excuse for using designated initializers. You can also write the initializer quite clearly without using designated initializers.

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

Sidebar

Related Questions

I have been writing unit tests using NUnit and Moq with my Silverlight code
I have been writing Python code for only a couple of weeks, so I'm
I have been writing Common Lisp macros, so Scheme's R5Rs macros are a bit
I have a piece of code similar to the below that I have been
I have been writing some code for a lib and experimented with a default
I have been writing some basic code for an application I am designing. I
I have just been getting into low level programming (reading/writing to memory that sort
I have been writing linear winforms for couple of months and now I am
I have been writing a SOAP client application in C++ on Ubuntu using OpenSSL
I have been writing C++ Console/CMD-line applications for about a year now and would

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.