Strings and memory.

Started by
8 comments, last by Daishim 21 years, 10 months ago
I have the following code as an example...

char *String1 = "String1";
char *String2 = "String2";

int main()
{
     String1 = String2;
     
     cout << String2;

     return 0;
}
 
When I set String1 to String2, do the contents of String1 get lost in memory or is it taken care of? ...since I am actually dealing with an address assignment and not acutal values.

I know only that which I know, but I do not know what I know.
Advertisement
The memory that String1 originally pointed to is part of your executable (it''s a string literal, so the char array actually is written out in your binary executable file). That memory is never "lost" in the traditional sense of allocating memory then being unable to deallocate it due to pointer mis-management, however it is lost in that you can''t point to it again without doing some hex-like digging.
I haven''t studied this but..
I would say that YOU didn''t allocate the memory so you don''t have to deallocate. But yes, the String1 pointer is lost, since you have no idea where it was pointed to anymore. I think the compiler keeps an internal string constant list that you don''t have to worry about for constant strings like what you''ve described.
life is unfair, take advantage of it.UNMB2 - if the link doesn't work, try clicking it :)
Well, per say that it was a user input string. Would that change things? If so, how would I go about correctly disposing of the memory and then assigning the new string to it?

I know only that which I know, but I do not know what I know.
quote:Original post by Daishi
Well, per say that it was a user input string. Would that change things? If so, how would I go about correctly disposing of the memory and then assigning the new string to it?

If you''re writing in C, don''t dispose of user input strings. Simply clear them:
char input[1024];memset( input, 0, sizeof(input) );// get user input... 

If you''re writing in C++, please use std::string. You''ll thank me:
#include <string> std::string String1;...getline( cin, String1 ); 
Trying to assign a new string to String1 could not be done after the line
String1 = String2;
Think of it this way.
Think of you compiled program as a peice of graph paper.
At the top of the paper you have 8 boxes with the letters ''S'' ''t'' ''r'' ''i'' ''n'' ''g'' ''1'' and a 0. Next to that you have another 8 boxes with String2 and a trailing 0.
The next 4 boxes have 0000 in it. The next 4 has 0007.
So the on line reads thus.
------------------------------------------------
S|t|r|i|n|g|1|0|S|t|r|i|n|g|2|0|0|0|0|0|0|0|0|7|
------------------------------------------------
OK. The 0 are the NULL''s that terminate c style stings.
It tells the string functions where to stop.
The 4 digit numbers are the pointers. They referance the start boxes of the strings.
After the line
String1 = String2;

we have
------------------------------------------------
S|t|r|i|n|g|1|0|S|t|r|i|n|g|2|0|0|0|0|7|0|0|0|7|
------------------------------------------------

The memory is never lost. It will be deallocated when your
program is finished.

You cant deallocate manually because you never allocated it manually.

If you want to write over it keep the referance to it and use that but dont write more than the orginal length.
If you put a longer string into that referance, The best you will do is over write other varibles as well as the intended one.
At worst you will over rite the actual code that your going to
execute. This could lead to all sorts of "fun".

Does this help?



It is a good day to code.
Armand -------------------------It is a good day to code.
quote:Original post by Landsknecht
I hate to say this and don''t mean to be rude, but Armand is wrong. An STL string is basically a vector. <font color="Yellow">Landsknecht</font>


Huh? Armand didn''t say anything about STL..

Landsknecht,

I know c++ is well as I''d like but I didn''t think that a decleration of "char *" had changed that much from c.

I know there is a string which behaves like the dynamic version your talking about.
It''s like the Delphi/pascle string type.

If I''m wrong thank you for the info and sorry to Daishi for posibly being confusing and misleading.

I more deal with straight c but I''m trying to learn c++.



It is a good day to code.
Armand -------------------------It is a good day to code.
Gah... My deepest apologies(sp?) , Armand. I just went back and reread his code snippet and see the problem.

I saw the variable names as the type declarations. Thusly, assumed he was using the string class. I totaly overlooked that he was dealing with char*.

My bad - I am REALY sorry about that, man.

Landsknecht

Deleted my previous boneheaded post. Sorry again.

[edited by - landsknecht on May 31, 2002 4:46:01 AM]
My sig used to be, "God was my co-pilot but we crashed in the mountains and I had to eat him..."
But folks whinned and I had to change it.
>Gah... My deepest apologies(sp?) , Armand. I just went back and reread his code snippet and see the problem.

Forget it, easy mistake to make.


It is a good day to code.
Armand -------------------------It is a good day to code.

This topic is closed to new replies.

Advertisement