Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clearing memory through LPCWSTR (Memory Address)

Status
Not open for further replies.

javaguru007

Programmer
Nov 25, 2003
17
PH
Given a value to a Memory Address/Location, can somebody teach me on how to clear the values in the memory ?? I was to place NULLS ('\0') values in the said locations. Thanks.
 
Try:
Code:
   #include <memory.h>
   void clearMem(
         ATYPE* pType
      ,  size_t nSize
   ) {
      memset(pType, 0x00, nSize * sizeof(ATYPE));
      return;
   }
Hope this helps

[tt]________________________________________________________________
[pc2]Roger
Life is a game of cards in which the deck contains only jokers.[/tt]
 
thanks for the help... but the memory still contains the previous values. wasn't able to clear it... thanks for the help...
 
I dont think memset can fail. It returns dest rather than any error code and no exceptions are thrown from inside memset AFAIK. Look up memset in your compiler documentation and make sure you are using it correctly. If you post your code I'm sure we could clear your problem up.
 
LPCWSTR lpcwSource;

while (1) {
lpcwSource = GetValue();
}

Specification of the GetValue() function :
- returns LPCWSTR
- waits for data from a dll every 10 secs.

For example, at first run of the program, the dll returns &quot;This is the data needed.&quot; to the GetValue() function. The GetValue() function returns the address of the data and stores it in the lpcwSource variable. But in the next run the dll returns a value of &quot;&quot;, the GetValue() function still returns the same address to the lpcwSource variable. So when I access the lpcwSource, the variable still returns the data &quot;This is the data needed.&quot; since the memory where it is store was not cleared. What I want to do is, after I get the value from the dll, using the Address Location given by the GetValue() function (which is LPCWSTR), I want to clear the memory so that I wont be able to get the previous value in case the dll does not return anything. Thanks in advance.
 
You don't need to clear all the memory then, you just want the data to be an empty string:
Code:
   while (1) {
      LPCWSTR lpcwSource = GetValue();
      if (*lpcwSource != 0) {
         ProcessData(lpcwSource);
         *lpcwSource = 0;
      }
   }
should do it.

[tt]________________________________________________________________
[pc2]Roger
Life is a game of cards in which the deck contains only jokers.[/tt]
 
What especially do you want?
To clear memory use delete[] operator:
LPWSTR x
x = new wchar_t[.....
delete[]x;

To reset all memory bytes to 0 use memset.

To mark string as empty set the first character to 0 ('\0' or NULL).
x[0] = 0; or *x = 0;

To mark string as not valid you should set the string pointer to 0 (NULL). Be aware of memory leaks, free memory at the first:
x = 0;

Ion Filipski
1c.bmp
 
One thing I forgot to tell you guys, I'm using Embedded Visual C++ 3.0... here are some of the things I noticed :

e.g.:

LPCWSTR lpcwSource;

lpcwSource[0] = 0; error C2166: l-value specifies const object (same with lpcwSource[0] = '\0';, *lpcwSource = 0;)

delete[] lpcwSource; (this call also does not work, whenever the dll returns an empty string, the function GetValue() still returns the same memory address/location, thus pointing again to the previous value)

thanks for you help....
 
You could try:
Code:
   *((LPWSTR) lpcwSource) = 0;


[tt]________________________________________________________________
[pc2]Roger
Life is a game of cards in which the deck contains only jokers.[/tt]
 
An alternative.

Assume that you know that the maximum size of the returned data is, say, 1024 characters:
Code:
   static WCHAR achLastValue[1024 + 1] = &quot;&quot;;
   static WCHAR achValue[1024 + 1] = &quot;&quot;;
   LPCWSTR getNewValue() {
      LPCWSTR lpcwDllValue = GetValue();
      achValue[0] = 0;
      if (_tcscmp(lpcwValue, achLastValue) != 0) {
         _tcscpy(achLastValue, lpcwValue);
         _tcscpy(achValue, lpcwValue);
      }
      return achValue;
   }
This would return something other that &quot;&quot; when the value from [tt]GetValue()[/tt] changes.

Hope thjis helps.


[tt]________________________________________________________________
[pc2]Roger
Life is a game of cards in which the deck contains only jokers.[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top