how to avoid the segfault from Address 0x1cd00000103 out of bounds

After allocating memory for some variables, segfault is often to happen, due to the same reason: Address 0x1cd00000103 out of bounds

It is welcome to recommend some treatments. Thanks

e.g.

is_done = 0x1cd00000103 <Address 0x1cd00000103 out of bounds>,
hood = 0x23c00000247,
c = 0x1a900000149 <Address 0x1a900000149 out of bounds>,
site = 0x40a000004fc,
site2 = 0x3ba0000059f,
des = 0x47200000199 <Address 0x47200000199 out of bounds>,
asc = 0x47500000056 <Address 0x47500000056 out of bounds>,

Perhaps you should post your code. If for legal or privacy reasons you cannot, then maybe post code from one such function that has these variables.

Let me show it here:

                    node-&gt;is_done = \(int *\) malloc\(sizeof\(int\)\);
                    node-&gt;hood = \(double *\) malloc\(sizeof\(double\)\);
                    node-&gt;c = \(int *\) malloc\(sizeof\(int\)\);
                    node-&gt;site = \(double *\) malloc\(sizeof\(double\)\);
                    node-&gt;site2 = \(double *\) malloc\(sizeof\(double\)\);double
                    node-&gt;des = \(double *\) malloc\(sizeof\(double\)\);
                    node-&gt;asc = \(double *\) malloc\(sizeof\(double\)\);

                   //null checking                        

                    node-&gt;is_done = 0;
                    node-&gt;hood = 0;
                    node-&gt;c = 1;
                    node-&gt;site = 0;
                    node-&gt;site2 = 0;
                    node-&gt;des = 0;
                    node-&gt;asc = 0

But, use of these varialbes in code will lead to segfaults, e.g. if(node->is_done) { --- }

check these varialbes, those showing 'address 0x1cd00000103 out of bounds' will finally lead to segments

I should remind that some nodes but not all the nodes in a tree will give segfaults when checking if(node->is_done)

It is said that unsuccessful initialization is the source of 'Address 0x1cd00000103 out of bounds'. Concretely speaking, how to avoid such unsuccessful initialization occur in some nodes?

Can we take some measures?

I think you are confused about what you are trying to do! In the examples above, you are allocating memory one address at a time and storing the pointers in your structure inside "node". So node->hood presumably holds a pointer to 4 (or 8) bytes (the size of "double").

Also, where did you allocate memory for node? Presumably you have:

node = (struct node_t*)malloc(sizeof(struct node_t));

Right!! these are pointers. What does 'out of bounds' mean? More important, how to avoid it? Clearly null checking can not make effect in this respect.

Program received signal SIGSEGV, Segmentation fault.
520 while( tr->root->is_done) { ----}

(gdb) p tr->root->is_done
$2 = (int *) 0x3f500000539 <Address 0x3f500000539 out of bounds>
(gdb) pt tr->root->is_likelihood_done
type = int *

-----------------------------------------------------------------
struct node_t {

int *is_done;
double *hood;
int *c;
double *site;
double *site2;
double *des;
double *asc;

}

Can you provide us with a short complete example program which exhibits this behaviour?
And provide details of your compilation environment.

"out of bounds" means "invalid memory address". You avoid it by properly mallocing memory before using it.

I hate to say this, but you seem very confused about pointers. I think you need to read a very concise summary on C pointers.

In the code you provide, you are trying to assign values to the address...

node->des = 0;

You should instead be doing:

*(node->des) = 0;

And your loop should be:

while ( *(  tr->root->is_done ) ) {
... 
}

Also, root itself is a pointer. So you first need to allocate it:

tr->root = malloc(sizeof(struct node_t));
tr->root->hood = malloc(sizeof(double*));

"tr" is also a pointer, and this needs to be allocated too, but I have no idea what kind of structure it is.

Also, this structure is very unusual. I cannot imagine why you want every element to be a pointer to something!

They are pointers. This is sure. But there is some mistakes in the code I gave before. The assignment should use * before the pointers. I am sorry for carelessness when posted. But when checking with 'while ( *( tr->root->is_done ) )', the same segfault happen. And corresponding pinter address still be 'out of bounds". In my code, such statements exist.

This is a package coded by a previous member. He habits to use such structure for varialbes which control execution. A large package means that it is not easy to change all the strutures of this kind.

What's more, I think this is really a topic worth discusssion.

I don't think anyone here can help you further unless you post the actual code.

I agree with Otheus. When you provide actual code perhaps then we can help you.

Thanks. I can understand. Let me first try to fix it.

Please post the code otherwise it is very hard to understand what's going on.