[Home]Segmentation fault

HomePage | Recent Changes | Preferences

Showing revision 8
Darn. I need a really good explanation of this. Time to look at google.

Isn't a seg fault related to trying to access memory not allocated to the program? I know that you can get a seg fault if you try to access a higher value than allocated for an array (or a negative value for that matter).

I think this is true, which means one can do all sorts of really cool abusive things depending on how large the pages are and how they are laid out. My question comes from abusing pointers in strlen to extract array sizes without carrying around stupid size variables all the time. I would have though that strlen should have segfaulted on some code I passed it, but it worked. Thus I need an exact definition of seg fault. google produces too many hits (a googol no doubt) too many to filter through.

Anyone up for this? The above go to Talk later.

strlen doesn't seg fault because all strings are terminated with the '\0' character, so all strlen needs is the start of the string and it can find the end.

Right...

double ** d = (double**)malloc(4*sizeof(double*));
memset(d,0xda,4*sizeof(double*));

Now d points to 16 bytes... strlen((char*)d);
Why doesn't the previous line segfault? strlen dereferences past what d is supposed to point to. I left out the memset on purpose. Without the memset it returns size 0. The point is that strlen hits byte 17 (which is coincidentally '\0') for my code. My question is why strlen will spin past 16, and when does it actually produce a violation? I think I will probably just erase this entry and go write some code to see what happens. Maybe look at the source for malloc or something. (I am not a cs person, just seem to write for more code than I ever really wanted too).

Try storing a definite value in all of the alloced memory blocks. Until a value has been assigned, you can't be sure what they are. strlen may be running in to a '\0' (ASCII # 0) by accident.

HomePage | Recent Changes | Preferences
This page is read-only | View other revisions | View current revision
Edited November 6, 2001 12:09 pm by 198.144.199.xxx (diff)
Search: