How to extern SDL_Surface array in C? -
i want extern sdl_surface
array 1 function (load_level
) (initialize_balls
). have sdl_surface
(brickim
) dynamic pointer-struct defined inside of load_level
function via malloc
function , declared sdl_surface **brickim
well. got segmentation fault when try access brickim[0]->w
in initialize_balls
function not in load_level
function. piece of code hope me fix.
file1.c
#include <stdio.h> #include <sdl.h> sdl_surface **brickim; /*as global*/ sdl_surface* load_map(char *map_name , int tran ); void load_level(int stage){ int n; sdl_surface **brickim=( sdl_surface **)malloc(sizeof(sdl_surface)*input.n); (n=0;n < input.n;n++){ **brickim[n]=load_map("brick1.bmp",1); /*load sdl_surfaces*/** printf("%d\n",brickim[n]->w); /***access succesfully (returns 100 n times)***/ } ... }} sdl_surface* load_map(char *map_name , int tran ){ sdl_surface *temp,*map; uint32 colorkey; printf("loading bit map %s %d...\n",map_name,tran); temp = sdl_loadbmp(map_name); if (temp == null){ printf("unable load bitmap: %s\n", sdl_geterror()); sdl_freesurface(temp); exit(0); }else{ map = sdl_displayformat(temp); colorkey = sdl_maprgb( map -> format, 132, 0, 12); if ( tran==1 ) sdl_setcolorkey( map, sdl_srccolorkey, colorkey ); sdl_freesurface(temp); } return map; }
file2.c
#include <sdl.h> #include <stdlib.h> #include <stdio.h> extern sdl_surface **brickim;/*brings brickim function*/ void initialize_balls() { printf("into initialization :)\n "); printf("%d \n",brickim[0]->w); /***returns segmentation fault***/ }
$./kuranes loading bit map brick1.bmp 1... level image:) 100 (x10 times) initialization :) violación de segmento (`core' generado)
everything running ok when used single sdl_surface
compiled gcc need dynamically. think basic missing.
your problem seems here. declare global brickim, extern declaration finds. then, in load_level, declare local brickim variable. can declare variable same name in different scopes, global brickim not used within load_level, local 1 will. therefore, while malloc local brickim, don't ever assign global brickim - , therefore if access via extern, null , you'll segfault.
sdl_surface **brickim; /*as global*/ sdl_surface* load_map(char *map_name , int tran ); void load_level(int stage){ int n; sdl_surface **brickim=( sdl_surface **)malloc(sizeof(sdl_surface)*input.n); (n=0;n < input.n;n++){ **brickim[n]=load_map("brick1.bmp",1); /*load sdl_surfaces*/** printf("%d\n",brickim[n]->w); /***access succesfully (returns 100 n times)***/ } ... }}
edit: might want check whole allocation in general. i'm not familiar sdl_surface struct, assume it's not pointer type (it's generic struct). allocation allocating n structures, you're casting if you're allocating n pointers sdl_surface structures. don't cast malloc.
if want n sdl_surface structures, need have:
sdl_surface * brickim = malloc(sizeof(sdl_surface) * n)
if want n pointers sdl_surface structures, need:
sdl_surface ** brickim = malloc(sizeof(sdl_surface *) * n) for( = 0; < n; i++){ // you've allocated array of pointers far, need // allocate n structures brickim[0] = malloc(sizeof(sdl_surface)); }
Comments
Post a Comment