LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   why char * is called read only memory [C/C++] (http://www.linuxquestions.org/questions/programming-9/why-char-%2A-is-called-read-only-memory-%5Bc-c-%5D-171014/)

Xconsole 04-16-2004 02:27 PM

why char * is called read only memory [C/C++]
 
why char * is called read only memory. what does it mean ? what is the difference with it from a char array ?

Mara 04-16-2004 04:28 PM

char * is not read-only. You can write it.

Hko 04-16-2004 05:16 PM

Quote:

Originally posted by Mara
char * is not read-only. You can write it.
Depends... See difference between:
Code:

#include <stdio.h>
#include <string.h>

int main()
{
        char *str = "Hi.123456789";

        strcpy(str, "Changed.");
        puts(str);
        return 0;
}

and:
Code:

#include <stdio.h>
#include <string.h>

int main()
{
        char str[20] = "Hi.123456789";

        strcpy(str, "Changed.");
        puts(str);
        return 0;
}

Xconsole:
Are you trying something like the first example above? If so, the answer to your question is that the string is effectively a constant in this case. But most of the times you can write to a char*, i.e. when there is a memory allocated to it with malloc(), of when it is declared like "char str[]". In that case "str" is also a char*, but you can write to the string through the pointer "str".

Xconsole 04-16-2004 09:56 PM

Quote:

you can write to a char*, i.e. when there is a memory allocated to it with malloc()
ok, i verified thsi but no result

Code:

      char* str = new char[20]; // memory allocated
        str = "Hi.123456789";

        strcpy(str, "Changed.");
        puts(str);
        return 0;

this gives segmentation fault.:mad:

jinksys 04-16-2004 10:52 PM

The reason is because pointers point to memory, and char *str points to memory in the data segment which is read only. The difference between char *str="hello" and str[6]="hello" is that while both are declared the same way on the data segment, str[] is treated as readable because it is pushed onto the stack if it is used. *str and str[] are not declared any different by gcc when it creates an executable, the difference is in their use by the programmer. If you want to see what I mean, do a "gcc file.c -S" and view the .s file that is created, it will show you what GCC actually created from your .C

Hko 04-17-2004 05:43 AM

Quote:

Originally posted by Xconsole
ok, i verified thsi but no result
What do you mean by "no result"? It works, and the char* is writable. Try:
Code:

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

int main()
{
        char *str;

        str = malloc(20);
        if (str == NULL) {
                  fprintf(stderr, "Could not allocatie memory.\n");
                  return 1;
        }

        strcpy(str, "Initial string");
        puts(str);

        strcpy(str, "Changed.");
        puts(str);

        free(str);
        return 0;
}

Quote:

Originally posted by Xconsole
Code:

      char* str = new char[20]; // memory allocated
        str = "Hi.123456789";

        strcpy(str, "Changed.");
        puts(str);
        return 0;

this gives segmentation fault.:mad:

Yes. That was exactly my point!
It is one of the few (the only?) cases where a char* is "read only"....

Carunkumar 10-22-2010 08:27 PM

Quote:

Originally Posted by Xconsole (Post 882531)
ok, i verified thsi but no result

Code:

      char* str = new char[20]; // memory allocated
        str = "Hi.123456789";

        strcpy(str, "Changed.");
        puts(str);
        return 0;

this gives segmentation fault.:mad:

It will. char * is a variable. If you make it point to something you can't change, you can't change. In this case after making it point to a memory space(new char[]) which can be written to, you made a second statement that made it point to another "constant", which can't be changed. So you get a segmentation fault.

paulsm4 10-22-2010 08:44 PM

Hi -

Quote:

Q: why char * is called read only memory?

A: It *isn't*.
"char *" is a pointer to a character array.
The characters can either be in read-write, or read-only storage. It depends.
Quote:

Q: What is the difference with it from a char array?

A: Effectively, none.
Kernighan and Ritchie (the guys who invented C) explicitly say array and pointers can usually be used interchangably.

There ARE subtle (and important!) differences. And I know somebody's going to yell at me for "over-simplifying" here.

But honestly, for your purposes, you CAN regard pointers and arrays as "pretty much the same thing". At least for the time being.

And, if you want to explore the issue in more depth, then you should Google for "C programming array pointer". Or, better, read Kernighan and Ritchie.
Quote:

Q: ok, i verified this but no result
...
this gives segmentation fault.

A: As Hko replied, that's EXACTLY the point he was trying to make!
See the example below.
Code:

  // This is allocated from the heap with the C++ "new" operator
  // It's read-write
  // You must use "delete" to dispose of it
  char* str1 = new char[20];

  // This is allocated from the heap with the C "malloc()" function
  // It's also read-write
  // You must use "free()" to dispose of it
  char* str2 = malloc (20);

  // This is allocated from the stack
  // It, too, is read-write
  // It's disposed of when then block exits (usually, at the end of the function)
  char* str3[20];

  // This is a constant
  // It is READ-ONLY
  // You MUST NOT try to write to it!
  char* str4 = "Hi.123456789";

'Hope that helps


All times are GMT -5. The time now is 03:36 AM.