String storage - which part of Memory

When I run the program below, I am getting a run time error "Access violation writing location 0x01125800."

The reason being the string is not stored in heap or stack.

So my question is which part of the memory, string "chris" is stored in the declaration {char *s = "chris"} below?

<<<<<<<<<<<<<<<<<<<<<<Start of Code>>>>>>>>>>>>>>>>>>>>>>
char* reverse(char *s,int start,int last){
char temp;
if(start<=last)
{
temp = s[start];
s[start]=s[last];
s[last]=temp;
reverse(s,start+1,last-1);
}
return s;
}

int main(void)
{
char temp;
char *s="chris";
reverse(s,0,(strlen(s)-1));
getch();
return 0;
}

<<<<<<<<<<<<<<<<<<<<<<<<<End of Code>>>>>>>>>>>>>>>>>>>>>>
char * s is stored on the stack.

It would be stored on the heap in case you declare it in this way:
char * s = new char[]="chris";

However it needn't to be stored on the heap to make the program working. Declare the other two parameters of your reverse function as pointers as well and it should work.
Tried 2 changes to the code

1) char data[] = "chris";
char *s= data;

This worked

2) char *s= new char[]="chris";
This one gave the same error "Access Violation writing location 0x00e47810"

@Danielsson: Tried converting the other 2 parameters to pointers, but was not able to convert the first call to the function where the integer position values are passed, donno how to pass the address for int positions.....

char* s is a variable that lives on the stack, but "chris" is a constant string that is not. I many modern architectures, it's stored in a data segment, and in your case, is marked constant (luckily).

If you want a string declared on the stack, do
char s[] = "chris";
This is something I'm a bit shaky about: stack vs heap. In principle I know what they are, but I don't understand the idea/purporse behind the concepts. When something is in the stack memory is allocated when the function is entered and freed on fuction return. To place something in the heap, on the other hand, the memory must be manually reserved and freed. Are these premises correct? What is the advantage of using the heap?

Thanx
@funprogrammer

Actually it's not essential to pass them as pointers although I made it working this way. But it only complicates the code.

@billinares

The main reason is that the stack is limited so it can be exhausted pretty quickly.
Here is the original code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char* reverse(char *s,int start,int last)
{
    char temp;
    if(start<=last)
    {
        temp = s[start];
        s[start]=s[last];
        s[last]=temp;
        reverse(s,start+1,last-1);
    }
    return s;
}

int main(void)
{
    char temp;
    char *s="chris";
    reverse(s,0,(strlen(s)-1));
    getch();
    return 0;
}



The problem is that the string given here char *s="chris"; is stored in
the program data segment - and is READ only - and therefore cannot be reversed
as reversing the string means writing to it - hence the error.
Last edited on
@guestgulkan

Thanks for your explanation I was not sure myself about this matter although I made the program working when trying to help funprogrammer.
Topic archived. No new replies allowed.