* RE: strange behavious
@ 2002-12-23 8:27 Alvarez Alberto-AALVARB1
2002-12-24 5:15 ` Mohammed Khalid Ansari
0 siblings, 1 reply; 13+ messages in thread
From: Alvarez Alberto-AALVARB1 @ 2002-12-23 8:27 UTC (permalink / raw)
To: Mohammed Khalid Ansari, linux c programming mailing list
Hi,
i'll try to answer your question. This may be not be an accurate answer, but i think it'll do.
The way you've assigned values to 'words' is the reason of the failure. The allocation of memory takes place in program data space (the same place where code is), so you can't modify it.
If you want to modify that array, you should dynamically allocate it. Something like this will work,
char **words;
words = calloc(3,sizeof(char **));
words[0]=calloc( strlen("mohammed")+1,sizeof(char *));
words[1]=calloc( strlen("khalid")+1,sizeof(char *));
words[2]=calloc( strlen("ansari")+1,sizeof(char *));
Why does it work when debugging it? I suppose it's due to the debugger behaviour. It isn't a "normal" execution, since the debugger has to control everything in the program. I think that every memory position of the program is allocated at debugger's heap, so it's writable.
Regards,
Alberto Alvarez Besada
Tlf.: +34 914002155
e-mail.: aalvarb1@motorola.com
> -----Original Message-----
> From: Mohammed Khalid Ansari [mailto:khalid@ncst.ernet.in]
> Sent: sabado, 21 de diciembre de 2002 6:25
> To: linux c programming mailing list
> Subject: strange behavious
>
>
>
> Hi,
>
> I was running a simple program to test two dimensional
> pointer. When I
> compiled it and executed, it received segmentaion fault, but when I
> checked it in debugger step by step, it went smoothly and
> produced the
> expected output. I did it many times and got the same
> behavious. Following
> is my program
>
> ###################
>
> #include <stdio.h>
>
> void parse (char **);
> char *words[] = {"mohammed", "khalid", "ansari"};
> int main()
> {
> int num_words, i;
>
> parse (words);
> for (i=0; i<3; i++)
> printf ("%s\n", *(words+i));
> return 0;
> }
>
> void
> parse (char *word[])
> {
> char *buf[] = {"abc", "bcd", "cde"};
> int i=0;
>
> for (i=0; i<3; i++)
> strcpy (*word++, buf[i]);
> }
>
> ##########
>
> Please try yourself and see. What could be the problem.
>
> with regards...
>
> --
>
> **************************************************************
> ************
>
> Mohammed Khalid Ansari Tel (res) : 0091-022-3051360
> Assistant Manager II (off) : 0091-022-2024641
> National Centre for Software Technology Fax :
> 0091-022-2049573
> 8th flr,Air India Build. Nariman Point, E-Mail :
> khalid@ncst.ernet.in
> Mumbai 400021.
>
> Homepage : http://soochak.ncst.ernet.in/~khalid
>
>
> **************************************************************
> ************
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
^ permalink raw reply [flat|nested] 13+ messages in thread* RE: strange behavious
2002-12-23 8:27 strange behavious Alvarez Alberto-AALVARB1
@ 2002-12-24 5:15 ` Mohammed Khalid Ansari
2002-12-25 9:55 ` Glynn Clements
0 siblings, 1 reply; 13+ messages in thread
From: Mohammed Khalid Ansari @ 2002-12-24 5:15 UTC (permalink / raw)
To: Alvarez Alberto-AALVARB1; +Cc: linux c programming mailing list
On Mon, 23 Dec 2002, Alvarez Alberto-AALVARB1 wrote:
> Hi,
> i'll try to answer your question. This may be not be an accurate answer, but i think it'll do.
>
> The way you've assigned values to 'words' is the reason of the failure. The allocation of memory takes place in program data space (the same place where code is), so you can't modify it.
> If you want to modify that array, you should dynamically allocate it. Something like this will work,
that means it should not work for the following code as the memory is
allocated in program data space, but it works...
#######
#include <stdio.h>
int main()
{
char string[] = "mohammed";
strcpy (string, "khalid");
printf ("%s\n", string);
return 0;
}
#######
>
> char **words;
> words = calloc(3,sizeof(char **));
> words[0]=calloc( strlen("mohammed")+1,sizeof(char *));
> words[1]=calloc( strlen("khalid")+1,sizeof(char *));
> words[2]=calloc( strlen("ansari")+1,sizeof(char *));
>
>
>
> Why does it work when debugging it? I suppose it's due to the debugger behaviour. It isn't a "normal" execution, since the debugger has to control everything in the program. I think that every memory position of the program is allocated at debugger's heap, so it's writable.
>
>
> Regards,
>
> Alberto Alvarez Besada
>
> Tlf.: +34 914002155
> e-mail.: aalvarb1@motorola.com
>
>
>
> > -----Original Message-----
> > From: Mohammed Khalid Ansari [mailto:khalid@ncst.ernet.in]
> > Sent: sabado, 21 de diciembre de 2002 6:25
> > To: linux c programming mailing list
> > Subject: strange behavious
> >
> >
> >
> > Hi,
> >
> > I was running a simple program to test two dimensional
> > pointer. When I
> > compiled it and executed, it received segmentaion fault, but when I
> > checked it in debugger step by step, it went smoothly and
> > produced the
> > expected output. I did it many times and got the same
> > behavious. Following
> > is my program
> >
> > ###################
> >
> > #include <stdio.h>
> >
> > void parse (char **);
> > char *words[] = {"mohammed", "khalid", "ansari"};
> > int main()
> > {
> > int num_words, i;
> >
> > parse (words);
> > for (i=0; i<3; i++)
> > printf ("%s\n", *(words+i));
> > return 0;
> > }
> >
> > void
> > parse (char *word[])
> > {
> > char *buf[] = {"abc", "bcd", "cde"};
> > int i=0;
> >
> > for (i=0; i<3; i++)
> > strcpy (*word++, buf[i]);
> > }
> >
> > ##########
> >
> > Please try yourself and see. What could be the problem.
> >
> > with regards...
> >
> > --
> >
> > **************************************************************
> > ************
> >
> > Mohammed Khalid Ansari Tel (res) : 0091-022-3051360
> > Assistant Manager II (off) : 0091-022-2024641
> > National Centre for Software Technology Fax :
> > 0091-022-2049573
> > 8th flr,Air India Build. Nariman Point, E-Mail :
> > khalid@ncst.ernet.in
> > Mumbai 400021.
> >
> > Homepage : http://soochak.ncst.ernet.in/~khalid
> >
> >
> > **************************************************************
> > ************
> >
> > -
> > To unsubscribe from this list: send the line "unsubscribe
> > linux-c-programming" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at http://vger.kernel.org/majordomo-info.html
> >
>
^ permalink raw reply [flat|nested] 13+ messages in thread* RE: strange behavious
2002-12-24 5:15 ` Mohammed Khalid Ansari
@ 2002-12-25 9:55 ` Glynn Clements
2002-12-25 12:31 ` Mohammed Khalid Ansari
0 siblings, 1 reply; 13+ messages in thread
From: Glynn Clements @ 2002-12-25 9:55 UTC (permalink / raw)
To: Mohammed Khalid Ansari
Cc: Alvarez Alberto-AALVARB1, linux c programming mailing list
Mohammed Khalid Ansari wrote:
> > i'll try to answer your question. This may be not be an accurate answer, but i think it'll do.
> >
> > The way you've assigned values to 'words' is the reason of the failure. The allocation of memory takes place in program data space (the same place where code is), so you can't modify it.
> > If you want to modify that array, you should dynamically allocate it. Something like this will work,
>
> that means it should not work for the following code as the memory is
> allocated in program data space, but it works...
>
> #######
> #include <stdio.h>
>
> int main()
> {
> char string[] = "mohammed";
>
> strcpy (string, "khalid");
> printf ("%s\n", string);
>
> return 0;
> }
> #######
Sure; in this case the variable "string" is stored in the data
segment, which is mutable.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 13+ messages in thread* RE: strange behavious
2002-12-25 9:55 ` Glynn Clements
@ 2002-12-25 12:31 ` Mohammed Khalid Ansari
2002-12-25 13:44 ` Glynn Clements
0 siblings, 1 reply; 13+ messages in thread
From: Mohammed Khalid Ansari @ 2002-12-25 12:31 UTC (permalink / raw)
To: Glynn Clements; +Cc: Alvarez Alberto-AALVARB1, linux c programming mailing list
On Wed, 25 Dec 2002, Glynn Clements wrote:
>
> Mohammed Khalid Ansari wrote:
>
> > > i'll try to answer your question. This may be not be an accurate answer, but i think it'll do.
> > >
> > > The way you've assigned values to 'words' is the reason of the failure. The allocation of memory takes place in program data space (the same place where code is), so you can't modify it.
> > > If you want to modify that array, you should dynamically allocate it. Something like this will work,
> >
> > that means it should not work for the following code as the memory is
> > allocated in program data space, but it works...
> >
> > #######
> > #include <stdio.h>
> >
> > int main()
> > {
> > char string[] = "mohammed";
> >
> > strcpy (string, "khalid");
> > printf ("%s\n", string);
> >
> > return 0;
> > }
> > #######
>
> Sure; in this case the variable "string" is stored in the data
> segment, which is mutable.
>
>
The above above answer by Mr Alvarez Alberto-AALVARB1 says that since the
array words is stored in program data space which is not modifiable and
therefore I get segmentation fault. While here I have given an example of
modifiable string and you say that since string is stored in the data
segment, it is modifiable. Please clarify.
with regards...
^ permalink raw reply [flat|nested] 13+ messages in thread* RE: strange behavious
2002-12-25 12:31 ` Mohammed Khalid Ansari
@ 2002-12-25 13:44 ` Glynn Clements
0 siblings, 0 replies; 13+ messages in thread
From: Glynn Clements @ 2002-12-25 13:44 UTC (permalink / raw)
To: Mohammed Khalid Ansari
Cc: Alvarez Alberto-AALVARB1, linux c programming mailing list
Mohammed Khalid Ansari wrote:
> > > > i'll try to answer your question. This may be not be an accurate answer, but i think it'll do.
> > > >
> > > > The way you've assigned values to 'words' is the reason of the failure. The allocation of memory takes place in program data space (the same place where code is), so you can't modify it.
> > > > If you want to modify that array, you should dynamically allocate it. Something like this will work,
> > >
> > > that means it should not work for the following code as the memory is
> > > allocated in program data space, but it works...
> > >
> > > #######
> > > #include <stdio.h>
> > >
> > > int main()
> > > {
> > > char string[] = "mohammed";
> > >
> > > strcpy (string, "khalid");
> > > printf ("%s\n", string);
> > >
> > > return 0;
> > > }
> > > #######
> >
> > Sure; in this case the variable "string" is stored in the data
> > segment, which is mutable.
>
> The above above answer by Mr Alvarez Alberto-AALVARB1 says that since the
> array words is stored in program data space which is not modifiable and
> therefore I get segmentation fault. While here I have given an example of
> modifiable string and you say that since string is stored in the data
> segment, it is modifiable. Please clarify.
Alberto's response was incorrect, at least for Linux/ELF.
Program code is stored in the "text" (code) segment, which is
read-only. Constant static data, such as string literals, are stored
in the "rodata" (read-only data) segment, which is also read-only.
Explicitly-initialised mutable static data are stored in the "data"
segment, which is writable. Implicitly-initialised mutable static data
are stored in the "bss" segment[1], which is also writable[2].
[1] The term arises from an IBM pseudo-op, and stands for "block
started by symbol".
[2] As the BSS segment only contains zeros, its contents don't
actually need to be stored in the executable; however, it does have an
entry in the segment table.
However, most of this information is unnecessary. The main thing to
remember is that a string literal is a pointer to *constant* data
(except when used as an array initialiser, as in the above example).
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 13+ messages in thread
* strange behavious
@ 2002-12-21 5:25 Mohammed Khalid Ansari
2002-12-23 8:19 ` Glynn Clements
0 siblings, 1 reply; 13+ messages in thread
From: Mohammed Khalid Ansari @ 2002-12-21 5:25 UTC (permalink / raw)
To: linux c programming mailing list
Hi,
I was running a simple program to test two dimensional pointer. When I
compiled it and executed, it received segmentaion fault, but when I
checked it in debugger step by step, it went smoothly and produced the
expected output. I did it many times and got the same behavious. Following
is my program
###################
#include <stdio.h>
void parse (char **);
char *words[] = {"mohammed", "khalid", "ansari"};
int main()
{
int num_words, i;
parse (words);
for (i=0; i<3; i++)
printf ("%s\n", *(words+i));
return 0;
}
void
parse (char *word[])
{
char *buf[] = {"abc", "bcd", "cde"};
int i=0;
for (i=0; i<3; i++)
strcpy (*word++, buf[i]);
}
##########
Please try yourself and see. What could be the problem.
with regards...
--
**************************************************************************
Mohammed Khalid Ansari Tel (res) : 0091-022-3051360
Assistant Manager II (off) : 0091-022-2024641
National Centre for Software Technology Fax : 0091-022-2049573
8th flr,Air India Build. Nariman Point, E-Mail : khalid@ncst.ernet.in
Mumbai 400021.
Homepage : http://soochak.ncst.ernet.in/~khalid
**************************************************************************
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: strange behavious
2002-12-21 5:25 Mohammed Khalid Ansari
@ 2002-12-23 8:19 ` Glynn Clements
2002-12-24 5:20 ` Mohammed Khalid Ansari
[not found] ` <Pine.LNX.4.33.0212241046350.31854-100000@soochak.ncst.erne t.in>
0 siblings, 2 replies; 13+ messages in thread
From: Glynn Clements @ 2002-12-23 8:19 UTC (permalink / raw)
To: Mohammed Khalid Ansari; +Cc: linux c programming mailing list
Mohammed Khalid Ansari wrote:
> I was running a simple program to test two dimensional pointer. When I
> compiled it and executed, it received segmentaion fault, but when I
> checked it in debugger step by step, it went smoothly and produced the
> expected output. I did it many times and got the same behavious. Following
> is my program
>
> ###################
>
> #include <stdio.h>
>
> void parse (char **);
> char *words[] = {"mohammed", "khalid", "ansari"};
While the array of pointers will be mutable, the strings themselves
aren't; if you attempt to modify them, you will get a segmentation
fault.
You need to either:
1. replace the array of pointers with an array of arrays instead,
e.g.:
char words[20][] = {"mohammed", "khalid", "ansari"};
or:
2. explicitly allocate mutable storage for the strings, e.g.:
static char word1[] = "mohammed";
static char word2[] = "khalid";
static char word3[] = "ansari";
char *words[] = {word1, word2, word3};
Or you could just explicitly declare the type of the array elements as
"pointer to const char" rather than just "pointer to char", i.e.
const char *words[] = {"mohammed", "khalid", "ansari"};
This should give you a compile-time error for the illegal code rather
than a run-time segfault.
Other alternatives involve:
1. Targeting MS-DOS (which doesn't provide any memory protection, so
*everything* is writable), or
2. Requiring the user to provide the appropriate compiler switches to
make string literals writable (e.g. "-fwritable-strings" for gcc).
However, it's better to just make your code portable.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: strange behavious
2002-12-23 8:19 ` Glynn Clements
@ 2002-12-24 5:20 ` Mohammed Khalid Ansari
2002-12-25 9:50 ` Glynn Clements
[not found] ` <Pine.LNX.4.33.0212241046350.31854-100000@soochak.ncst.erne t.in>
1 sibling, 1 reply; 13+ messages in thread
From: Mohammed Khalid Ansari @ 2002-12-24 5:20 UTC (permalink / raw)
To: Glynn Clements; +Cc: linux c programming mailing list
On Mon, 23 Dec 2002, Glynn Clements wrote:
>
> Mohammed Khalid Ansari wrote:
>
> > I was running a simple program to test two dimensional pointer. When I
> > compiled it and executed, it received segmentaion fault, but when I
> > checked it in debugger step by step, it went smoothly and produced the
> > expected output. I did it many times and got the same behavious. Following
> > is my program
> >
> > ###################
> >
> > #include <stdio.h>
> >
> > void parse (char **);
> > char *words[] = {"mohammed", "khalid", "ansari"};
>
> While the array of pointers will be mutable, the strings themselves
> aren't; if you attempt to modify them, you will get a segmentation
> fault.
>
Is this in ANSI C? Will you please elaborate why is it so with this array
only and how it is different from the example you have given below because
ultimately both examples are same ie array of pointers.
As far as I know if there is an string in a code with sufficient memory,
it can be overwritten unless it is static.
> You need to either:
>
> 1. replace the array of pointers with an array of arrays instead,
> e.g.:
>
> char words[20][] = {"mohammed", "khalid", "ansari"};
>
> or:
>
> 2. explicitly allocate mutable storage for the strings, e.g.:
>
> static char word1[] = "mohammed";
> static char word2[] = "khalid";
> static char word3[] = "ansari";
> char *words[] = {word1, word2, word3};
>
> Or you could just explicitly declare the type of the array elements as
> "pointer to const char" rather than just "pointer to char", i.e.
>
> const char *words[] = {"mohammed", "khalid", "ansari"};
>
> This should give you a compile-time error for the illegal code rather
> than a run-time segfault.
>
> Other alternatives involve:
>
> 1. Targeting MS-DOS (which doesn't provide any memory protection, so
> *everything* is writable), or
>
> 2. Requiring the user to provide the appropriate compiler switches to
> make string literals writable (e.g. "-fwritable-strings" for gcc).
>
> However, it's better to just make your code portable.
>
>
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: strange behavious
2002-12-24 5:20 ` Mohammed Khalid Ansari
@ 2002-12-25 9:50 ` Glynn Clements
0 siblings, 0 replies; 13+ messages in thread
From: Glynn Clements @ 2002-12-25 9:50 UTC (permalink / raw)
To: Mohammed Khalid Ansari; +Cc: linux c programming mailing list
Mohammed Khalid Ansari wrote:
> > > I was running a simple program to test two dimensional pointer. When I
> > > compiled it and executed, it received segmentaion fault, but when I
> > > checked it in debugger step by step, it went smoothly and produced the
> > > expected output. I did it many times and got the same behavious. Following
> > > is my program
> > >
> > > ###################
> > >
> > > #include <stdio.h>
> > >
> > > void parse (char **);
> > > char *words[] = {"mohammed", "khalid", "ansari"};
> >
> > While the array of pointers will be mutable, the strings themselves
> > aren't; if you attempt to modify them, you will get a segmentation
> > fault.
>
> Is this in ANSI C?
Yes.
Note that ANSI C doesn't *require* that constant data are stored in
read-only memory (not all hardware supports memory protection);
however, it does permit it, so any program which assumes that constant
data (e.g. string literals) are mutable isn't portable.
> Will you please elaborate why is it so with this array
> only
It isn't.
> and how it is different from the example you have given below because
> ultimately both examples are same ie array of pointers.
The pointers point to different regions of memory. In your version,
they point into the read-only data ("rodata") segment, which is
read-only. In my second version, they point into the data segment,
which is mutable.
> As far as I know if there is an string in a code with sufficient memory,
> it can be overwritten unless it is static.
No; not all of the process' memory is writable. Program code and
read-only data are stored in read-only areas of memory, so that they
can be shared between multiple processes.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 13+ messages in thread
[parent not found: <Pine.LNX.4.33.0212241046350.31854-100000@soochak.ncst.erne t.in>]
* Re: strange behavious
[not found] ` <Pine.LNX.4.33.0212241046350.31854-100000@soochak.ncst.erne t.in>
@ 2002-12-24 10:12 ` Stephen Satchell
2002-12-25 12:28 ` Mohammed Khalid Ansari
[not found] ` <Pine.LNX.4.33.0212251757430.11856-100000@soochak.ncst.erne t.in>
0 siblings, 2 replies; 13+ messages in thread
From: Stephen Satchell @ 2002-12-24 10:12 UTC (permalink / raw)
To: Mohammed Khalid Ansari, Glynn Clements; +Cc: linux c programming mailing list
At 10:50 AM 12/24/02 +0530, Mohammed Khalid Ansari wrote:
> > > #include <stdio.h>
> > >
> > > void parse (char **);
> > > char *words[] = {"mohammed", "khalid", "ansari"};
> >
> > While the array of pointers will be mutable, the strings themselves
> > aren't; if you attempt to modify them, you will get a segmentation
> > fault.
> >
>
>Is this in ANSI C? Will you please elaborate why is it so with this array
>only and how it is different from the example you have given below because
>ultimately both examples are same ie array of pointers.
>
>As far as I know if there is an string in a code with sufficient memory,
>it can be overwritten unless it is static.
Look at the type for string constants. It is "const array of char". And,
based on the number of compilers that assign this type to a string
constant, it is ANSI C. (It was true for the original ANSI specification,
I don't know about the latest ones.)
This is at variance with K&R C, and has been documented.
Because the string constant has the "const" property, the compiler is free
to include it in a read-only space. In modern computers with worthwhile
operating systems, the hardware will then enforce the "const"-ness of the
string.
This is a good thing, because it avoids some of the more astonishing
results that happens when you change a string constant. Older Unix
compilers would store separate instances of each constant string; modern
compilers will re-use the constant for multiple instances.
The GCC compiler has switches to modify the behavior such that the old
Unix-style mechanisms can be used. RTFM.
Or learn how to deal with the new rules -- which is what I had to do, and
have found my programming life made a little easier by them. They make sense.
Satch
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: strange behavious
2002-12-24 10:12 ` Stephen Satchell
@ 2002-12-25 12:28 ` Mohammed Khalid Ansari
2002-12-25 13:58 ` Glynn Clements
[not found] ` <Pine.LNX.4.33.0212251757430.11856-100000@soochak.ncst.erne t.in>
1 sibling, 1 reply; 13+ messages in thread
From: Mohammed Khalid Ansari @ 2002-12-25 12:28 UTC (permalink / raw)
To: Stephen Satchell; +Cc: Glynn Clements, linux c programming mailing list
On Tue, 24 Dec 2002, Stephen Satchell wrote:
> At 10:50 AM 12/24/02 +0530, Mohammed Khalid Ansari wrote:
> > > > #include <stdio.h>
> > > >
> > > > void parse (char **);
> > > > char *words[] = {"mohammed", "khalid", "ansari"};
> > >
> > > While the array of pointers will be mutable, the strings themselves
> > > aren't; if you attempt to modify them, you will get a segmentation
> > > fault.
> > >
> >
> >Is this in ANSI C? Will you please elaborate why is it so with this array
> >only and how it is different from the example you have given below because
> >ultimately both examples are same ie array of pointers.
> >
> >As far as I know if there is an string in a code with sufficient memory,
> >it can be overwritten unless it is static.
>
> Look at the type for string constants. It is "const array of char".
I haven't defined them to be const, please see above.
And,
> based on the number of compilers that assign this type to a string
> constant, it is ANSI C. (It was true for the original ANSI specification,
> I don't know about the latest ones.)
>
> This is at variance with K&R C, and has been documented.
>
> Because the string constant has the "const" property, the compiler is free
> to include it in a read-only space. In modern computers with worthwhile
> operating systems, the hardware will then enforce the "const"-ness of the
> string.
>
> This is a good thing, because it avoids some of the more astonishing
> results that happens when you change a string constant. Older Unix
> compilers would store separate instances of each constant string; modern
> compilers will re-use the constant for multiple instances.
>
> The GCC compiler has switches to modify the behavior such that the old
> Unix-style mechanisms can be used. RTFM.
>
> Or learn how to deal with the new rules -- which is what I had to do, and
> have found my programming life made a little easier by them. They make sense.
>
> Satch
>
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: strange behavious
2002-12-25 12:28 ` Mohammed Khalid Ansari
@ 2002-12-25 13:58 ` Glynn Clements
0 siblings, 0 replies; 13+ messages in thread
From: Glynn Clements @ 2002-12-25 13:58 UTC (permalink / raw)
To: Mohammed Khalid Ansari; +Cc: Stephen Satchell, linux c programming mailing list
Mohammed Khalid Ansari wrote:
> > > > > #include <stdio.h>
> > > > >
> > > > > void parse (char **);
> > > > > char *words[] = {"mohammed", "khalid", "ansari"};
> > > >
> > > > While the array of pointers will be mutable, the strings themselves
> > > > aren't; if you attempt to modify them, you will get a segmentation
> > > > fault.
> > > >
> > >
> > > Is this in ANSI C? Will you please elaborate why is it so with this array
> > > only and how it is different from the example you have given below because
> > > ultimately both examples are same ie array of pointers.
> > >
> > > As far as I know if there is an string in a code with sufficient memory,
> > > it can be overwritten unless it is static.
> >
> > Look at the type for string constants. It is "const array of char".
Actually, no; for backwards compatibility with K&R, string literals
are treated as "char []" rather than "const char []".
If they were implicitly qualified with "const", you would get a
warning when you try to modify them or pass them to functions which
might try to modify them.
> I haven't defined them to be const, please see above.
Stephen never suggested that you had defined them as "const"; he was
saying that they are "const" automatically (which isn't technically
the case; declaring something as "const" and storing it in a read-only
segment are two different things).
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 13+ messages in thread
[parent not found: <Pine.LNX.4.33.0212251757430.11856-100000@soochak.ncst.erne t.in>]
* Re: strange behavious
[not found] ` <Pine.LNX.4.33.0212251757430.11856-100000@soochak.ncst.erne t.in>
@ 2002-12-25 21:39 ` Stephen Satchell
0 siblings, 0 replies; 13+ messages in thread
From: Stephen Satchell @ 2002-12-25 21:39 UTC (permalink / raw)
To: Mohammed Khalid Ansari; +Cc: Glynn Clements, linux c programming mailing list
At 05:58 PM 12/25/02 +0530, Mohammed Khalid Ansari wrote:
> > Look at the type for string constants. It is "const array of char".
>
>I haven't defined them to be const, please see above.
You misunderstand, the inherent type for string constants is "const array
of char". Let's look at what you are doing.
the statement
char *words[] = {"mohammed", "khalid", "ansari"};
defines a array of three pointers to char, and initializes those pointer to
three strings which have the type "const array of char", which is promoted
to "pointer to const char". A way to rewrite this statement so the result
is identical but shows the exact typing is this:
const char w1[9] = {'m','o','h','a','m','m','e','d',0};
const char w2[7] = {'k','h','a','l'.'i'.'d',0};
const char w3[7] = {'a','n','s','a','r','i',0};
char *words[3] = {w1, w2, w3};
That is what the "shorthand" in the first statement expands to, if you
apply the rules of ANSI to it.
Just remember that constants have a type associated with them, and you HAVE
LITTLE CONTROL OF THE TYPE assigned to constants. GCC allows you to say
whether a "plain" char is signed or unsigned, but not (to the best of my
knowledge) whether you can remove the "const" attribute from a constant
character.
Investigate whether you can ask GCC to use the old Unix conventions for
strings...but I wouldn't recommend it for new programs, and would strongly
recommend doing the Right Thing(tm) and port the old code to the new
semantics, for they will keep you from going down a road that is full of
astonishment.
Stephen
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2002-12-25 21:39 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-12-23 8:27 strange behavious Alvarez Alberto-AALVARB1
2002-12-24 5:15 ` Mohammed Khalid Ansari
2002-12-25 9:55 ` Glynn Clements
2002-12-25 12:31 ` Mohammed Khalid Ansari
2002-12-25 13:44 ` Glynn Clements
-- strict thread matches above, loose matches on Subject: below --
2002-12-21 5:25 Mohammed Khalid Ansari
2002-12-23 8:19 ` Glynn Clements
2002-12-24 5:20 ` Mohammed Khalid Ansari
2002-12-25 9:50 ` Glynn Clements
[not found] ` <Pine.LNX.4.33.0212241046350.31854-100000@soochak.ncst.erne t.in>
2002-12-24 10:12 ` Stephen Satchell
2002-12-25 12:28 ` Mohammed Khalid Ansari
2002-12-25 13:58 ` Glynn Clements
[not found] ` <Pine.LNX.4.33.0212251757430.11856-100000@soochak.ncst.erne t.in>
2002-12-25 21:39 ` Stephen Satchell
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).