OPTION TO OVERWRITE CONSTANT VARIABLE(STRING) IN C (VC++ IDE)

R

Ramaraj M Bijur

Hi All,

Could anyone help me to resolve following problem in C the IDE used is
Microsoft VC++,

Please let me know the which option in VC++ will do the needful

The problem statement:
overwrite of charcter in constatnt string like char *str="Hi
all"; str[3]='A'; the result of this str points to the string "Hi All"


code pragment


const char * const message[]={
(const char * const)"message -1",
(const char * const)"message -2",
(const char * const)"message -3",
(const char * const)"message -4",
(const char * const)"message -5"
};


int main()
{
int i=0;
char *ptr = (char *)message[1];
ptr[9]='1';
ptr = (char *)message[2];
ptr[9]='1';
ptr = (char *)message[3];
ptr[9]='1';
ptr = (char *)message[4];
ptr[9]='1';
for(;i<5;i++)
puts(message);
return 0;
}


the output is
message -1
message -1
message -1
message -1
message -1

with ragards
ramaraj
 
J

Joona I Palaste

Ramaraj M Bijur said:
Could anyone help me to resolve following problem in C the IDE used is
Microsoft VC++,
Please let me know the which option in VC++ will do the needful
The problem statement:
overwrite of charcter in constatnt string like char *str="Hi
all"; str[3]='A'; the result of this str points to the string "Hi All"

Overwriting characters in constant strings causes undefined behaviour.
Don't do that.
 
R

Ramaraj M Bijur

Hi JIP,
Thank u for replying, Waht u said is logically right still we can do this
without any unexpected behavaiour
we know the constant thing are storing in readonly section it may be in code
or readonly data segment.
Most of the compiler having a option to overwite these data without any
crash and unexpected behavoiur.

GCC compiler having the option -fwrite-strings, u can try for the
confirmatiion.

Thanka a lot
ramaraj

Joona I Palaste said:
Ramaraj M Bijur said:
Could anyone help me to resolve following problem in C the IDE used is
Microsoft VC++,
Please let me know the which option in VC++ will do the needful
The problem statement:
overwrite of charcter in constatnt string like char *str="Hi
all"; str[3]='A'; the result of this str points to the string "Hi All"

Overwriting characters in constant strings causes undefined behaviour.
Don't do that.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Normal is what everyone else is, and you're not."
- Dr. Tolian Soran
 
J

Joona I Palaste

Ramaraj M Bijur said:
Hi JIP,
Thank u for replying, Waht u said is logically right still we can do this
without any unexpected behavaiour
we know the constant thing are storing in readonly section it may be in code
or readonly data segment.

The ISO C standard has no concept of "code segments" or "data segments".
They are OS-specific concepts.
Most of the compiler having a option to overwite these data without any
crash and unexpected behavoiur.
GCC compiler having the option -fwrite-strings, u can try for the
confirmatiion.

Those options are non-standard features and thus off-topic here.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'It can be easily shown that' means 'I saw a proof of this once (which I didn't
understand) which I can no longer remember'."
- A maths teacher
 
R

Ramaraj M Bijur

Hi ,
I don't know ISO C standard so what u said is right accroding to the ISO C
standard but I am working on WINDOWS / VC++ IDE , I know we can change the
charcter in constant string in VC++ by setting the option available in
workplace/project(.dsw/.dsp), alteration of constant string in VC++
environment was allowed and also most of the embbeded C/C++ compiler will
allows becoz I saw in 3 embedded C compiler.

I need one favour ..................where can i get the ISO C standard
preperaly free copy. can u send the link or copy of it.

many thnks
ramaraj
 
S

Serve Laurijssen

Ramaraj M Bijur said:
Hi ,
I don't know ISO C standard so what u said is right accroding to the ISO C
standard but I am working on WINDOWS / VC++ IDE ,

What he was trying to say was that you should ask in a windows/vc++
newsgroup.
 
J

Joona I Palaste

Ramaraj M Bijur said:
Hi ,
I don't know ISO C standard so what u said is right accroding to the ISO C
standard but I am working on WINDOWS / VC++ IDE , I know we can change the
charcter in constant string in VC++ by setting the option available in
workplace/project(.dsw/.dsp), alteration of constant string in VC++
environment was allowed and also most of the embbeded C/C++ compiler will
allows becoz I saw in 3 embedded C compiler.

I know you want to know the proper option for VC++. But this is a C
newsgroup, not a VC++ newsgroup. Please ask in a dedicated newsgroup
for VC++ instead.
I need one favour ..................where can i get the ISO C standard
preperaly free copy. can u send the link or copy of it.

Check out http://www.iso.org.
 
D

Dan Pop

In said:
Could anyone help me to resolve following problem in C the IDE used is
Microsoft VC++,

Please let me know the which option in VC++ will do the needful

What makes you think your compiler is relevant to the C programming
language, the topic of this newsgroup?
The problem statement:
overwrite of charcter in constatnt string like char *str="Hi
all"; str[3]='A'; the result of this str points to the string "Hi All"

Try just doing it and see what happens: if the program doesn't crash,
that's it. Otherwise, figure out the name of a newsgroup dedicated to
programming with VC++ and ask there.

Dan
 
C

Christopher Benson-Manica

Ramaraj M Bijur said:
I need one favour ..................where can i get the ISO C standard
preperaly free copy. can u send the link or copy of it.

You can find a copy of the N869 draft (I believe that's the number -
clc may feel free to correct me) that's a fairly good approximation of
the Standard. The real Standard is not free; the PDF costs $18, I
believe. A good start to writing clc-kosher code is to read the FAQ:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html
 
