473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to identify a pointer to a string literal

Hello,

If we have c code like what's below, we will get an error because in
the stringManipulator function we attempt to modify a string literal on
the second call to the function. My question is: in the
stringManipulator function, is there any way to identify if the char*
is pointing to writable memory space to to a string literal?

Sure, the example is trivial, but we run into such problems when the
function call stack gets 20 functions deep! Thanks in advance!

int stringManipulator(char *str)
{
/* check here to see if string is writable? */
str[0] = 'A';
str[1] = 'B';
}

int main()
{
char string_array[10] = "thisisok";

stringManipulator(string_array);
stringManipulator("thisisbad"); /* shouldn't do this */
}

Jan 7 '06 #1
12 2601

<we***********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hello,

If we have c code like what's below, we will get an error because in
the stringManipulator function we attempt to modify a string literal on
the second call to the function. My question is: in the
stringManipulator function, is there any way to identify if the char*
is pointing to writable memory space to
I assume you mean 'rather than'
to a string literal?
No there is not.

If you want to ensure that the function will not
modify what the pointer points to, declare it as 'const':

int stringManipulator(const char *str)
Sure, the example is trivial, but we run into such problems when the
function call stack gets 20 functions deep!
The function call nesting level has nothing to do with
this.

int stringManipulator(char *str)
{
/* check here to see if string is writable? */
str[0] = 'A';
str[1] = 'B';
}

int main()
{
char string_array[10] = "thisisok";

stringManipulator(string_array);
stringManipulator("thisisbad"); /* shouldn't do this */
}


If the function must modify what the pointer points to,
then you can't declare it as const, and must resort to
(gasp) self-discipline in order to refrain from passing
a pointer to a non-modifiable object. There's also another
thing you should take care with: make sure that you only
pass a pointer to a string of at least a length of two
characters (plus one more for the terminator). E.g.
this will produce undefined behavior:

char a[] = "";
stringManipulator(a);
Finally, note that the language reserves to the implementation
any external names beginning with 'str' followed by a lower case
character. Your name 'stringManipulator' violates this rule.

-Mike
Jan 7 '06 #2
we***********@gmail.com a écrit :
If we have c code like what's below, we will get an error because in
the stringManipulator function we attempt to modify a string literal on
the second call to the function.


Define the string literal with the 'const' qualifier. It will ring a
bell when you pass it eo the function.

int stringManipulator(char *str)
{
/* check here to see if string is writable? */
str[0] = 'A';
str[1] = 'B';
}

int main()
{
char string_array[10] = "thisisok";

stringManipulator(string_array);
stringManipulator((char const *)"thisisbad"); /* shouldn't do this
(Awsome) */

/* or */

char const * p = "thisisbad";
stringManipulator(p);

return 0;
}

You also can activate a compiler/lint checking (like gcc's
-Wwrite-strings), at last at debug time.

--
A+

Emmanuel Delahaye
Jan 7 '06 #3
we***********@gmail.com said:
Hello,

If we have c code like what's below, we will get an error because in
the stringManipulator function we attempt to modify a string literal on
the second call to the function. My question is: in the
stringManipulator function, is there any way to identify if the char*
is pointing to writable memory space to to a string literal?
If the function doesn't write to the string you accept as a parameter, make
it const char *. Don't pass string literals as arguments except where const
char * is expected.
stringManipulator("thisisbad"); /* shouldn't do this */


So don't. The time to get this right is when you're writing the calling
code.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 7 '06 #4
On Fri, 6 Jan 2006 15:03:11 UTC, we***********@gmail.com wrote:
Hello,

If we have c code like what's below, we will get an error because in
the stringManipulator function we attempt to modify a string literal on
the second call to the function. My question is: in the
stringManipulator function, is there any way to identify if the char*
is pointing to writable memory space to to a string literal?

Sure, the example is trivial, but we run into such problems when the
function call stack gets 20 functions deep! Thanks in advance!

