Structure Segmentation Fault

This is a discussion on Structure Segmentation Fault within the C++ Programming forums, part of the General Programming Boards category; I am having a little trouble with my structure, since I can't give it any values. Here is the structure: ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    1

    Structure Segmentation Fault

    I am having a little trouble with my structure, since I can't give it any values.

    Here is the structure:

    Code:
    typedef struct TPacote {
    	unsigned int pos_bola[2];
    	unsigned int pos_jogadores_nossos[5][2]; //[ID][posx, posy]
    	unsigned int dir_jogadores_nossos[5][1]; //[ID][angulo em graus]
    	unsigned int pos_jogadores_deles[5][2]; 
    
    	//Marcacao do campo
    	//Mundo e campo: sup_dir, sup_esq, inf_esq e inf_dir
    	unsigned int pos_mundo[4][2];
    	unsigned int pos_campo[4][2];
    	//Meio campo: sup e inf
    	unsigned int pos_meio_campo[2][2];
    	//Gol: sup e inf
    	unsigned int pos_gol_0[2][2];
    	unsigned int pos_gol_1[2][2];
    } Pacote;
    Now, i want to give it values, like this:

    Code:
    Pacote *pack;
    
    pack->pos_bola[0] = 1;
    pack->pos_bola[1] = 2;
    pack->pos_jogadores_nossos[0][0] = 3;
    pack->pos_jogadores_nossos[0][1] = 4;
    pack->pos_jogadores_nossos[1][0] = 5;
    pack->pos_jogadores_nossos[1][1] = 6;
    pack->pos_jogadores_nossos[2][0] = 7;
    pack->pos_jogadores_nossos[2][1] = 8;
    pack->pos_jogadores_nossos[3][0] = 9;
    pack->pos_jogadores_nossos[3][1] = 10;
    pack->pos_jogadores_nossos[4][0] = 11;
    pack->pos_jogadores_nossos[4][1] = 12;
    pack->dir_jogadores_nossos[0][0] = 13;
    pack->dir_jogadores_nossos[1][0] = 14;
    pack->dir_jogadores_nossos[2][0] = 15;
    pack->dir_jogadores_nossos[3][0] = 16;
    pack->dir_jogadores_nossos[4][0] = 17;
    pack->pos_jogadores_deles[0][0] = 18;
    pack->pos_jogadores_deles[0][1] = 19;
    pack->pos_jogadores_deles[1][0] = 20;
    pack->pos_jogadores_deles[1][1] = 21;
    pack->pos_jogadores_deles[2][0] = 22;
    pack->pos_jogadores_deles[2][1] = 23;
    pack->pos_jogadores_deles[3][0] = 24;
    pack->pos_jogadores_deles[3][1] = 25;
    pack->pos_jogadores_deles[4][0] = 26;
    pack->pos_jogadores_deles[4][1] = 27;
    Them, I compile and everything is okay, but when I run, I get a Segmentation Fault error. Where is it?

    See ya!

    GouSan

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You declared a pointer to a struct.... and then you tried messing with whatever it pointed to. Where do you think the pointer is pointing to?

    Hint: Not valid memory.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Your pointer isn't pointing anywhere specific. The address stored in pack is just whatever random value that happens to be there and then you attempt to dereference that random address. You need to new some space for pack first before you use it... or point it somewhere valid.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    Hint: new/delete.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Given that you obviously don't yet know about dynamically allocating memory, I suspect you perhaps just want to declare an instance of this object as a global or local variable. In that case, just remove the * and use a dot . instead of an arrow ->.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Replies: 7
    Last Post: 12-10-2004, 01:58 AM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21