Bad permissions for mapped region [duplicate] - c

This question already has answers here:
Why is this string reversal C code causing a segmentation fault? [duplicate]
(8 answers)
Closed 9 years ago.
I get an error when trying to run the following function:
char* reverseInPlace(char* src)
{
//no need to alloc or free memory
int i=0;
int size=mystrlen(src);
for(i=0;i<size;i++)
{
int j=size-i-1;
if(i<j)
{
char temp;
printf("Interchange start %d:%c with %d:%c",i,src[i],j,src[j]);
temp=src[i];
src[i]=src[j];//error occurs here
src[j]=temp;
printf("Interchange complete %d:%c and %d:%c",i,src[i],j,src[j]);
}
}
return src;
}
I call this code like this:
char* rev2=reverseInPlace("BeforeSunrise");
printf("The reversed string is %s\n",rev2);
The error looks like this:
Interchange start 0:B with 12:e
Process terminating with default action of signal 11 (SIGSEGV)
Bad permissions for mapped region at address 0x401165
Why does this error occur?

You are passing a constant string to your function.
String literals are of type char [N + 1] (where N is the length of the array) in C, but modifying them results in undefined behavior. Your compiler should have already issued a warning at that point.
If you wish to modify it then you have to create a copy:
char str[] = "BeforeSunrise";
char* rev2=reverseInPlace(str);

It's because you try to modify a string literal, which is a constant array, i.e. it's read-only.

Related

Character type pointer memory allocation? [duplicate]

This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Why is this string reversal C code causing a segmentation fault? [duplicate]
(8 answers)
Closed 5 years ago.
#include<stdio.h>
#define ASIZE 50
void Reverse(char *str){
int Asize,i=0;
char temp;
// Find the length of the string
while(*(str+i)!='\0'){
i++;
}
Asize=i;
// string reverse
for(i=0;i<(Asize/2);i++){
temp = *(str+i);
//may be below is some error with first input method 1
//but for input method 2 it works perfectly
*(str+i) = *(str+(Asize-(i+1)));
*(str+(Asize-(i+1))) = temp;
}
}
int main()
{
//input method 1. (error aries while i pass the pointer as argument)
char *s = "abcxyz";
//input method 2 (works perfectly while as function parameter)
char s[ASIZE];
scanf("%s",s);
Reverse(s);
printf("%s",s);
}
In the main the input method 1 not working perfectly for the reverse of the string, but the method 2 works.
The concept of mine is not clear with the memory representation of char pointer. Maybe I am not good to make the question correctly but Would someone please make me clear why the method 1 is not working. Thanks in advance for the help.
"abcxyz" is actually a const char[7] type that can decay to a const char* in certain situations.
It is not a char* type; the behaviour on attempting to modify the string is undefined.
char s[ASIZE]; on the other hand is an array of char with automatic storage duration. You are free to modify any element of that as you please.

Why can't I overwrite an array (which passed to another function)with a dynamically allocated array? [duplicate]

This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 6 years ago.
Here is the code I tried:
void check(char*, int);
int main(){
int len;
char* str="stack over flow";
len=strlen(str);
check(str,len);
getch();
return 0;
}
void check(char* str,int len)
{
char* arr;int i=0;
arr=(char*)malloc(sizeof(char)*len);
arr="over flow stack";
while(arr[i]!='')
{
str[i]=arr[i];//here the error is thrown
i++;
}
puts(str);
}
when I tried to print back elements of arr into str I'm getting error.
You're first allocating memory with malloc, then you're changing that pointer to point to "over flow stack", then you're trying to copy characters from "over flow stack" over the constant string "stack over flow", which has undefined behaviour, thus your program crashes.
Using a literal string doesn't allocate any memory - it's being put into the read only space of your program. You can't update this piece of memory.
To get memory you can overwrite you could do something like char* str = strdup("stack over flow");
You're still going to run into issues if the arr in the check function is longer than str

Segmentation fault (core dumped) in C, The C Programming Language by K&R Exercise 2.5 [duplicate]