int stringManipulator(char *str)
{
/* check here to see if string is writable? */
str[0] = 'A';
str[1] = 'B';
}

int main()
{
char string_array[10] = "thisisok";

stringManipulator(string_array);
is defined
stringManipulator("thisisbad"); /* shouldn't do this */
undefined behavior.
}

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Jan 8 '06 #5
Herbert Rosenau wrote:
On Fri, 6 Jan 2006 15:03:11 UTC, we***********@gmail.com wrote:
Hello,

If we have c code like what's below, we will get an error because in
the stringManipulator function we attempt to modify a string literal on
the second call to the function. My question is: in the
stringManipulator function, is there any way to identify if the char*
is pointing to writable memory space to to a string literal?

Sure, the example is trivial, but we run into such problems when the
function call stack gets 20 functions deep! Thanks in advance!

int stringManipulator(char *str)
{
/* check here to see if string is writable? */
str[0] = 'A';
str[1] = 'B';
}

int main()
{
char string_array[10] = "thisisok";

stringManipulator(string_array);


is defined
stringManipulator("thisisbad"); /* shouldn't do this */


undefined behavior.


If you are going to take the time to post a response, please at least
read the post you are responding to first. The OP obviously knows that
the latter is undefined, he was asking if there is any way for the
program to know at runtime that it was passed a pointer to a string
literal.

Robert Gamble

Jan 9 '06 #6
Ok, so if a compiler or lint can determine that the string is not
modifiable, then why can't I?

Jan 9 '06 #7

<we***********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Ok, so if a compiler or lint can determine that the string is not
modifiable, then why can't I?


You can - if you've a brain like a C parser, and use it to parse to an
arbitrary depth.
Jan 9 '06 #8

<we***********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hello,

If we have c code like what's below, we will get an error because in
the stringManipulator function we attempt to modify a string literal on
the second call to the function. My question is: in the
stringManipulator function, is there any way to identify if the char*
is pointing to writable memory space to to a string literal?

Sure, the example is trivial, but we run into such problems when the
function call stack gets 20 functions deep! Thanks in advance!

int stringManipulator(char *str)
{
/* check here to see if string is writable? */
str[0] = 'A';
str[1] = 'B';
}

int main()
{
char string_array[10] = "thisisok";

stringManipulator(string_array);
stringManipulator("thisisbad"); /* shouldn't do this */
}


Maybe I'm reading more 'in-between the lines' that I ought to here, but, I
suspect you may have a codebase that you're porting to a platform [or that
uses a different compiler for the same platform] that allows this undefined
behaviour? And, that you'd like to run the code on your main development
platform *and* find such things? If the answer's yes, besides using lint
etc, there is a hack that you might use to get some big hints as to where
your code's got this particular problem, I won't go into it here [unless
what I say hits the nail on the head] - as it's damn awful!


Jan 9 '06 #9
In article <11*********************@g44g2000cwa.googlegroups. com>,
we***********@gmail.com <we***********@gmail.com> wrote:
Ok, so if a compiler or lint can determine that the string is not
modifiable, then why can't I?


Short answer: *You* can, but your program can't.
Slightly longer answer: Because they have access to the source code; your
program (at runtime) does not.

Longer answer: Subject to the usual caveat of:

Not portable. Can't discuss it here. Blah, blah, blah.

You could probably figure it out yourself with a little experimentation;
figure out what sort of addresses string literals have vs. what sort of
addresses allocated space has (with all the standard caveats of: on your
platform, your compiler, etc, etc) and then write the functions to check
the value of the pointer and behave accordingly.

Jan 9 '06 #10
we***********@gmail.com wrote:
Ok, so if a compiler or lint can determine that the string is not
modifiable, then why can't I?


Are you a compiler or lint? If not, what makes you think you should be
able to do what they can do?

