Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays, pointers, and Segmentation Faults

Status
Not open for further replies.

Strogian

Technical User
Nov 11, 2000
36
US
Does anyone know exactly what happens when you define an array? I'm asking this, because (in Linux) I don't always get Segmentation Faults, when I would normally expect one. For example:

char s[25];
s[400] = 0;
s[-5] = 0;

With that, I don't think that I got any errors running the program. I expected to get a Segmentation Fault, but I didn't.


Here's another thing that I found kind of strange:

char *s = "Hi there";

That statement works just fine (no errors), but when I try to do this:

*s = 0;

it will give me a segmentation fault. Can anyone explain this stuff to me? Thanks in advance.
 
You can't write in position 0. Some positions are readonly. Try to do:
char *s = "Hi there";
_asm mov ax,cs
_asm mov s,ax
strcpy(s,"xxx"); John Fill
1c.bmp


ivfmd@mail.md
 
P.S.
What I wanted is you to get a segmentation fault. John Fill
1c.bmp


ivfmd@mail.md
 
Oh, well I do get Segmentation Faults when I try to modify anything related to *s. However, if I use the array s[], I don't get them. (I can make a negative index, or just an index larger than the array was defined, and it won't give me any error) I just tested this again:

char s[25];
s[-5] = 'H'
s[400] = 'i';
putchar(s[-5]);
putchar(s[400]);

And it outputs: Hi
 
C++ can be dangerous as it allows you to do just about anything. Here is a bit of explanation of what the [] does.

if you have your char s[25] and do your s[-5] = value, it is the same as *(s-5)= value; Some compilers check to see if your array goes out of range... some do not. In your case, it is not checking. In the compliers eyes, it is treating the address as such, and if you use the pointer + any value you will be able to write to it. You could be writing over bogus memory OR you could be writing over important information. You just dont know.

The reason why it still outputs "Hi" is because the original pointer value did not change, nor did anything before the null terminating character. Why the *s = 0 did not work I do not understand as it should make the first character the null terminating character and the output should be nothing.

As for the setting -5 to "h" and 400 "i", is you got lucky. It makes no sense to me why this would output "Hi" if you didnt set the array to begin with. Try this:

memset(s,0,25);

after you declare the array and do the setting of the array the same with the -5 and the 400. I am curious what you will get for a result.

Matt
 
bit of clarification...

char s[25];
memset(s,0,25); <------------------ here
s[-5] = 'H'
s[400] = 'i';
putchar(s[-5]);
putchar(s[400]);

Sorry about the last post... I should have proofread it :)

Matt
 
Heh, actually, I did set those two spots to output Hi. (s[-5] = 'H', s[400] = 'i';) And the *s = 0 not working is really what confused me. I would understand if s[-5] did not work also, but that does. And I can initialize the pointer *s with &quot;Hi there&quot;, but cannot modify it afterwards. That's even more confusing. =) (I have verified that &quot;Hi there&quot; actually did go into s*, so I know the initialization worked)

Oh, and I'm using C, not C++. :)
 
When you declare an aray x[], usualy you declare it in a read/write memory. Try to do
strcpy(*(x-x/2),&quot;hello world&quot;); and you sure will get a segmentation fault. If not try to put different numbers instead of x/2, and/or change - to + John Fill
1c.bmp


ivfmd@mail.md
 
Are you saying that, when I define some array, it puts that array somewhere in the middle of some special &quot;read/write&quot; memory area, and I'll only get a segmentation fault if I try to access a memory location outside of that entire read/write memory area?
 
>Does anyone know exactly what happens when you define an >array? I'm asking this, because (in Linux) I don't always >get Segmentation Faults, when I would normally expect >one. For example:
>
>char s[25];
>s[400] = 0;
>s[-5] = 0;

Here you're possibly writing to memory you don't own. This produces undefined behvavior.

>With that, I don't think that I got any errors running the >program. I expected to get a Segmentation Fault, but I >didn't.

Because with undefined behavior, *anything* can happen. You could have very well gotten a seg fault, you just got lucky.

>Here's another thing that I found kind of strange:
>
>char *s = &quot;Hi there&quot;;
>
>That statement works just fine (no errors), but when I try >to do this:
>
>*s = 0;

This results in undefined behavior, but for a different reason than above. In this case, s is initially set to point to a string literal which the compiler has the option to store in read-only memory. It is free to do this because the C standard explicitly states that modifying string literals results in undefined behavior.

This is exactly equivalent to:

*(&quot;Hi there&quot;)=0;

You don't own the memory that's used to store string literals! So you shouldn't try to modify them.

>it will give me a segmentation fault. Can anyone explain >this stuff to me? Thanks in advance.

Probably it actually was stored in read-only memory :)

Note that if you were to define s like this:

char s[]=&quot;Hi there&quot;;

And do this:

s[0]=0;

You would be fine, because s is an array of char that has sizeof &quot;Hi there&quot; elements that contains &quot;Hi there&quot;
Russ
bobbitts@hotmail.com
 
Yes, rbobbitt is right. Lot of things in C defined like this. &quot;You should not do&quot;, if you do it will fall in the undefined behavior.

Maniraja S
 
Probably better use
const char *s = &quot;Hi there&quot;;
instead of
char *s = &quot;Hi there&quot;;
And pay more attention to compiler output.
Can save you some time in future.
 
When i declared int i=o in the C program in UNIX
i got segmentation fault but when i removed the value assignment i still got that message.
when i moved the declaration int i three lines below it did not give me the error.
can anyone tell me why?
 
???
Maybe you wanted to start another thread, even if this thread is preety much about the same thing.

Could you be more specific? When you declared... int i = 0? and then moved the declaration three lines below?
What was in those lines?

A cool Heisen(?) Bug appears ussualy in Unix environments when trying to debug an app that crashes and inserting printf statements... Then the app won't crash... This is related usually to memory overwriting made by some statements just around the
Code:
printf
statament.

I'm not sure HOW EXACTLY inserting additional code in the program could skip the crash, this is a very interesting thing i would like to know about! Anybody any ideas? It surely has something to do with the stack and the heap, but since the program and the data as far as i know are stored in different areas of memory (usually) i'm not sure about this.

Arian... Maybe in your code happens the same thing as with the printf stataments [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
char *s = &quot;Hello&quot; declares a pointer of a constant, hence you cannot modify that location.

I would also be curious to know what was in 'those' lines in your code.

Sriks
 
i had declared in main,
{
int i=o;
char *x;
for(....)
{...
.....
}
..
...
}
i got segmentation fault.
i put int i; after removing the assignment i.e. =0
(no other modifications in pgm.)
i still got segmentation fault.

then i put int i; below the declaration char *x;
the pgm worked fine.

i am very much confused.
 
the problem is what you're using a non initialized pointer x. You must initialize it before using. Use only allocated memory. Ion Filipski
1c.bmp


filipski@excite.com
 
Aria
As said by Ion, it is a problem with the usage of the pointer 'x'. Given that code, I don't see a memory allocation done to that pointer. Its failing due to that and not releated to int i=0, etc.

U must be have a malloc call for x before using it.
Check that.
IF u are still confused, send me the complete code. I'll have a look at it.

Sriks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top