This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 9 years ago.
I am writing a code for squeeze(s1,s2) that deletes each character in string s1 that matches any character in string s2 and I get "Segmentation fault (core dumped)" when I try to run the program.
I believe the error comes from in how i call the function inside the main(). I am a beginner and I don't know how to call functions. Please help!
#include<stdio.h>
void squeezer(char s[], char c[]);
main()
{
squeezer("abcdefgabcdefgabcdefg", "abcd");
}
void squeezer(char s[], char c[])
{
int i,j,k,z;
for(k=0; c[k] != '\0'; k++) {
for(i=j=0;s[i] != '\0';i++) {
if (s[i] != c[k]) {
s[j++] = s[i];
}
s[j] = '\0';
}
}
for(z=0; z < j; z++)
printf("%c",s[z]);
}
You are passing string literal to your function and then trying to modify it. You can't modify a string literal. Modifying a string literal invokes undefined behavior. In such case you may get either expected or unexpected result You may get segmentation fault or program crash too.
You can change your main function as
int main(void)
{
char s1[] = "abcdefgabcdefgabcdefg";
char s2[] = "abcd";
squeezer(s1, s2);
}
Must read: comp.lang.c FAQ list ยท Question 1.32.
String literals are "constant", read-only. You cannot change them, and that is what squeezer() tries to do.
You're writing to string literals, which is undefined behaviour. If your compiler put them in read-only memory, then your program should segfault. Try making writable copies of your strings in main, like so:
int main(int argc, char** argv)
{
char s[] = "abcdefgabcdefgabcdefg";
char c[] = "abcd";
squeezer(s, c);
return 0;
}
Depending on the C compiler and operating system you are using, the string literal you pass to squeezer may be "read-only" -- i.e. immutable -- at run-time. This is the same mechanism meant to prevent modification of compiled code at run-time.
The fix is to allocte a character array large enough to hold s using malloc or declare a char s[80] in main or as a global variable and then use strcpy to copy your first string literal into s before passing it as the first argument to squeezer.
Alternatively, you could pass the allocated or declared array variable as a third argument to squeezer and copy the "squeezed" string into it. Or, if you want to make squeezer as robust as you can, allocate a result array with malloc in squeezer that is strlen(s) in size, use it to accumulate the "squeezed" letters, and then return the pointer to the allocted array from squeezer whose return type will have changed from void to char *.

Why segmentation fault occurs in the following [duplicate]

This question already has answers here:
Modifying String Literal [duplicate]
(4 answers)
Closed 10 years ago.
I am trying to remove two consecutive duplicate elements from the string.I am getting segmentation fault in the line 16 .even the commented 17th line also get me the same error.
dont worry about my logic of my program.it may be wrong...but i am struck with this error..help me out...explain why i am getting this error
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *st="fvvbbyyr";
int i=0,j,len;
for(len=0;st[len]!='\0';len++);
for(i=0;i<len;i++)
{
if(st[i]==st[i+1])
{
for(j=i+2;j<len;j++)
{
*(st+j-2)=*(st+j);
//st[j-2]=st[j];
}
len = len-2;
i=-1;
}
}
return 0;
}
char *st = "fvvbbyyr";
st points to the anonymous string "fvvbbyyr", which can be located in read-only memory.
*(st+j-2) = *(st+j);
Attempting to modify such value leads to an undefined behavior.
A good habit is to declare the pointer as const char * because a string litteral behaves in this way.
const char *st = "fvvbbyyr";
Then your compiler should print some warnings/errors. Use rather an array:
char st[] = "fvvbbyyr";
BTW, the array subscripting operator [] may make your code more readable.
st[j - 2] = st[j];
Moreover you are accessing to st[i+1] == st[len] once in your loop.

Increment character value of a string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why do I get a segmentation fault when writing to a string?
Here is a small function, was testing something so wrote it. Here i tried to increment a character value of the string literal when i tried doing so i got a segmentation fault. Can you please tell what i am doing wrong here
#include <stdio.h>
int input_string(char *str)
{
printf("%s\n", str);
printf("%c\n", *str);
printf("%c\n", (*str)++); // I get a segmentation fault here, cant i increment the value like this ?
}
void main()
{
char *str = "andrew";
input_string(str);
}
What this char *str = "andrew"; does is create a pointer to a string that MAY be located on .text (where the executable code resides) and trying to modify it is undefined behavior.
Change it for this:
char str[] = "andrew";
It will make a copy of the string in a stack allocated buffer that you can safely modify.
This:
char *str = "andrew";
means what str points to a constant string literal. You will get undefined behavior if you try to change it.
If you want to perform string manipulation, define a character array.

Resources