C++ :: Pointer To Memory At Offset And Length
Aug 26, 2013
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);
}
View 14 Replies
ADVERTISEMENT
Apr 1, 2014
Is this the correct way to calculate the offset of a 3d array? Also is the dynamic allocation for it in the main program correct?
Some how it's not working fine after I input the values for some values of x,y,z e.g when the const ints are 2.
#include <iostream>
using namespace std;
const int x = 2;
const int y = 2;
[Code]....
View 8 Replies
View Related
Sep 28, 2013
I would to learn how could i calculate the offset of simple c program?
Lets say that I have that simple program:
Code:
#include <stdio.h>
int main() {
int x=1;
printf("x = %d", x);
return 0;
}
View 9 Replies
View Related
Jan 1, 2013
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;
.cpp
pgObjViewportClose gObjViewportClose = (pgObjViewportClose) 0x04F1940;
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?.
View 1 Replies
View Related
Aug 3, 2013
I am having a problem with a small exercise program. The program works the first time through the loop but not the second. It faults on this line:
cin >> *(pint + i1);, with this error: pint <Unable to read memory>.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int MAX(5);
int count(4);
int value(MAX);
[Code] ....
View 5 Replies
View Related
Jan 24, 2014
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" ****
[Code] .....
m_name is a private data member of type char* .
View 3 Replies
View Related
Apr 29, 2013
What I'm trying to do is:
int *p;
someType memoryLocation;
cout<<"Enter your memory location: ";
cin >> memoryLocation;
p = memoryLocation;
cout << *p;
I was just messing around with some code, and was curious to if this was possible.
View 6 Replies
View Related
Nov 5, 2014
There is a part in the lesson that explains how malloc is used to allocate free memory to a pointer and gives us 2 examples:
Code:
float *ptr = malloc( sizeof(*ptr) ); and Code: float *ptr;
ptr = malloc( sizeof(*ptr) );
From my logic in the first case we allocate the memory to *ptr and in the second to ptr.
It's a bit confusing, am I missing something?
View 14 Replies
View Related
Jun 3, 2013
I have an integer pointer and i want its address without allocating memory,
main() {
int *a;
cout<<a;
}
This is giving 00000000 and its but obvious. Now if i use address of a (&a) along with *a,
main() {
int *a;
cout<<a;
cout<<&a;
}
'cout<<a' gives me a constant address but 'cout<<&a' gives me different address.
what is the reason behind & and why behaviour of 'cout<<a' changes when using with &.
View 8 Replies
View Related
Aug 10, 2014
If I do this:
void* testPtr = malloc ( 1000 )
And then this:
testPtr = realloc ( testPtr, 500 )
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.
View 2 Replies
View Related
Feb 13, 2013
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:
unsigned char* dataBuffer2 = (unsigned char *) (pci_buffer2.UserAddr);
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.
View 1 Replies
View Related
Aug 25, 2012
Code:
HRESULT CAllocMeshHierarchy::CreateMeshContainer(PCTSTR Name,
const D3DXMESHDATA* pMeshData, const D3DXMATERIAL* pMaterials,
const D3DXEFFECTINSTANCE* pEffectInstances, DWORD NumMaterials,
const DWORD *pAdjacency, ID3DXSkinInfo* pSkinInfo,
D3DXMESHCONTAINER** ppNewMeshContainer
[Code] ....
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
View 1 Replies
View Related
Feb 25, 2015
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();
[Code] .....
View 6 Replies
View Related
Jul 29, 2014
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
[Code] .....
View 4 Replies
View Related
Jan 8, 2014
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.
View 4 Replies
View Related
Jun 26, 2014
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?
View 4 Replies
View Related
Nov 12, 2014
Exception Details: System.ArgumentOutOfRangeException: Length cannot be less than zero. Parameter name: length
using System;
using System.Data;
using System.Configuration;
[Code]....
View 2 Replies
View Related
Nov 20, 2013
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;
[Code]...
And how can send Matrix to other function?
View 2 Replies
View Related
Oct 3, 2013
The programming problem is supposed to take a decimal number from a user and turn it into a binary number. Here is my code:
for (int i=0; i< count; i++) {
binary[i] = decimal % 2;
decimal = decimal/2;
} cout << binary[2] << endl;
decimal is the number entered by the user.
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.
View 3 Replies
View Related
Jan 29, 2015
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
0x00
0x01
.......
0x00 0x00
0x01 0x01
.......
0xff 0xff 0xfe
0xff 0xff 0xff
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)
View 3 Replies
View Related
Apr 16, 2013
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?
View 7 Replies
View Related
Apr 20, 2013
Code:
char line[BUFSIZ];
while ( fgets(line, sizeof line, file) != NULL ){
llen = strlen(line);
printf("%d - %s
",llen,line);
}
I get a full line printed but my llen is the size of my buffer. how do i get the total size om my line from the beginning to " "
View 3 Replies
View Related
Feb 9, 2014
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?
View 13 Replies
View Related
Nov 23, 2013
Any good function that get the length of an integer in C++
I do know of str.length(), however i want something similar for integers
View 1 Replies
View Related
May 24, 2013
i having a code to pass in array as argument, but the length returned is 1. This is not match with the array size.
int Getsize(int Array[])
{
int len = sizeof(Array)/sizeof(int);
cout << len << "
";
}
int main()
{
int X[] = {45, 12, 54, 83, 41, 36};
getsize(X);
}
View 19 Replies
View Related
Oct 5, 2014
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)
(hi__my__
name_is_
balh____
blah____
blah____
._nice__
to_meet_
you.) (_ equal space)
View 3 Replies
View Related