Re: whether this is valid

From: Prawit Chaivong (arsenal_pui_at_hotmail.com)
Date: 06/29/04


Date: 28 Jun 2004 20:41:33 -0700


"madhur" <madhur@sdf.com> wrote in message news:<2k65g4F18kcpnU1@uni-berlin.de>...
> Hello
>
> char *a="str";
> strcpy(a,"abc");
"str" actually doesn't exist in your (function) stack. BUT It gonna be
located in data segment which is read only. (not 'a' itself, as you
said)
And your *a is a pointer (located in your stack that means it be able
to modified). This variable pointed to address that "str" located.

And then you tried 'strcpy(a,"abc");'. That means you tried to modify
memory that a pointed to. Do you remember I just mentioned that it is
read only data segment.

This code is not gonna work. Let change to char a[]= "str"; like the
other people in group said.

In this case compiler will allocate 4 bytes(Ignor alignment here) for
you in stack and copy "str" from data segment to that memory. And It
can be modify because it's located in stack segment.

Regards,
Prawit Chaivong.
>
> I have learnt that since "a" is a string literal, it might be allocated on
> read only memory on some implementations.
> Is it correct to modify a string literal using this way.
>
> Madhur Ahuja
> India



Relevant Pages

  • Re: Questions about Minix
    ... allocating each component the memory it needs can be a real pain). ... This real makes stuff very "static", because the stack ... necessary) the segment when the application demands more heap than was ... address in the data segment, because the data segment contains only data ...
    (comp.os.minix)
  • Re: Some advice about references on memory management in C
    ... > far on memory management as I would like. ... > on the stack area as other functions. ... everything you allocon the heap. ... The .data segment ...
    (comp.lang.c)
  • Re: Global Variables : Where are they stored ?
    ... If it is at file scope, then only if something initialised the ... same memory area as a. ... most likely be on a stack after some level of function calls. ... "general concept" (stack, heap, data segment, etc.), but it is ...
    (comp.lang.c)
  • Re: Heap of the application
    ... Who decides the Heap of an application? ... Heap is dependent upon the RAM's free memory size which is determined ... (sometimes called the stack segment) ... malloc arena is part of the data segment, ...
    (comp.programming)
  • Re: [PATCH RFC/RFB] x86_64, i386: interrupt dispatch changes
    ... | that instruction sequence. ... the cost is 6 cycles instead of 1 cycles. ... save the current data segment registers (the stack segment and code ... I kept the information that is saved on the stack exactly the ...
    (Linux-Kernel)