ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This program compiles fine, without any error/warning messages. On TC it even executes okay(if I print the string pointed to by a, it prints Healo), however with gcc on execution it gives a segmentation fault.
Now I know that TC and gcc are different in some ways like :
1) TC uses 16 bit pointers while gcc uses 32 bit pointers
2) gcc conforms to the ansi version of C, more that TC
I may be wrong about these differences but this is what I know.
Whatever the differences may be, I could not understand why it gives the segfault error with gcc.
Help needed.
In the implementation of C that's blowing up on you, constant strings such as the "Hello" are exactly that: constant. They are stored in a segment of memory which is marked read-only. This is not a bug; this is a feature. Consider this snippet:
Compilers can feel free to have alpha and beta pointing to the same string, because in theory it's constant. But unless the implementation puts that string in non-modifiable memory, you'll get "Healo" for beta.
If you want to be able to modify the string, do this:
Code:
alpha=strdup("Hello");
if(alpha==NULL)
{
... couldn't allocate the memory for the modifiable copy; blow up here
}
Thanks a lot wjevans_7d1, that solved it completely. But now I have one more question. I tried the following code :
Code:
#include <stdio.h>
int main()
{
const int a=10;
int *b = &a;
*b=20;
printf("%d\n", a);
return 0;
}
when I compiled it, I got the following warning:
Code:
koodoo@knapsacker:~$ gcc prog.c -o prog
prog.c: In function `main':
prog.c:5: warning: initialization discards qualifiers from pointer target type
koodoo@knapsacker:~$
But when I executed it, I got the following result:
The "const" is the qualifier that's being discarded. Remove that and the warning will go away.
If you're asking why this prints 20, it's because the const only prevents you from changing the variable directly, e.g. by assignment. It doesn't necessarily make the variable unchangable through indirect means like pointers. If you want a truly unchangable constant, you can use a preprocessor define.
Code:
#define A 10
As an interesting side-note, if you pass the -O (for optimize) flag to GCC, this program will actually print out 10. This is probably because the constant is initialized at declaration, so GCC is inlining it at compile-time. It's also a good example of how you can't count on weird things like that to work every time.
k, thanks AdaHacker,
That's what I guessed, but as wjevans_7d1 pointed out for the previous case, shouldn't in this case also the 'a' be stored in a segment of memory which is marked read-only ?.
This in effect overrides const because you are using indirection -
Code:
const int a=10;
int *b = &a;
*b=20;
Since this is implementation dependent (and a bad idea, too) the compiler is free to do what it wants. Since it did not segfault it probably located the contents of "a" to writable memory.
C leaves a lot up to the implementers, so you can't count on that sort of thing.
Also, some compliers may optimize memory, like
Code:
Hi, I am a Penguin.
and
Code:
char *a = "Hi, I am a Penguin."
char *b = "Penguin."
and a points 'H', b points 'P' which is a[11]. In this case, if you modify b by whatever reason, a is also modified. I think "Segfault" happens, if you want to change it.
k, thanks AdaHacker,
That's what I guessed, but as wjevans_7d1 pointed out for the previous case, shouldn't in this case also the 'a' be stored in a segment of memory which is marked read-only ?.
The string constant should be considered a pointer to an array of 'char' stored by the program temporarily wherever it finds appropriate. When initializing a 'char*' you copy that pointer. '10' is a literal constant, however when you use it to initialize an 'int' the value itself is copied and might never reside outside of a register or the variable it's assigned to (aside from the machine language of the function.) In other words, storing '10' in the same way you do a string literal would only make sense if you were doing something like this:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.