Jump to content
  • Advertisement
Sign in to follow this  
likeafox

extern array seg fault

This topic is 3128 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This code compiles with no errors or warnings but when I run my one-line program I get a seg fault: Record.h
#ifndef RECORD_H_INCLUDED
#define RECORD_H_INCLUDED

struct SubrecordType{
	unsigned int name;
	char desc[24];
};

#ifndef IN_RECORD_CPP
extern const int numsubrectypes;
extern const SubrecordType * const subrecordtypes;
#endif


#endif // RECORD_H_INCLUDED


Record.cpp
#define IN_RECORD_CPP

#include "Record.h"

const int numsubrectypes = 2;

extern const SubrecordType subrecordtypes[numsubrectypes] = {
	{ 0                        ,   "Unknown Subrecord" },
	{ *((unsigned int *)"NAME"),   "Object ID"         }
};


main.cpp
#include <stdio.h>
#include "Record.h"

int main()
{
    printf("%i", subrecordtypes[0].name);
    return 0;
}


However! Changing extern const SubrecordType * const subrecordtypes; to extern const SubrecordType subrecordtypes[]; seems to have fixed my problem and all is well. I don't understand this. I grew up thinking [] was just a preallocated *const, and basically synonymous. I wracked my brain for hours today breaking my program into pieces trying to find this problem. I'm looking for some insight. HOW are these different? And if they are fundamentally different types then why didn't my compiler (GCC on Windows) even warn me about it? [Edited by - likeafox on February 25, 2010 11:36:02 AM]

Share this post


Link to post
Share on other sites
Advertisement
int [] x; means that x is an array of integers.
int * y; means that y is a pointer to an array of integers.

The compiler will automatically convert when you use an array where you need a pointer. This means y=x is a valid statement, even though the types differ.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!