DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 17th April 2011
xmorg xmorg is offline
Real Name: Tim Cooper
Fdisk Soldier
 
Join Date: Sep 2010
Location: San Diego
Posts: 56
Default C: address of an int in an array in a struct?

This is an SDL program. gr->rcFlora[1] is an SDLRect with members x,y,w,h
Previously gr->rcFlora[1] was just a normal SDLRect that I declared on top of the code block. Now that I moved it into the struct "gr", it causes the program to segfault.

I allocate memory for it in this way

gr = (GameRes *) malloc(sizeof(GameRes));
LoadResources(gr, scrmode);


Inside the LoadResouces Function I can access the variables like so

Code:
printf("Loading broken tower.\n");
  temp = IMG_Load("data/broken_tower.png");
  gr->flora[1] = SDL_DisplayFormatAlpha(temp);
  gr->rcFlora[1].x = 600; gr->rcFlora[1].y = 128;
  printf("Is the value accessible here?-> x= %d\n",gr->rcFlora[1].x); //Here they work but not in main?
  SDL_FreeSurface(temp);
Code:
if(gr->rcFlora[1].x >= 0 && gr->rcFlora[1].y >= 0 ) {
	printf("SDL_BlitSurface(gr->tower, NULL, screen, &gr->rcFlora[1]);\n"); //This prints
      	SDL_BlitSurface(gr->tower, NULL, screen, &gr->rcFlora[1]); //it crashes here
	printf("success!\n"); //This does not print.
I hope it makes sense. My question is why cant I access the address of an int in an array which is a member of a struct?
Reply With Quote
  #2   (View Single Post)  
Old 17th April 2011
thirdm thirdm is offline
Spam Deminer
 
Join Date: May 2009
Posts: 248
Default

Where was gr->tower given a value? Where was memory for screen allocated?

But you said all this worked before you moved into the struct. What's the definition of this struct? You moved it in like this?

struct GameRes { .... SDLRect rcFlora[2]; ...};

Or did you do something like the following and forget to allocate room for the member array?


struct GameRes { .... SDLRect rcFlora[]; ...};

Also, you don't want the address of an int, you want the address of an SDL_Rect, but I'm guessing that was just a flub, that sentence.

Last edited by thirdm; 17th April 2011 at 04:59 PM.
Reply With Quote
  #3   (View Single Post)  
Old 17th April 2011
xmorg xmorg is offline
Real Name: Tim Cooper
Fdisk Soldier
 
Join Date: Sep 2010
Location: San Diego
Posts: 56
Default

Thanks for the reply. Right, I want the address of the SDL_Rect. (note, i already recognize an error where I am no longer initializing gr->tower, instead im initializing gr->flora[1]

ok First of all I have a stuct for the overall game resources. Ill add comments here.

Code:
typedef struct s_gameresources {
  SDL_Surface *screen; 
  SDL_Surface *invscreen;
  SDL_Surface *temp;
  SDL_Surface *sprite; 
  SDL_Surface *grass; 
  SDL_Surface *actionBar; 
  SDL_Surface *geographic_map; 
  SDL_Surface *tower;
  SDL_Surface *seltarget;
  SDL_Surface *flora[200];  //I am trying to put all static objects like the "tower" and tree here.
  SDL_Surface *tiles[200];
  SDL_Rect rcSprite, rcGrass, rcactionBar, rctower, rcMap, rcInv;
  SDL_Rect rcFlora[200];  //Rectangles for the tower.
  SDL_Event event;
} GameRes;
Next, I have a function called LoadResouces which basically initializes the struct, making surfaces of all the images and their corresponding rectangles.

//Before that I have a memory allocation in main()

gr = (GameRes *) malloc(sizeof(GameRes));
LoadResources(gr, scrmode);

I have certain rectangles still declared in main() which work fine

rcGrass.x = 800; rcGrass.y = 600;
//rctower.x = 600; rctower.y = 128;
rcInv.x = 800; rcInv.y = 600;
rcactionBar.x = 0;
rcactionBar.y = 0; sheight = 32;

Im guessing SDL_Rect is a struct, so its a array of struct within a struct?
HEre is something else. In the case of my "floor tiles", i have declared rcTiles[200][200] but as global within main's C file, and if I do a

SDL_BlitSurface(gr->tiles[0], NULL, screen, &rcTiles[y][x]);

it works fine.
Reply With Quote
  #4   (View Single Post)  
Old 18th April 2011
thirdm thirdm is offline
Spam Deminer
 
Join Date: May 2009
Posts: 248
Default

Sorry, nothing's jumping out at me. rcFlora is indeed an array of structs (SDL_Rect is a typedef of a struct according to its man page) within your GameRes struct. When you do your malloc it should get its memory as long as malloc succeeds (you ought to check btw -- malloc returns NULL if it fails to find memory for you). I don't see a problem there.

All I can think is that maybe you do a free on gr sometime before you use it this last time. Either that or you've got bounds errors or use after free elsewhere that have corrupted your heap and you're seeing some kind of nasty action at a distance effect. I wonder a little about temp in the resource load function. I assume IMG_Load allocates and SDL_FreeSurface releases it. You wouldn't be inadvertently assigning it to something and then using the object it pointed to after it was freed would you?

Maybe now is the time to run this through gdb and get a stack trace at the point where it gets the (I'm guessing) segv signal? That's often a good clue. Also, you may want to read the man page for malloc to see what kind of debugging options it has. I know OpenBSD has some nice options you can set with an environmental variable to help debug memory problems by "failing fast" in various ways. Probably the other BSDs have something similar.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with struct kdu Programming 9 31st March 2011 04:01 PM
BACKDOR in storage array by HP, in P2000/MSA2000 G3 model vermaden News 2 17th December 2010 06:14 AM
Need to access FakeRAID-0 Array on New System Weaseal FreeBSD General 2 17th January 2009 03:48 PM
gmirror array broken stukov FreeBSD General 5 15th July 2008 08:45 PM
RAID array not recognized on new Intel server clevershark OpenBSD Installation and Upgrading 6 14th May 2008 09:20 PM


All times are GMT. The time now is 08:00 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick