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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:38:44+00:00 2026-05-16T11:38:44+00:00

The following code causes a SIGSEGV, but only while debugging. #include <stdio.h> #include <stdlib.h>

  • 0

The following code causes a SIGSEGV, but only while debugging.

#include <stdio.h>
#include <stdlib.h>

typedef struct enemy_desc
{
int type;
int x;
int y;
}enemy;

int main()
{
    enemy **enemies;
    enemies=(enemy **)malloc(sizeof(enemy *)*16);

    enemies[0]->type=23;

    printf("%i",enemies[0]->type);
    return 0;
}
  • 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-16T11:38:45+00:00Added an answer on May 16, 2026 at 11:38 am

    You have allocated memory for 16 enemy * pointers, but you have not allocated room for the 16 enemy structs themselves. There are two ways to fix this. One is to add a loop that allocates each of the 16 enemy structs one by one:

    int main()
    {
        enemy **enemies;
        int i;
    
        enemies = (enemy **) malloc(sizeof(enemy *) * 16);
    
        for (i = 0; i < 16; ++i) {
            enemies[i] = (enemy *) malloc(sizeof(enemy));
        }
    
        enemies[0]->type = 23;
    
        printf("%i",enemies[0]->type);
        return 0;
    }
    

    The other is to remove one level of indirection. If you declare enemy *enemies then you can allocate the 16 structs at once and forgo a loop. If there’s no need for the double indirection this would be my preferred solution:

    int main()
    {
        enemy *enemies;
        enemies = (enemy *) malloc(sizeof(enemy) * 16);
    
        enemies[0].type=23;
    
        printf("%i",enemies[0].type);
        return 0;
    }
    

    Notice that the -> operator switches to ..

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

Sidebar

Related Questions

The following code causes a segmentation fault. The executable is named './struct' #include <stdio.h>
The following code causes a stack overflow but I don't see why... int _tmain(int
I have the following code. but it causes exception. using (var context = new
The following code causes a compiler error, as it is ambiguous call but the
I'm trying to build googletest with Visual C++ 11, but following code causes an
For some reason the following code causes a SIGSEGV when I try to call
The following code compiles with gcc-4.5.1 but not in Visual Studio 11. #include <map>
The following code works just fine with Android 4, but causes an IllegalArgumentException in
The following code causes a NullReferenceException tStartParameter = String.Format(tStartParameter, tTo, tSubject) tProcess = Process.Start(New
Environment: Seam, Richfaces The following code snippet causes the method getUsers to be called

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.