I'm working with memory mapped files and I have a block of memory that I've mapped to.
I want to write a function that returns a pointer to a portion of the mapped memory at an offset and length so I can write to it. I've never worked with memory at this level, is what I'm attempting possible?
I know that mapping functions can map to a part of the file at length and offset but I'm not sure if I should make multiple calls to map the memory from the file or just map the memory once and work with the portions I'm interested in using my proposed GetMemory function.
Code:
LPVOID m_lpData;
LPVOID GetMemory(DWORD pos, DWORD length) {
BYTE* buffer = (BYTE*)m_lpData;
buffer += pos;
// how to get a length of the memory?
return ((LPVOID)buffer);
}
i usually use this method for accesing functions in executables, the code is executed from a DLL (always works, except when the function are inside of a class, even tho is public):
.h:
typedef int (*pgObjViewportClose) (OBJECTSTRUCT* gObj); extern pgObjViewportClose gObjViewportClose;
That works, but i can't get it to work if the accesing function is inside of a class, i get Unhandled Exception while trying to access a function inside a class, is there a way to do it?.
I wrote the following C++ constructor, and I get an error - BUFFER too small on strcpy_s
Trace::Trace(const char *str) { if (str) { int len = strlen(str); this->m_name = new char[len+1]; // asking for 'len+1' memory elements of char strcpy_s(m_name, len, str); // **** I get here an error "BUFFER TOO SMALL" ****
Will the realloc just reduce the allocated size and keep the same pointer, or can there be a chance of it finding another place for that allocation ( Meaning that it will expensively move the memory to another location )?
I am trying to create efficient programs by making my dynamic allocations the least resource hungry as possible during runtime.
I am using a pair of pthreads that call a pair of functions for ping-pong dma data transfer that are used in a loop for data transfer from an acquisition board. For a large # of waveforms, I ultimately run out of PC memory and the program stops. At the end of each function I use the delete[] command to clear memory for reuse, but the pointer appears to advance by the array size used for the transfer until the location exceeds the 2 GB I have for memory. I can see this happening using the Task Manager performance button time plot and window of total memory used continuing to increase to the limit. The culprit for one of the functions (2nd) is:
where pci_buffer1 and 2 have been set up and allocated in main. I also had the following line in each function process:
double* Rin = new double[length];
and it used up memory twice as fast. When I transferred the last line to an area just prior to main and used a constant 1024 for length, the program ran twice as far before exceeding system memory, so it appears that both lines were forcing new memory assignments and moving the pointers accordingly. In addition to using the delete[] command to free memory unsucessfuly at the end of each function procedure, I ended up closing the memory at the end of each procedure, then reallocating it again with the idea that the pointer would be set back to the original value, but it still seems to icrement along. So, neither approach appears to allow reuse of the memory because the pointer continues to march along. Using Visual C++ 6.0 to compile.
When I make a shallow copy of pMeshData to OriginalMesh (for static mesh rendering, 2 objects pointing to one COM interface), memory leaks start to occur.When I don't, everything is fine.How to solve it? I addref() to it once (with pMeshData) and ReleaseCOM it in destroyMeshContainer
Remarks **** MeshData.pMesh is 0x00000000 while OriginalMesh is a pointer of some value whose values are 0xfeeefeee
An attempt to create a class which is basically a mimic of vector<int> i don't seem to know how to delete pointer x in a destructor to free memory, also on pushback and pushfront methods, i can't free y when i implement delete[] y; y=NULL; i get some NULL out put when cout 'ing the object in main, why is that happening and how do i free memory y.
#include<iostream> using namespace std; class vectorOfint{ int* x; int size; public: vectorOfint();
I have found that when I dump a C++ object from memory to a file - it seems that there is a misplacement of the last Virtual-Function-Table pointer - in that appears at the beginning. The result is that the gdump information based on this object dump (using green hills) is incorrect. I copied the contents of the gdump information below. The executable is compiled in linux.
Basically MEIO::CameraStatus contains an item that relates to its parent class (line 188). Then it has 18 items that are all Diagnostics::EventsCounter items. Now for each Diagnostics::EventsCounter item there is a Virtual-Function-Table Info Pointer as its last item. All is fine and good except that the last item of MEIO::CameraStatus which is _selfReset is missing its last item of 4 bytes (which is the Virtual-Function-Table Info Pointer ). On the other hand - right before the first Diagnostics::EventsCounter item ("_vidErrors") - there is an extra 4 bytes which happens to be the Virtual-Function-Table Info Pointer. As I said the gdump information file does not see this.
Why the object memory "moves" the last Virtual-Function-Table Info Pointer to the beginning (right before _vidErrors) and is there a way to "fix" this?
/////////////////////////////////////////////////////////////////////////// "MEIO::CameraStatus" val:0x000002f0 ind208,-1) Struct-Begin Info 188: "" offset 0, Parent-Class Private Info C++ Struct ref = 114 189: "_vidErrors" offset 160, Member Info C++ Struct ref = 128 190: "_vdiErrors" offset 480, Member Info C++ Struct ref = 128
I'm trying to keep track of the size of blocks of memory that a pointer points to. No matter what I do, this code below always outputs the integer 8.
If I change 1000 to 5, I still get 8. If I change it to 0, I get 8... If I change it to -1, I get 8. If I change int *a to double *a, I get 8. If I take away the & symbol, I get 8. If I use *& instead, I get 8.
Why? I want it to output 1000. If I change that to 500, I want it to output 500.
int *a; a = malloc(1000 * sizeof(int));
int j = sizeof(&a); printf("%d", j);
I want to build my skills where I can allocate, inspect and change memory sizes.
If I have a pointer variable indicating memory location in which we have stored what user entered and the pointer is of type volatile if the user gives the character 'a' twice , then this character will not be fetched twice from the memory but only when the character is changed???
This is the one meaning of the volatile? the other is that the value will be changed without the program itself change it?
I'm trying some codes about string arrays and taking array length. But i have some problems. I can't get length of string and can't send to a function.
------------------------ #include<iostream> #include<cstring> #include<string> using namespace std; void GetLength(string); std::string Words[]={"table","gun","programming"}; int main() {std::string InputWord;
binary [] is the char array and count is... you know how many times the for loop will turn. So my question is, how do i know the length of the number ? Any function that shows the integer length ? because its impossible to know what count is equal to. like 100 is 3.
I need to be able to find every possible permutation using all possible values of a char. But I have to make it be able to form permutations from a length of 1 to variable N. For example, if N=3, I need it to be able to come up with
How could I do this. (I would like to avoid recursion, since N might be as large as 50 or 60 and using recursion would most likely cause a stack overflow)
I'm working with arrays that might have NULL bytes in them and I'm wondering how to determine the length of the array or store it somewhere with the array (strlen() won't work because of the NULL, right?).
I've found advice like store the length of the array in the first byte of the array, but since sizeof(size_t) is 8 should I leave the first 8 bytes for the length?
Would it be better do define my own structure which would store the array and its length? What's the usual way these things are handled in practice?
I was reading in a book I had about C that an array has at the very end a "null character" signifying the end of the string inside it, "/o". So that made me think, "I guess one needs to declare arrays as having 1 extra space than one expects the array to need. I wonder what will happen if I exceed the array length?" So I made a program to test it out. Here is the program/results:
Code:
#include <stdio.h> int main(void){ char name[3]; printf(" What's your name? "); scanf("%s", name); }
[code]....
As you can see my name was able to fit in the array somehow even though I only allocated 3 bytes to the array. I tried again using my legal first name, Benjamin, and it was still able to fit. How is the array able to hold my name when I declared it as only having 3 bytes?
im new to c++ ,so my question is how do i change a length of a text. for example hi my name is blah blah blah. nice to meet you. (n i want every lines to have 8 chars how do i do that)