As to why a program can't determine whether a string is modifiable or
not, that is simple. The language does not provide any mechanism for
determining whether a string is modifiable or not. This allows (but does
not require) pointers to be just memory addresses rather than having to
have type information encoded in them as well.

Note that although I say that pointer can just be simple memory
addresses, they can also be something rather more complex, so don't take
what I've said as carte blanch permission to cast away warnings about
incompatible pointer types, or to pass the wrong pointer type to a
varidac function or anything like that.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 9 '06 #11
ga*****@yin.interaccess.com (Kenny McCormack) writes:
In article <11*********************@g44g2000cwa.googlegroups. com>,
we***********@gmail.com <we***********@gmail.com> wrote:
Ok, so if a compiler or lint can determine that the string is not
modifiable, then why can't I?

[snip]
You could probably figure it out yourself with a little experimentation;
figure out what sort of addresses string literals have vs. what sort of
addresses allocated space has (with all the standard caveats of: on your
platform, your compiler, etc, etc) and then write the functions to check
the value of the pointer and behave accordingly.


Bad idea. The resulting function would be *extremely*
system-specific, assuming it's possible to write it consistently at
all. It would almost certainly break if you port the code to another
system. It would be likely to break on a new release of your
compiler, linker, or operating system, or when you invoke your
compiler or linker with a different set of options, or for any of a
number of other reasons.

Your time would be better spent avoiding the problem in the first
place by not writing code that depends on this.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 9 '06 #12
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
ga*****@yin.interaccess.com (Kenny McCormack) writes:
In article <11*********************@g44g2000cwa.googlegroups. com>,
we***********@gmail.com <we***********@gmail.com> wrote:
Ok, so if a compiler or lint can determine that the string is not
modifiable, then why can't I?

[snip]
You could probably figure it out yourself with a little experimentation;
figure out what sort of addresses string literals have vs. what sort of
addresses allocated space has (with all the standard caveats of: on your
platform, your compiler, etc, etc) and then write the functions to check
the value of the pointer and behave accordingly.

Bad idea. The resulting function would be *extremely* system-specific,
assuming it's possible to write it consistently at all.
So noted and stipulated.
It would almost certainly break if you port the code to another system.
It would be likely to break on a new release of your compiler, linker, or
operating system, or when you invoke your compiler or linker with
a different set of options, or for any of a number of other reasons.
I guess that, despite my best efforts, I didn't put in QUITE enough caveats
of:
(with all the standard caveats of: on your platform, your compiler, etc, etc)
to keep you quiet. Oh well, I'll be sure to do better next time.

I suppose I should have included a few "Blah, blah, blah"s as well.
Your time would be better spent avoiding the problem in the first
place by not writing code that depends on this.


I think it is pretty clear what situation the OP is in. This is a one-time
deal, and involves a large existing code base.

Jan 9 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
22
by: lokman | last post by:
Hi, In the following code, can someone tell me the difference between *p++ and p++ ? I can see both achieve the same result. Thanks a lot !
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
4
by: Paul | last post by:
hi, there, In the following codes, If I change the varible "string "in main() to char* string=" this is the test string "; the program will be crashed. can someone tell me why and how...
9
by: TuAmigoFiel | last post by:
Why does the following give a segmentation fault? void breakme(char* st) { char* cp = st; *cp = 'x' // This is the problem line. } int main() { char* mine = "teststringfortestingpurposes";...
25
by: dis_is_eagle | last post by:
Hi.I have a question on the following statement. char* a="hello"; The question is where "hello" gets stored.Is it in some static area ,stack or heap.I have observed that attempting to modify...
41
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
19
by: mail1779205 | last post by:
I (certainly) hope I know what this function does: char *fun(void){ char *ptr = "Hello World"; return ptr; } It returns a pointer to a string stored somewhere in the memory and is...
156
by: Lame Duck | last post by:
Hi Group! I have a vector<floatvariable that I need to pass to a function, but the function takes a float * arguement. That's OK, I can convert by doing &MyVector.front(), but when I get back a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.