1

Is it possible to determine whether a string can not be written on the side it had been passed to as a char * string?

my code:

#include <stdio.h>

typedef enum string_type 
    { READ_ONLY, READ_WRITE }
    String_type;

String_type isvalid(char *s);

void test(char *s){
    if(isvalid(s))
        printf("OK\n");
    else
        printf("NG\n");
}

int main(void){
    char data_str[] = "data_str";

    test("data_str");// fails
    test(data_str);// works

    return 0;
}

String_type isvalid(char *s){
    //Can it be determined by this?
    //I don't think this is a portable way.
    return (void *)s > (void *)main ? READ_ONLY : READ_WRITE;
}
alk
  • 69,737
  • 10
  • 105
  • 255
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • 1
    So if I get this correctly, you want to test if a char* passed into a function is a string literal or a variable that you can modify? – CompuChip Nov 17 '13 at 09:06
  • @CompuChip to change if you say `isvalid` is possible, but literal want to determine the Read-Only. – BLUEPIXY Nov 17 '13 at 09:09
  • As others have already commented, this can't be done in a standard way and I don't know of any *reliable* non-standard way to do it either. I know you know this question is coming @BLUEPIXY, so here it goes: what are you trying to do that you think requires this? Knowing that, maybe we can offer a solution to the problem that doesn't involve this kind of checking. – Nik Bougalis Nov 17 '13 at 09:29
  • @NikBougalis What kind is the solution? – BLUEPIXY Nov 17 '13 at 09:38
  • 2
    @BLUEPIXY I can't answer until I know what *underlying* problem you are trying to solve? Or is this detection of string literals the *actual* problem? – Nik Bougalis Nov 17 '13 at 09:38
  • @NikBougalis We would want to be that of the literal string that can not be rewritten in fact. – BLUEPIXY Nov 17 '13 at 09:44
  • @BLUEPIXY you are continuing to say the same thing. WHY are you trying to solve this problem? I.e. what _real_ problem do you think _this_ is the solution to? – CompuChip Nov 17 '13 at 09:45
  • @CompuChip I want to prevent in advance an error from occurring when performing the rewriting of string. – BLUEPIXY Nov 17 '13 at 09:49
  • Please see my update answer, on a possibity to detect such mistakes. – alk Nov 17 '13 at 10:07
  • the conclusion, It is how good that there is no. – BLUEPIXY Nov 17 '13 at 10:13
  • Even though this is a ridiculous question, I don't know the reason for downvoting. – BLUEPIXY Jun 19 '17 at 02:30

4 Answers4

8

Is it possible to determine whether a string is “read-only”?

There is no standardised way to do this.

Using addresses might also fail miserably on even one platform, as linkers are free to place data where they (the developers mastering them) want.


Update:

To prevent code passing literals to functions which themselfs try to modify the memory being passed in you might like to run a statical source analyser against the code.

A good tool to use for this is splint.

alk
  • 69,737
  • 10
  • 105
  • 255
  • I'm really lost here, but what ever happened to `const char *` ? see: http://stackoverflow.com/questions/890535/what-is-the-difference-between-char-const-and-const-char – ebyrob May 12 '17 at 00:44
2

Totally arbitrary, but perhaps you can make it stable on your platform.

#include <stdio.h>
#include <stdlib.h>

int isliteral(char *s) {
    char *lit = "totally literal";
    char nolit[] = "some";
    return abs(nolit - s) > abs(lit - s) ;
}

int main(){
    char sa[] = "not literal";
    char *sl  = "literal";
    printf("%d\n", isliteral(sa));
    printf("%d\n", isliteral(sl));
    return 0;
}

Gives:

0
1
perreal
  • 94,503
  • 21
  • 155
  • 181
  • 2
    I'm sorry perreal, but I think this is a _very_ bad idea. There is no guarantee this code will work on another system, another compiler or even another day, in some cases. I agree with Nik Bougalis' comment that @BLUEPIXY should describe the _actual_ problem and maybe we can find an actual _solution_. – CompuChip Nov 17 '13 at 09:44
  • Thanks. This(new version) behavior I wanted. I think it is better than my way. – BLUEPIXY Nov 17 '13 at 10:02
0

If you want to change a string I suggest you create a copy of the string, change that, and leave it up to the caller what they want to do with it.

char* change(char* original)
{
  int len = strlen(original);
  char* copy = new char[len + 1];
  strcpy(copy, original);
  copy[0] = 'H';
  return copy;
}

int main()
{
  char* data_str = "Bello world";

  const char* first_changed = change(data_str);
  const char* second_changed = change("Bello world");

  cout << first_changed << endl;
  cout << second_changed << endl;

  delete[] first_changed;
  delete[] second_changed;

  return 0;
}

Even better: use std::string and be freed of the burden of new/deleteing your character arrays.

CompuChip
  • 9,143
  • 4
  • 24
  • 48
0

constant is a compile time concept , but there is one exception that is for pointers , since c-string = char * so there will be possible to know if a char* is read-only at runtime :

#include <iostream>
#include <typeinfo>
int main()
{
    char * str1;
    const char *  str2 = "str2";
    std::cout<<"char * : "<<typeid(str1).name()<<"\n";
    std::cout<<"const char * : "<<typeid(str2).name()<<"\n";
    system("pause");
}

the output is :

char * : Pc
const char * : PKc
Appuyez sur une touche pour continuer... // press any key to continue
Hassen Dhia
  • 555
  • 4
  • 11
  • I wanted a way in C. BTW Is string literal to become a value that was decided? – BLUEPIXY Mar 28 '16 at 01:30
  • acctualy there is some tricks to do that in c , something like this http://c-faq.com/struct/taggedunion.html , but i prefer to install a signal listener and then write to that string if a signal is raised then it is read-only string , there is full documentation of signals over the web – Hassen Dhia Mar 28 '16 at 01:45
  • here is the code that allows you to know if a c-string is read-only : `#include #include #include void handler(int) { printf("const char * "); } int main() { signal(SIGSEGV,handler); const char * str1 = "dhia"; char * str2 = (char*) str1; str2[0]='a'; }` – Hassen Dhia Mar 28 '16 at 01:54
  • fine , it will stay there for someone else i enjoyed solving the problem – Hassen Dhia Mar 28 '16 at 02:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107495/discussion-between-hassen-dhia-and-bluepixy). – Hassen Dhia Mar 28 '16 at 02:05
  • I do not need, after all of the place, I think it useless. – BLUEPIXY Mar 28 '16 at 02:08