Thread: [Dev-C++] Pointer string declaration initialization problem (?)
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <and...@su...> - 2005-05-21 15:47:50
|
Dear users,
This is a simple question:
Please, consider the following the two codes with
their respective results:
code 1:
1 -#include <stdio.h>
2 -#include <stdlib.h>
3 -#include <string.h>
4 -
5 -int main()
6 -{
7 - char *name="name1";
8 -
9 - printf("%s\n", name);
10- strcpy(name, "name2");
11- printf("%s\n", name);
12-
13- system("PAUSE");
14- return 0;
15-}
execution results from code 1:
Access violation as a consequence of line 10.
code 2:
1 -#include <stdio.h>
2 -#include <stdlib.h>
3 -#include <string.h>
4 -
5 -int main()
6 -{
7 - char *name;
8 -
10- name = malloc(6*sizeof(char));
11- if(!name) exit(1);
12- strcpy(name, "name1");
13- printf("%s\n", name);
14- strcpy(name, "name2");
15- printf("%s\n", name);
16-
17- system("PAUSE");
18- return 0;
19-}
execution results from code 2:
name1
name2
My question:
Doesn´t a pointer support an implicit memory allocation
through the declaration/inicialization on line 7 from code 1
the same way a
char name[]="name1";
can do?
Regards
Andre
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.322 / Virus Database: 266.11.14 - Release Date: 20/5/2005
|
|
From: Michal M. <mi...@mo...> - 2005-05-21 16:12:01
|
André Macário Barros wrote:
> Dear users,
> This is a simple question:
> Please, consider the following the two codes with
> their respective results:
>
> code 1:
> 1 -#include <stdio.h>
> 2 -#include <stdlib.h>
> 3 -#include <string.h>
> 4 -
> 5 -int main()
> 6 -{
> 7 - char *name="name1";
> 8 -
> 9 - printf("%s\n", name);
> 10- strcpy(name, "name2");
> 11- printf("%s\n", name);
> 12-
> 13- system("PAUSE");
> 14- return 0;
> 15-}
>
> execution results from code 1:
> Access violation as a consequence of line 10.
See documentation about -fwritable-strings
http://gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/C-Dialect-Options.html#index-fwritable_002dstrings-116
and AFAIK in gcc 4.0 this option was even removed
--
Regards,
Michal
ICQ# 175762750
|
|
From: Per W. <pw...@ia...> - 2005-05-21 17:29:13
|
A string "hello" is in most environment considered write-protected. If you
assign a ointer directly to a string constant like this, you will get into
troubles when trying to overwrite the original string.
In some environments this will work, but you really shouldn't depend on
it.
The reason the string constant is often write-protected is that it is
stored directly in the application binary - together with the application
code. Changing it would then be similar to writing self-modifying code.
One other reason why it is problematic to change string constants, is that
multiple parts of the program may use the same constant even if it isn't
obvious. A lot of compilers detects similar text strings at different
parts of the source code and merges them into a single string literal.
/Per W
On Sat, 21 May 2005, Andr=E9 Mac=E1rio Barros wrote:
> Dear users,
> This is a simple question:
> Please, consider the following the two codes with
> their respective results:
>
> code 1:
> 1 -#include <stdio.h>
> 2 -#include <stdlib.h>
> 3 -#include <string.h>
> 4 -
> 5 -int main()
> 6 -{
> 7 - char *name=3D"name1";
> 8 -
> 9 - printf("%s\n", name);
> 10- strcpy(name, "name2");
> 11- printf("%s\n", name);
> 12-
> 13- system("PAUSE");
> 14- return 0;
> 15-}
>
> execution results from code 1:
> Access violation as a consequence of line 10.
>
> code 2:
> 1 -#include <stdio.h>
> 2 -#include <stdlib.h>
> 3 -#include <string.h>
> 4 -
> 5 -int main()
> 6 -{
> 7 - char *name;
> 8 -
> 10- name =3D malloc(6*sizeof(char));
> 11- if(!name) exit(1);
> 12- strcpy(name, "name1");
> 13- printf("%s\n", name);
> 14- strcpy(name, "name2");
> 15- printf("%s\n", name);
> 16-
> 17- system("PAUSE");
> 18- return 0;
> 19-}
>
> execution results from code 2:
> name1
> name2
>
> My question:
> Doesn=B4t a pointer support an implicit memory allocation
> through the declaration/inicialization on line 7 from code 1
> the same way a
>
> char name[]=3D"name1";
>
> can do?
>
> Regards
> Andre
> --
> No virus found in this outgoing message.
> Checked by AVG Anti-Virus.
> Version: 7.0.322 / Virus Database: 266.11.14 - Release Date: 20/5/2005
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by Oracle Space Sweepstakes
> Want to be the first software developer in space?
> Enter now for the Oracle Space Sweepstakes!
> http://ads.osdn.com/?ad_id=3D7412&alloc_id=3D16344&op=3Dclick
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: <and...@su...> - 2005-05-21 20:02:54
|
Thank you all, gentlemen,
Mr. Westermark, Mr. Bohlender, and Mr. Molhanec.
very well explained.
Regards
Andre
-----Mensagem original-----
De: Per Westermark [mailto:pw...@ia...]
Enviada em: sábado, 21 de maio de 2005 14:28
Para: André Macário Barros
Cc: ListaDev
Assunto: Re: [Dev-C++] Pointer string declaration initialization problem
(?)
A string "hello" is in most environment considered write-protected. If you
assign a ointer directly to a string constant like this, you will get into
troubles when trying to overwrite the original string.
In some environments this will work, but you really shouldn't depend on
it.
The reason the string constant is often write-protected is that it is
stored directly in the application binary - together with the application
code. Changing it would then be similar to writing self-modifying code.
One other reason why it is problematic to change string constants, is that
multiple parts of the program may use the same constant even if it isn't
obvious. A lot of compilers detects similar text strings at different
parts of the source code and merges them into a single string literal.
/Per W
On Sat, 21 May 2005, André Macário Barros wrote:
> Dear users,
> This is a simple question:
> Please, consider the following the two codes with
> their respective results:
>
> code 1:
> 1 -#include <stdio.h>
> 2 -#include <stdlib.h>
> 3 -#include <string.h>
> 4 -
> 5 -int main()
> 6 -{
> 7 - char *name="name1";
> 8 -
> 9 - printf("%s\n", name);
> 10- strcpy(name, "name2");
> 11- printf("%s\n", name);
> 12-
> 13- system("PAUSE");
> 14- return 0;
> 15-}
>
> execution results from code 1:
> Access violation as a consequence of line 10.
>
> code 2:
> 1 -#include <stdio.h>
> 2 -#include <stdlib.h>
> 3 -#include <string.h>
> 4 -
> 5 -int main()
> 6 -{
> 7 - char *name;
> 8 -
> 10- name = malloc(6*sizeof(char));
> 11- if(!name) exit(1);
> 12- strcpy(name, "name1");
> 13- printf("%s\n", name);
> 14- strcpy(name, "name2");
> 15- printf("%s\n", name);
> 16-
> 17- system("PAUSE");
> 18- return 0;
> 19-}
>
> execution results from code 2:
> name1
> name2
>
> My question:
> Doesn´t a pointer support an implicit memory allocation
> through the declaration/inicialization on line 7 from code 1
> the same way a
>
> char name[]="name1";
>
> can do?
>
> Regards
> Andre
> --
> No virus found in this outgoing message.
> Checked by AVG Anti-Virus.
> Version: 7.0.322 / Virus Database: 266.11.14 - Release Date: 20/5/2005
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by Oracle Space Sweepstakes
> Want to be the first software developer in space?
> Enter now for the Oracle Space Sweepstakes!
> http://ads.osdn.com/?ad_id=7412&alloc_id=16344&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.322 / Virus Database: 266.11.14 - Release Date: 20/5/2005
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.322 / Virus Database: 266.11.14 - Release Date: 20/5/2005
|