472,805 Members | 957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

char* and different memory moving functions like memcpy etc

Is it true that it is not allowed to overwrite statically initialized
char * ?
For example:

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

int main(void)
{
char *dest=" ";
char *src="kakjis";
memcpy(dest,src,1);
printf("%s",dest);
return 0;
}

the gived code compiled with gcc under SuSe 10.1 gives segmentation
fault;
if i replace char *dest=" "; with a correct malloc everything
works.

Could someone please explain this behaivor to me ?

Mar 16 '07 #1
2 1828
ziibrs wrote:
Is it true that it is not allowed to overwrite statically initialized
char * ?
No.
For example:

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

int main(void)
{
char *dest=" ";
char *src="kakjis";
memcpy(dest,src,1);
printf("%s",dest);
return 0;
}

the gived code compiled with gcc under SuSe 10.1 gives segmentation
fault;
if i replace char *dest=" "; with a correct malloc everything
works.

Could someone please explain this behaivor to me ?
It's not allowed to change the contents of a /string literal/.

It might be in write-protected memory (BOOM) or it might be in
write-ignoring memory (WHOOSH) or it might be in plain old
RAM (Mother, he has deaded "me"!).

--
Chris "eccles" Dollin
"Who are you? What do you want?" /Babylon 5/

Mar 16 '07 #2
Thanks, i kinda thought that could be the reason but i was not sure.

Thanks for the answer , it brought some joy into this cloudy day :)

Mar 16 '07 #3

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

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
11
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I've noticed a few threads (full of sound and fury, signifying nothing) here recently about allocation of large memory blocks. I'm about to start on a personal pet project where I'll be using...
6
by: Marcelo | last post by:
Hi, I have a declaration like the following: char tmp; char tmp2; strcpy (tmp, "58257619"); strcpy (tmp2, "");
6
by: Juergen Wohnich | last post by:
Hello, i want to do store int variablen into a char Buffer. Like this: char Buffer; int b = 1447; int *pb; // pb deklariert als pointer auf int pb = &b; // & ist Adress operator, liefert...
42
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: nass | last post by:
hello everyone, i have a bit of problem reading char * strings from a buffer (a shared memory, pointed to by 'file_memory'). basically i have a structure in memory 'ShMem' that can be accessed by...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
7
by: daniel | last post by:
Hello , I always had the feeling that is better to have char arrays with the size equal to a power of two. For example: char a_str; // feels ok char b_str; //feels not ok.
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.