Advertisement

Check if memory is writeable

Started by October 30, 2003 05:36 AM
32 comments, last by ZMaster 21 years, 4 months ago
Kevlar-X: Actually I think your understanding of const is a little off.
int y = 5;const int* x = &yy++; // okay - y is not constx++; // okay - pointer x is not constx--; // reset x to point to y*x = 7; // error - p points to a const int so can''t be modified


see c++ FAQ LITE

(I think this is right, but don''t have a compiler available right now to confirm).

Enigma
quote:
Original post by Enigma
Hmm, I suppose that wasn''t a very good example of what I was trying to show. Unfortunately I can''t really think of a decent one. I think the problem I have is that I expect two strings consisting of the same characters to be seperate entities and require strcmp to compare them. In a way I suppose I''m thinking that if
int i1 = 7;int i2 = 7;  

results in two seperate chunks of memory then
char* t1 = "Text";char* t2 = "Text";  

should have the same behaviour.
I realise that char*''s shouldn''t necessarily have the same behaviour as primitive types - I suppose I''m just used to the way my compiler does things. I''m interested though - how would you go about implemented a function like:
char* upper(char* word="none"){   int i = 0;   while (i < std::strlen(word)){      if (word >= ''a'' && word <= ''z''){<br>         word = word + ''A'' - ''a'';<br>      }<br>      i++;<br>   }<br>   return word;<br>}  </pre>  <br><br>OK, so this is a contrived example but still…?<br><br>This whole business certainly raises some interesting questions regarding cross compiler compatibility!<br><br>I''ll be offline for a while now, but I''m going to go home and try this with gcc - I''ll post my findings later.  It''s certainly been an interesting and enlightening discussion,<br><br>Enigma<br><br>EDIT: blasted  interpreted as italics!   <br><br><SPAN CLASS=editedby>[edited by - Enigma &#111;n October 30, 2003 1:27:20 PM]</SPAN><hr height=1 noshade></SPAN></BLOCKQUOTE><br><br>If you want that function, just do this:<br><pre><br>char* upper(char word[]="none"){<br>   int i = 0;<br>   while (i < std::strlen(word)){<br>      if (word >= ''a'' && word <= ''z''){<br>         word = word + ''A'' - ''a'';<br>      }<br>      i++;<br>   }<br>   return word;<br>}  </pre>  <br><br>I know in MSVC''s case, it treats pointers as pointers, and character arrays as character arrays.  If you set a char* to a string, it remembers that strings address, if you set a char[] to a string, it copy''s the string into the char array, hence having multiple copies of the same string like you are used to:<br><br>Borland C++ 5.5 -<br>char *Test = "Test";<br><br>Is the same as  MSVC -<br>char Test[] = "Test";<br><br>I''m not here to say which compiler is correct, but MSVC you can do it BOTH ways with 2 different syntaxes, while Borland does it the same way with either syntax.  </i>  
Advertisement
all this mess.. i know why i moved over to D ..

in the meantime.. this could be interesting for you..

use strings.. then, a const string a = "Test"; is not writable..


if you code in c only.. then.. possibly opening your eyes for other languages could help, if possible.. if not.. then.. urgh.. just don''t do it:D




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud



[edited by - code_evo on November 2, 2003 7:05:02 AM]

This topic is closed to new replies.

Advertisement