Click to See Complete Forum and Search --> : How does one access a class object in memory?


Mike Pliam
March 19th, 2004, 05:23 PM
I dont think I am asking this question properly, but I really dont know how to ask it.

I need to access a section of memory directly, byte by byte. If this block of data were in a binary file, no problem. But how to access the block if it's in memory somewhere.

Actually, I didnt think this question through very well before I asked it. In fact, I plan to write a section of data to memory, encrypt it byte by byte, then save it to disk.

Has anyone dealt with this kind of problem before. Any and all ideas greatly appreciated.

Thanks

Mike

Joe Nellis
March 19th, 2004, 06:51 PM
I plan to write a section of data to memory
Do you have any code to show or do you have an idea how you are going to do this, it may be important to traversing the memory.

A char is one byte. Declare a char* and assign it the beginning memory location of data block (you may have to cast it). Then dereference the char* to a char and perform your encryption algorithm on it. You can increment the char* by one and repeat until you have stepped through the entire data block. You must know the length of the data block and that it is contiguous.

Mike Pliam
March 19th, 2004, 07:12 PM
Thanks, Joe. I think I figured it out.




// allocate a code block in memory
const int codeSegmentSize = 4096;
char *pCode; // ptr to the code segment
char *cursor; // ptr to current code location
pCode = cursor = new char[codeSegmentSize];
memset((void*) cursor, ' ', codeSegmentSize *sizeof(char));

char *code;
code = "What's up, Mike.";
memcpy((void *) cursor, (const void *) code, strlen(code)*sizeof(char));
cursor += strlen(code)* sizeof(char);;

code = "Why are you doing this?";
memcpy((void *) cursor, (const void *) code, strlen(code)*sizeof(char));
cursor += strlen(code)* sizeof(char);;


cursor = pCode;
for(i=0; i<codeSegmentSize; i++) {
cout << cursor[i];
}

delete [] pCode;




This works. Further, if you want to save the memory block to a file,




f.open("test2.txt", ios::binary | ios::out);
if(!f){
cout << "Unable to open " << "test2" << endl;
return 0;
}

i = codeSegmentSize;
f.write( (char*) &i, sizeof(i));
f.write( cursor, i*sizeof(char));

f.close();



This works as well.

I'm posting this because I asked a related question before but got no real answers. Hopefully this will help somebody else. However, I'm not certain what operating system limitations might impose on this code.

Regards,

Mike

sephiroth2m
March 20th, 2004, 10:52 AM
Just consider what happens if strlen(code)> the remains of cursor buffer in ur memcpy functions

Mike Pliam
March 20th, 2004, 12:58 PM
Right you are, Sephi. Thanks.

I wonder how big a block of memory one can safely allocate at the start ? Does anyone know how to check for available 'heap' memory ? Can this be outside of heap memory ?

Mike

Joe Nellis
March 20th, 2004, 02:11 PM
Maybe consider using std::vector. It handles dynamic memory allocation for you and you can access as if it were a contiguous block of memory. As long as you are not using the actual sequential memory location #'s in your encryption algorithm, the behavior should be the same.

#include <vector>
#include <iostream>

int main(){
char encryptionConstant = 1;// simple encryption;
char* code = "What's up, Mike";
std::vector<char> pCode(code,code+strlen(code));
pCode.push_back('?'); // add a char to the end.
int i;
int size = pCode.size();
std::cout << "encrypted data:" << std::endl;
for (i=0;i<size;++i){
pCode[i] += encryptionConstant;
std::cout << pCode[i];
}
std::cout << std::endl;
return 0;
}

Sam Hobbs
March 20th, 2004, 03:14 PM
You did not specify whether the data will ever be 8-bit. If it will be 8-bit (where the high-order eighth bit could be a one) then you should use "unsigned char" instead of "char". Right? Unless you want the eighth bit to be signed, I suppose.

I am not sure but I think that in "strlen(code)*sizeof(char))" the sizeof is unnecessary. I think that sizeof(char) is always one according to the standard.

Since you are using strlen, then you should be able to use strcpy and strcat instead of memcpy. And if you use strncpy and strncat then you can protect the program from overflowing the allocated memory. However if those could work, then you can use ths "string" class; I think it supports embedded nulls. I am not sure, but I think it is possible to derive from basic_string to get "unsigned char" data instead of "char". You could get help with that from STL experts.