LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2004, 06:25 PM   #16
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53

I think i found it.

this works fine:
Code:
#include <string.h>
#include <stdio.h>

int main()
{
	char s[]="bla";
	strcpy(s,"bla");
	printf("%s\n",s);
}
But this causes segmentation fault:
Code:
#include <string.h>
#include <stdio.h>

int main()
{
	char *s="bla";
	strcpy(s,"bla");
	printf("%s\n",s);
}
change your
Code:
 char *dev="tap";
to
Code:
char dev[]="tap";

Quote:
Code:
char s[10];
s = NULL;
sorry.. this is not allowed.

Last edited by perfect_circle; 12-09-2004 at 06:38 PM.
 
Old 12-09-2004, 07:13 PM   #17
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Quote:
Originally posted by perfect_circle
Any array
Code:
char s[10];
is a pointer pointing to a statically allocated memory.
i think if u do :
Code:
char s[10];
s = NULL;
will make you loose the allocated memory.
I don't remember how memset works
but s is still just a pointer.
Code:
itsme@dreams:~/C$ cat -n bzzt.c
     1  #include <stdio.h>
     2
     3  int main(void)
     4  {
     5    char s[10];
     6
     7    s = NULL;
     8    return 0;
     9  }
itsme@dreams:~/C$ gcc -Wall bzzt.c -o bzzt
bzzt.c: In function `main':
bzzt.c:7: incompatible types in assignment
itsme@dreams:~/C$
However, you can do:
Code:
char *s = malloc(10);
s = NULL;
In that event you lose track of your allocated memory. That's why the distinction between ifr_name being a pointer or an array was important.
 
Old 12-09-2004, 07:37 PM   #18
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
I know, i figured that by my self.

why is this:
Code:
#include <string.h>
#include <stdio.h>

int main()
{
	char *s="bla";
	strcpy(s,"bla");
	printf("%s\n",s);
}
causing a segmentation fault?

Code:
#include <stdio.h>

int main()
{
	char *s="bla";
	printf("%s\n",s);
}
this works file.

is char *s="bla"; allowed in ANSI C?
 
Old 12-09-2004, 09:20 PM   #19
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Compilers are not guaranteed to store string literals in writeable memory. The reason your example is failing is because you're trying to modify read-only memory (the memory that s is pointing to).
 
Old 12-10-2004, 07:14 AM   #20
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Code:
char s[10];
s = NULL;
This should not work since s converts to a 'char * const &' lvalue for the assignment which cannot be modified.
ta0kira

[EDIT]
RE perfect_circle: string literals generally use the same memory, but the address is not guaranteed past the expression it is used in. This means that although 'char *s="bla";' leaves s with a pointer to internally used memory, beyond that statement, the memory may not be there anymore. That is why you should copy string literals into static arrays with []. At the point where "bla" is used in the function call in the following line, the memory location for storing a string literal may be changed, making s become invalid. "bla" is loaded into memory just long enough for it to initialize something, such as a local variable or a static array; the pointer to this temporary location should never be used. Your second block is the equivalent of 'printf("%s\n","bla");', which is why it works because you do not have any new string literals before the function call.
ta0kira

[EDIT again]
RE itsme86: it isn't read-only memory; if it was, the pointer could not be converted to a char*; you would get a compiler error in that case. The pointer cannot be changed, which is what I think you mean (try doing 'char *&s = "bla";' in C++ to see what I mean). Or maybe you mean that the mem used for the literal does not belong to the user's process, thereby causing the segfault, just like trying to use a bad pointer, which also points to non-user mem?
ta0kira

Last edited by ta0kira; 12-10-2004 at 07:49 AM.
 
Old 12-10-2004, 09:33 AM   #21
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Please don't correct me with bad information:

This is from: http://www.faqs.org/faqs/C-faq/faq/

There are other references at the bottom if you need them.

Code:
1.32:	What is the difference between these initializations?

		char a[] = "string literal";
		char *p  = "string literal";

	My program crashes if I try to assign a new value to p[ i ].

A:	A string literal can be used in two slightly different ways.  As
	an array initializer (as in the declaration of char a[]), it
	specifies the initial values of the characters in that array.
	Anywhere else, it turns into an unnamed, static array of
	characters, which may be stored in read-only memory, which is
	why you can't safely modify it.  In an expression context, the
	array is converted at once to a pointer, as usual (see section
	6), so the second declaration initializes p to point to the
	unnamed array's first element.

	(For compiling old code, some compilers have a switch
	controlling whether strings are writable or not.)

	See also questions 1.31, 6.1, 6.2, and 6.8.

	References: K&R2 Sec. 5.5 p. 104; ISO Sec. 6.1.4, Sec. 6.5.7;
	Rationale Sec. 3.1.4; H&S Sec. 2.7.4 pp. 31-2.
Anyway, dereferencing a string literal through a pointer is not asking for trouble. It's perfectly valid. Just don't try to modify the string literal.

For instance:
Code:
{
  char *s = "some string";
  char *s2 = "some other string";

  while(*s)
    putchar(*s++);
  putchar('\n');
}
...is perfectly valid code. The memory for the string literals is guaranteed to be there and stay intact.

Last edited by itsme86; 12-10-2004 at 09:38 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what does Segmentation Fault mean ? baronlynx Linux - Newbie 10 10-25-2009 04:32 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Help !!! Segmentation fault mola Linux - Software 3 06-23-2005 11:13 AM
Segmentation fault tejas15_10 Programming 9 06-20-2005 09:12 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 05:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration