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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:40:07+00:00 2026-05-31T20:40:07+00:00

I have a struct that looks like this: typedef struct _my_struct { float first_vector[SOME_NUM][OTHER_NUM];

  • 0

I have a struct that looks like this:

typedef struct _my_struct {
    float first_vector[SOME_NUM][OTHER_NUM];
    float second_vector[SOME_NUM][OTHER_NUM];
    int some_val;
} my_struct;

I’d like to do:

my_struct * thing = (my_struct *)malloc(sizeof(my_struct));

But when I do so and then try to access anything in the vectors, I get a seg fault.

If I instead declare the vectors in the struct as:

typedef struct _my_struct {
    float **first_vector;
    float **second_vector;
    int some_val;
} my_struct;

and then allocate with:

my_struct->first_vector = (float**)malloc(sizeof(float*) * SOME_NUM);
my_struct->second_vector = (float**)malloc(sizeof(float*) * SOME_NUM);
int i;
for (i = 0; i < SOME_NUM; i++){ 
    my_struct->first_vector[i] = (float*)malloc(sizeof(float) * OTHER_NUM);
    my_struct->second_vector[i] = (float*)malloc(sizeof(float) * OTHER_NUM);
} 

Then everything works fine.

Since first and second vector are fixed in size and known at compile time, it seems strange that I have to individually allocate memory for them. Is there a way to declare them in the struct so that I can just malloc a new struct without also allocating memory for each vector separately?

  • 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-31T20:40:08+00:00Added an answer on May 31, 2026 at 8:40 pm

    What you have should be fine.. aside from the funky typedef at the top and odd declaration of my_struct * struct = malloc... this small test case works for me:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct {
        float first[10][20];
        float second[10][20];
        int val;
    } my_struct;
    
    int main(void) {
        printf("Sizeof mystruct %d\n", sizeof(my_struct));
    
        my_struct* str = malloc(sizeof(my_struct));
        if(!str) {
            printf("Memory allocation error!");
            exit(1);
        }
    
        str->first[0][0] = 1;
        str->second[1][19] = 15;
    
        printf("values %f %f\n", str->first[0][0], str->second[1][19]);
    
        free(str);
    
        return 0;
    
    }
    

    Output is:

    Sizeof mystruct 1604
    values 1.000000 15.000000
    

    Additionally, you should not cast the return value of malloc. It can hide important compiler warnings.

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

Sidebar

Related Questions

I have a C structure that looks like this typedef struct event_queue{ Event* event;
I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {
I have a code that looks something like: struct Data { int value; };
I have a struct that I initialize like this: typedef struct { word w;
I have a struct that's defined like this: struct Vec3 { float x, y,
I have seen structure declarations which looks like this one typedef struct br {
I have a file that looks like this: namespace myName { typedef HRESULT (*PFN_HANDLE)(myName::myStruct);
If I have some code that looks something like: typedef struct { bool some_flag;
I have an unmanaged struct I'd like to marshal to c sharp that looks
I have a text file that looks like this: 1 2 4 3 5

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.