This site is no longer active and is available for archival purposes only. Registration and login is disabled.

Is this bad code?


Re: Is this bad code?

Postby Digby » Jul 26, 2001 @ 6:22pm

Incrementing the pointer doesn't free the memory.  <br><br>The pointer is analogous to a telescope that lets you look at the contents of memory.  The contents of the memory are still there, you're just looking at a different part of it.  Does that make sense?<br><br>
Digby
pm Insider
 
Posts: 1011
Joined: Apr 29, 2001 @ 1:53pm


Re: Is this bad code?

Postby Dan East » Jul 26, 2001 @ 6:26pm

Okay, I think don't think you have a good grasp of pointers. Let's look at a contrived chunk of memory:<br>[fixed]0x1000 'M' 'y' ' ' 'T' 'e' 'x' 't' '/0'[/fixed]<br><br>In this case we are using a byte to store a letter (which is ANSI / ASCII). So if we look at the byte at memory address 0x1000 we will see it has a value of 77 (remember that all a byte is is a number from 0 to 255). 77 is the ASCII value for M. If we look at the byte at memory location 0x1001 we get 121, which is y. Now in your example you have a pointer to a character, so in our example it would point to the 'M' character:<br>[fixed]char *pTest="My Text";[/fixed]<br>Now all pTest is is a number that refers to a memory location. So in this example pTest has a value of 0x1000, which is the address of the first byte of the string. If we want to see what the actual value is at memory location 0x1000 then we use the indirection operator like so:<br>[fixed]if (*pText=='M') WhatDanSaysIsTrue=TRUE;[/fixed]<br>And...<br>[fixed]if ((int)pText==0x1000) WhatDanSaysIsTrue=TRUE;[/fixed]<br>In the above line I am treating pText like the number it really is, and checking to see if it is pointing to the memory address 0x1000. Now if I perform any math to pText, I only affect the number that the pText pointer is:<br>[fixed]pText+=3;[/fixed]<br>So pText was 0x1000, I incremented it by 3, and since a char is 1 byte, pText's numerical value is incremented by 3*1. So pText is 0x1003. If we look at our memory block above, we see that:<br>[fixed]*pText=='T';[/fixed]<br>What we did has nothing to do with changing the memory that contains the string. We just changed the value of a pointer that happens to point to that memory block.<br><br>Also, neither C nor C++ perform any type of memory management for you. If you don't delete or free it, then it is still there. Other languages like Java try to keep track of what data still has references to it and free what is not used. Don't confuse that with variable scope, which is where you declare a variable in a particular code block or function, and try to use it after it goes out of scope. In that case the data will have been reused by some other variable that is now in scope, and bad things may happen. Here is an example:<br>[fixed]char *MyFunction() {<br>   char szText[10]="Testing";<br>   return szText;<br>}[/fixed]<br>That function returns a pointer to the string szText. Well szText will be out of scope outside that function, so the data will not be valid. In that case you would either allocate the data with new or malloc and return that, or you could declare szText as static, which makes sure it hangs around, or you could create a global variable, which will be around as long as the program is running.<br><br>Dan East
User avatar
Dan East
Site Admin
 
Posts: 5264
Joined: Jan 25, 2001 @ 5:19pm
Location: Virginia, USA


Re: Is this bad code?

Postby RwGast » Jul 26, 2001 @ 6:54pm

Thanks, now that ive read the whole thread 10 or 12 times im pretty sure im getting the hang of it. What was throwing me all of was the concept of char* pszStuff = "blah" becuase i couldnt figure out how it was getting stored since i wasnt making the pointer acually point to a variable. But now i think the compiler allocates the space for "blah" on the stack then set the pointer equal to the address of where it stored "blah" then after the pszStuff goes out of scope is when the memmory where "blah" was stored is freed back to the system. Is this right?
http://www.angelfire.com/ego/esoteric if you like to play quake3 keep your eye on this site
User avatar
RwGast
pm Member
 
Posts: 1123
Joined: Jun 28, 2001 @ 7:36pm
Location: California, USA


Previous

Return to Windows Mobile


Sort


Forum Description

A discussion forum for mobile device developers on the Windows Mobile platform. Any platform specific topics are welcome.

Moderators:

Dan East, sponge, Digby, David Horn, Kevin Gelso, RICoder

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron