Microsoft Developer Network >
Forums Home
>
Visual Studio Express Editions Forums
>
Visual C++ Express Edition
>
String Arthimetic
String Arthimetic
- I have 1GB of memory.
After I declare a string x add a offset of 1GB and interact
with it the program error comes up.
Example:
char *x + offset;
memset(x, offset, 1GB-1);
I did a little digging, came across that I can use the malloc and it'll work.
My question is how do I get it to work without using the malloc?
What's is the basic concept which I need in order to build my own which can handle 1GB or more?
Answers
- Quote>how do I assign a string which can peek/poke without having violating
Quote>any memory access rights and can be done from the ground up?
What does "from the ground up" mean?
If you are trying to peek/poke 1GB of memory which hasn't been allocated to
your process, you're SOL. Windows has been designed intentionally to protect
other processes from such unwelcome intrusions. Your program (Ring 3 access)
will not have the required privileges (Ring 0 - kernel mode).
- Wayne- Marked As Answer byElit99 Tuesday, November 03, 2009 12:37 PM
- You can peek/poke within the memory allocated to your application by the OS, but you would have to know the specific layout for that version of VC++ and that specific OS. Peeking/poking outside memory allocated to your process can only be done with OS API and security access methods well beyond the scope of this forum. Memory is no longer an "open" resource, so you can't peek/poke anywhere you want anymore without serious low-level research. Even with that, you still need the proper access rights.
However, I still might not understand what you're trying to do. You have only described your attempted implementation of a task, but you have not yet described the actual task you are trying to perform. The last example you posted uses a pointer to a single allocated character, but you are trying to add an index to that pointer that points it well outside your allocated memory.
I hope that helps.
All Replies
- I'm not at all sure of what you're trying to do here. You need to provide a complete source sample and the actual errors printed in the output window.
A char * definition only allocates space for the pointer and not to what it points. You have to allocate that separately.
The function calloc() will both allocate the block (using malloc() ) and set each element to 0.
#include <iostream> using namespace std; void main() { char *x = (char *)calloc(1000000000, sizeof(char)); if (x == NULL) { cout << "Failure" << endl; return; } cout << "Success" << endl; }
You have the memset arguments wrong. They should be memset(void * pDest, int value, size_t count) . If you used malloc to allocate the preceding memory block, then you would set all the elements to '\0' with:
memset(x, 0, 1000000000);
If you are trying to peek/poke unallocated memory addresses, the OS may complain that you are violating memory access rights.
I hope that helps.
Yes it helped. It explains all the errors.
Please, allow me to make it simpler to understand.
Basically, how do I assign a string which can peek/poke without having violating any memory access rights and can be done from the ground up? What's the concept which is needed to do so?
#include "stdio.h"
#include "stdlib.h"
int main( void )
{
char x, *y = &x;
memset(y, 0, 1000000000-1);
return 0;
}- Quote>how do I assign a string which can peek/poke without having violating
Quote>any memory access rights and can be done from the ground up?
What does "from the ground up" mean?
If you are trying to peek/poke 1GB of memory which hasn't been allocated to
your process, you're SOL. Windows has been designed intentionally to protect
other processes from such unwelcome intrusions. Your program (Ring 3 access)
will not have the required privileges (Ring 0 - kernel mode).
- Wayne- Marked As Answer byElit99 Tuesday, November 03, 2009 12:37 PM
- You can peek/poke within the memory allocated to your application by the OS, but you would have to know the specific layout for that version of VC++ and that specific OS. Peeking/poking outside memory allocated to your process can only be done with OS API and security access methods well beyond the scope of this forum. Memory is no longer an "open" resource, so you can't peek/poke anywhere you want anymore without serious low-level research. Even with that, you still need the proper access rights.
However, I still might not understand what you're trying to do. You have only described your attempted implementation of a task, but you have not yet described the actual task you are trying to perform. The last example you posted uses a pointer to a single allocated character, but you are trying to add an index to that pointer that points it well outside your allocated memory.
I hope that helps.
Yes it helps. Thank for your assistance.- The phrase "from the ground up" means understanding something from the beginning.

