few questions about memory allocation 
Author Message
 few questions about memory allocation

Sample code:

char *buf;

buf = "aaa";
// now buf poins to string "aaa"

buf = "bbb";
// now it points to string "bbb"
// but what happens with memory area that contains string "aaa"?
// does it automatically free()d after I assign buf to new string?



Tue, 18 May 2004 18:51:48 GMT  
 few questions about memory allocation

Quote:

> Sample code:

> char *buf;

> buf = "aaa";
> // now buf poins to string "aaa"

> buf = "bbb";
> // now it points to string "bbb"
> // but what happens with memory area that contains string "aaa"?
> // does it automatically free()d after I assign buf to new string?

No. It's still there.
--
Morris Dovey
West Des Moines, Iowa USA


Tue, 18 May 2004 18:59:29 GMT  
 few questions about memory allocation

Quote:

>> Sample code:

>> char *buf;

>> buf = "aaa";
>> // now buf poins to string "aaa"

>> buf = "bbb";
>> // now it points to string "bbb"
>> // but what happens with memory area that contains string "aaa"?
>> // does it automatically free()d after I assign buf to new string?

> No. It's still there.

Yes it is for sure still there, because it has to have static
storage duration, which means, that it has to be available until
the end of the program. But your program loses any reference to
the array-object that holds the string literal. Whether the
compiler/implementation installs a new object that represents
the same string literal when you try to reconnect to it with
buf = "aaa";
is implementation defined. So the compiler is free to reuse the
array or to introduce a new such array for every usage of the
"aaa" string literal.

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Tue, 18 May 2004 19:36:57 GMT  
 few questions about memory allocation
On Fri, 30 Nov 2001 12:36:57 +0100, "Zoran Cutura"

Quote:

> So the compiler is free to reuse the
>array or to introduce a new such array for every usage of the
>"aaa" string literal.

Or even "baaaz"  :-))

--
rihad



Wed, 19 May 2004 07:51:56 GMT  
 few questions about memory allocation

Quote:

>On Fri, 30 Nov 2001 12:36:57 +0100, "Zoran Cutura"

>> So the compiler is free to reuse the
>>array or to introduce a new such array for every usage of the
>>"aaa" string literal.

>Or even "baaaz"  :-))

Oops, I guess it can't.
Make it "baaaaa"

--
rihad



Wed, 19 May 2004 07:53:57 GMT  
 few questions about memory allocation


Quote:
> Sample code:

> char *buf;

> buf = "aaa";
> // now buf poins to string "aaa"

> buf = "bbb";
> // now it points to string "bbb"
> // but what happens with memory area that contains string "aaa"?
> // does it automatically free()d after I assign buf to new string?

I had thought that you could not legally reassign a pointer to a string
literal -- i.e., it can provoke undefined behavior, such as a segmentation
fault, if your compiler lets you do it at all.  Or is this only the case if
you alter some parts of the content of the memory to which the pointer
points?  That is, I _know_ that it is illegal and potentially dangerous to
do this:

char * buf = "aaa";
buf[2] = "z";

Amittai Aviram



Wed, 19 May 2004 01:30:38 GMT  
 few questions about memory allocation

Quote:


>> Sample code:

>> char *buf;

>> buf = "aaa";
>> // now buf poins to string "aaa"

>> buf = "bbb";
>> // now it points to string "bbb"
>> // but what happens with memory area that contains string "aaa"?
>> // does it automatically free()d after I assign buf to new string?

> I had thought that you could not legally reassign a pointer to a string
> literal -- i.e., it can provoke undefined behavior, such as a segmentation
> fault, if your compiler lets you do it at all.  Or is this only the case if
> you alter some parts of the content of the memory to which the pointer
> points?  That is, I _know_ that it is illegal and potentially dangerous to
> do this:
> char * buf = "aaa";
> buf[2] = "z";

You and the OP are talking about different things. Reassigning a pointer
that is pointing to a string literal is 100% legal and safe. There would
be little point in pointers otherwise.
However, what you are doing above, is not reassigning a pointer, it is
reassigning *the contents of* a pointer. This is quite ghastly and leads
to Undefined Behaviour, when used on string literals.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
   - Douglas Adams



Wed, 19 May 2004 03:36:29 GMT  
 few questions about memory allocation


...

Quote:
>> char * buf = "aaa";
>> buf[2] = "z";

>You and the OP are talking about different things. Reassigning a pointer
>that is pointing to a string literal is 100% legal and safe. There would
>be little point in pointers otherwise.
>However, what you are doing above, is not reassigning a pointer, it is
>reassigning *the contents of* a pointer.

The contents of a pointer is the pointer value that is stored in the
pointer object. There's no problem writing to that as long as the
pointer isn't const. What's happening here is that the object that the
pointer value buf+2 points to is being written to. That is a character
that is part of an object defined by a string literal. Writing to that
sort of object is bad. :-)

--
-----------------------------------------


-----------------------------------------



Thu, 20 May 2004 06:26:45 GMT  
 few questions about memory allocation

Quote:

> I had thought that you could not legally reassign a pointer to a string
> literal -- i.e., it can provoke undefined behavior, such as a segmentation
> fault, if your compiler lets you do it at all.

No, it's fine. It happens every time you send a literal string to a
function, for example puts("string");.

Quote:
> Or is this only the case if you alter some parts of the content
> of the memory to which the pointer points?  That is, I _know_
> that it is illegal and potentially dangerous to do this:

> char * buf = "aaa";
> buf[2] = "z";

ITYM, buf[2] = 'z';

Yes, it causes undefined behaviour. Normally, one of 3 things may happen:
the write succeeds, the write fails silently, or the program crashes.

--
Simon.



Wed, 19 May 2004 02:15:18 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Tree memory allocation, pool memory allocation

2. question about memory allocation

3. General Question about memory allocation

4. URGENT Memory allocation question

5. Question about memory allocation

6. Question regarding memory allocation/deallocation

7. dynamic memory allocation question

8. Memory Allocation Question

9. Questions on memory allocation and gets()

10. Dynamic allocation of memory in a function - question

11. A interesting Memory allocation question

12. ## question on memory allocation for char strings ##

 

 
Powered by phpBB® Forum Software