Skip to main content
gamedev.net
🔒 Locked

Pointer Hell

Started by Witchcraven 11 replies 450+ views
Original Post
Witchcraven
Witchcraven
Its been a few years since I have done C ( I was a pro), but now I am all rusty. And I am having a problem with pointers that could be considered basic. Essesntially I am trying to make a dynamically allocated array of pointers. I declare my pointer as follows (it is actually declared inside a structure): struct World { struct Section **sections; //array of sections. 32 bit pointers. }; I allocate space for 100 pointers like: w->sections=malloc(sizeof(struct Section *)*100); The w-> is there becuase my array pointer was declared inside a structure. The variable s is a pointer to an allocate Section structure. I try to put it in the array with: w->sections[0]=s; It compiles without warnings or errors, but it seg faults when it is executed. Any ideas? Is it something obvious that I have overlooked?
--------------------------I present for tribute this haiku:Inane Ravings OfThe Haunting JubilationA Mad Engineer©Copyright 2005 ExtrariusAll Rights Reserved
kuphryn
kuphryn
Assuming you are using C and not C++, here is one solution.

w->sections = (World *)(malloc(sizeof(World)*100));

Kuphryn
Witchcraven
Witchcraven
w->sections is of type Section, not World. My World is already allocated. Plus, thats basically what I did with:
w->sections=malloc(sizeof(struct Section *)*100);
when I change it to
w->sections=(Section *)malloc(sizeof(struct Section *)*100);
it makes no difference.
--------------------------I present for tribute this haiku:Inane Ravings OfThe Haunting JubilationA Mad Engineer©Copyright 2005 ExtrariusAll Rights Reserved
rypyr
rypyr
quote:
Original post by Witchcraven
struct World
{
struct Section **sections; //array of sections. 32 bit pointers.
};

I allocate space for 100 pointers like:

w->sections=malloc(sizeof(struct Section *)*100);



K, i haven''t used malloc in awhile, but technically don''t you have to cast the void ptr returned from malloc back into a (Section**) ?

quote:

The variable s is a pointer to an allocate Section structure. I try to put it in the array with:

w->sections[0]=s;

It compiles without warnings or errors, but it seg faults when it is executed. Any ideas? Is it something obvious that I have overlooked?


K, where do you allocate s? where is it freed?

I don''t think w->sections[0]=s; has any problems so it has to be with the pointed-to memory. Haven''t used malloc in a while, do you need to use far for anything?

Regards,
Jeff
Witchcraven
Witchcraven
Ok, here is a little demo of what I mean, except it uses char instead of Section, and it has no structures involed. Why does this demo work, but the problem I originally posted failed.

#include <stdio.h>
//#include "world.h"

int main()
{
/*struct World *w;
struct Section *s;

ManifestWorld(w,2,2);
ManifestSection(s,0,0);
InsertSection(w,s);*/
char l = ''A'';
char *p = &l
char **ar = (char **)malloc(sizeof(char *)*100);
ar[0]=p;
printf("%c",*ar[0]);
return 0;
}
--------------------------I present for tribute this haiku:Inane Ravings OfThe Haunting JubilationA Mad Engineer©Copyright 2005 ExtrariusAll Rights Reserved
Witchcraven
Witchcraven
rypyr:
s is allocated with ManifestSection(s,0,0);

THe function code is:

void ManifestSection(struct Section *s, unsigned int x, unsigned int y) //create a new section
{
s=malloc(sizeof(struct Section));
s->grid=malloc(PTRSIZE*10000);
s->x=x;
s->y=y;
}

THe Section structure is:
struct Section //the section structure. duh
{
void * grid; //will point to 40,000 bytes (10K dwords) to hold pointers.2d array
unsigned int x, y; //the x and y coordinate of this section.
};
--------------------------I present for tribute this haiku:Inane Ravings OfThe Haunting JubilationA Mad Engineer©Copyright 2005 ExtrariusAll Rights Reserved
cmptrgear
cmptrgear
I think you have to do
w->sections=(struct Section *)malloc(sizeof(struct Section *)*100);
unless you have struct Section typdefed as Section in which case you can just do Section * without the struct part

"I may not agree with what you say but I will defend to the death your right to say it."
--Voltaire
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
rypyr
rypyr
I guess I''m really rusty...shouldn''t the cast be:

(struct Section **) ?
JuNC
JuNC
Hehe, your problem is you aren''t actually extracting the pointer from your function.


  
void ManifestSection(struct Section **s, unsigned int x, unsigned int y) //create a new section

{
*s=malloc(sizeof(struct Section));
(*s)->grid=malloc(PTRSIZE*10000);
(*s)->x=x;
(*s)->y=y;
}


Otherwise your s in main is pointing off into the wild blue yonder
Witchcraven
Witchcraven
D''OH!!! So why did it work with ManifestWorld() ? It uses the same mechanism. Or is it just an illusion. I would bet illusion. So to let me clarify for myself, what I was passing was whatever random value s had, not the pointer s itself. So I was allocating memory to some random address.
--------------------------I present for tribute this haiku:Inane Ravings OfThe Haunting JubilationA Mad Engineer©Copyright 2005 ExtrariusAll Rights Reserved
Witchcraven
Witchcraven
Ok, when I used that code I got an odd error:
request for member `sections'' in something not a structure or union.

So I changed ManifestSection to:

void ManifestSection(struct Section **s, unsigned int x, unsigned int y)
{
struct Section *temp=(struct Section *)malloc(sizeof(struct Section));
temp->grid=(unsigned long int **)malloc(PTRSIZE*10000);
temp->x=x;
temp->y=y;
*s=temp;
}

all things seem to be working without seg faults, but is there a better way to do this without a temporary variable. My attempts at typecasting seemed to fail. Thanks in advance.
--------------------------I present for tribute this haiku:Inane Ravings OfThe Haunting JubilationA Mad Engineer©Copyright 2005 ExtrariusAll Rights Reserved
Luke Hutchinson
Luke Hutchinson
This avoids the temporary variable,


void ManifestSection( struct Section **s, unsigned int x, unsigned int y )
{
assert( s );

*s = (struct Section*) malloc( sizeof(**s) );
(*s)->grid = (unsigned long int**) malloc( PTRSIZE * 10000 );
(*s)->x = x;
(*s)->y = y;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.