Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Witchcraven

Pointer Hell

This topic is 5635 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

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?

Share this post


Link to post
Share on other sites
Advertisement
Assuming you are using C and not C++, here is one solution.

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

Kuphryn

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.
};

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • 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!