D

Dan Pop

In said:
Ramaraj M Bijur said:
Could anyone help me to resolve following problem in C the IDE used is
Microsoft VC++,
Please let me know the which option in VC++ will do the needful
The problem statement:
overwrite of charcter in constatnt string like char *str="Hi
all"; str[3]='A'; the result of this str points to the string "Hi All"

Overwriting characters in constant strings causes undefined behaviour.

Wasn't it obvious that the OP knew that? Otherwise, why would have he
asked about the compiler option that renders the behaviour defined?
Don't do that.

Sometimes, you need to do it, especially when you don't have control over
the whole code of the application: a dumb user is passing you the address
of a string literal and you're supposed to modify it in place.

Pre-ANSI C allowed it, which means that identical string literals could
not be merged. ANSI C changed the rules, but most implementors still
played by the old rules, to avoid breaking correct code for no good
reason (and a quite change in the language definition is NO good reason).

In my experience, gcc is the only mainstream compiler merging string
literals. To prevent the users from shooting in their feet, it usually
allocates them in a read only memory segment, so that the unsuspecting
programmer will get a crash. To avoid *unnecessarily* breaking existing
code, it also provides -fwritable-strings. This option not only puts
string literals in writable memory, it also disables string literal
merging.

Dan
 
K

kal

Could anyone help me to resolve following problem in C the IDE used is
Microsoft VC++,

Is better phrased as "... the following problem ... ."

Please let me know the which option in VC++ will do the needful

Is better phrased as "... the option in VC++ which ... ."

The problem statement:
overwrite of charcter in constatnt string like char *str="Hi
all"; str[3]='A'; the result of this str points to the string "Hi All"
code pragment
^^^^^^^^
Shouldn't it be "fragment?"

const char * const message[]={
(const char * const)"message -1",
(const char * const)"message -2",
(const char * const)"message -3",
(const char * const)"message -4",
(const char * const)"message -5"
};

int main()
{
int i=0;
char *ptr = (char *)message[1];
ptr[9]='1';
ptr = (char *)message[2];
ptr[9]='1';
ptr = (char *)message[3];
ptr[9]='1';
ptr = (char *)message[4];
ptr[9]='1';
for(;i<5;i++)
puts(message);
return 0;
}

the output is
message -1
message -1
message -1
message -1
message -1


From the output shown above, the program seems to tbe doing what you
want it to do. So, it is not clear what your question is.

There are no options in VC++ 6.0 to OVERWRITE strings. However, there are
options for string pooling which, depending on the setting, will enable
or disable overwriting of string literals.

- No pooling (can overwrite.)
/Gf - Pools strings (can overwrite.)
/GF - Pools strings and places them in read-only memory (cannot overwrite.)
 
C

Christian Bau

"Ramaraj M Bijur said:
Hi JIP,
Thank u for replying, Waht u said is logically right still we can do this
without any unexpected behavaiour

Using 'u' instead of 'you' is not considered cool, kewl or whatever.
People stop reading after that.
 
C

Christian Bau

"Ramaraj M Bijur said:
Hi ,
I don't know ISO C standard so what u said is right accroding to the ISO C
standard but I am working on WINDOWS / VC++ IDE , I know we can change the
charcter in constant string in VC++ by setting the option available in
workplace/project(.dsw/.dsp), alteration of constant string in VC++
environment was allowed and also most of the embbeded C/C++ compiler will
allows becoz I saw in 3 embedded C compiler.

Wrong newgroup then. Try a WINDOWS / VC++ IDE newsgroup. Also learning
bad habits won't help you once you start programming on a real operating
system.
 
S

Severian

Wrong newgroup then. Try a WINDOWS / VC++ IDE newsgroup. Also learning
bad habits won't help you once you start programming on a real operating
system.

(Christian, this is a broad reaction to several years of reading clc,
and not directed at you!)

I agree in general (everything should be standard C as much as
possible), but is there a "real operating system" for general-purpose
computers? I certainly haven't found it yet (especially in the context
that this newsgroup uses it: that minimal thing that can run a
standard C program but has no concept of processes, threads, stacks,
etc.).

I have studied and worked in O/S design, compiler design, database
design, and portability, and I have programmed in many environments. I
doubt that any pure standard C compiler is going to meet the needs of
any complex, real-world application.

*The key is /understanding and modularizing/ when you have to do
non-standard things*!

Every environment has its needs; so most useful C compilers need to
support things that "The One True C" would not -- for backward
compatibility, for O/S compatibility, for hardware compatibility, for
efficiency, for concurrency, or for other reasons. This is a reality
that every real programmer has to deal with. (And many never learn,
because they don't have to. This applies to Windoze, Linux, BeOS, DOS,
VMS, and embedded programmers; though embedded programmers probably
have an advantage, since their environments are so diverse).

While it is possible to get *most* compilers to tell us what is
non-standard (and I use these options as much as possible), it's not
always a simple task to separate things until you've had quite a few
years experience. I think newbies (and even the pour souls who have
used a single environment for years and have to learn a new one)
deserve a little bit of a break.

Is it not possible to redirect people to the appropriate place without
being condescending?

Sorry. That probably could be shortened, but it's late.
 
S

Serve Laurijssen

Christian Bau said:
Using 'u' instead of 'you' is not considered cool, kewl or whatever.
People stop reading after that.

*You* stop reading after that
 
C

CBFalconer

Serve said:
*You* stop reading after that

He and many others. We also tend to ignore yelling (the subject
line here). If the OP is too ignorant to discover these things on
his own, we are too ignorant and lazy to discuss it with him. Why
should we strain to translate those mewlings?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top