-2

I want to use malloc()/new to allocate 256KB memory to variable m. Then, use m to store data such as strings and numbers. My problem is how to save data to m and retrive them.

For example, how to store int 123456 in offsets 0 to 3 and read it to variable x? Or store "David" string from offset 4 to 8(or 9 with \0) and then, retrive it to variable s?

millimoose
  • 39,073
  • 9
  • 82
  • 134
  • 1
    But are you professional enough to check out pointer arithmetic before asking questions like these? What have you tried? – Maarten Bodewes Mar 01 '12 at 21:58
  • 2
    You're post is tagged C++, but you're asking about C. EDIT: also, this doesn't deal directly with fragmentation, though I suspect this is a sub-problem of reimplementing malloc (an interesting exercise, I'll admit). Still, that tag is not relevant to the question you asked. – jpm Mar 01 '12 at 21:59
  • I would highly recommend reading "The C Programming Language" if you wish to learn C or "Accelerated C++" if you wish to learn C++, this is quite a vague question and come key concepts need to be learnt first. – 111111 Mar 01 '12 at 22:00
  • I've never heard "pointer arithmetic" before. Note that PHP is different from C in most cases, like working with memory & pointers. I'll read about pointer arithmetic & answer ASAP. – user1243815 Mar 01 '12 at 22:00
  • 3
    I'm not sure what answer to give you other than "learn to program in C or C++ or whatever language you want to do this in". – David Schwartz Mar 01 '12 at 22:01
  • The reason (if you have looked) that you cannot find how to do this is because it's a **bad** idea. Everyone else is happy using objects and collections without worrying about memory fragmentation, operating systems have come a long way in case you haven't noticed. – Tony Mar 01 '12 at 22:22

2 Answers2

1

You can store an integer by casting pointers.

unsigned char *p = new unsigned char[256 * 1000];
*(int *) p = 123456;
int x = *(int *) p;

This is a terrible idea. Don't worked with untyped memory, and don't try to play fast and loose like you do in PHP because C++ is less tolerant of sloppy programming.

I suggest reading an introductory C++ textbook, which will explain things like types and classes which you can use to avoid dealing with untyped memory.

Edit: From the comments above, it looks like you want to learn about pointer arithmetic.

Don't use pointer arithmetic*.

* unless you promise that you know what you are doing.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • This piece of code could crash on architectures where unaligned reads are not allowed. Please, please, please, don't do this. – Alexandre C. Mar 01 '12 at 22:06
  • @AlexandreC.: No, there is no architecture where this could crash. And if you had continued reading, you would notice that I say **not to do this**. (If it were C with `malloc`, I could even point to the parts of the standard which guarantee that it won't crash, but this is C++ so I'm not sure it's spelled out.) – Dietrich Epp Mar 01 '12 at 22:07
  • "don't do this" was not addressed to you :) Are you sure about the fact that the memory is guaranteed to be correctly aligned for storing integers ? I will check in the C++ standard tomorrow (i don't have it here), and I don't have a clue about C here (but I guess this is not very different). – Alexandre C. Mar 01 '12 at 22:15
  • After some quick googling, indeed `malloc` is guaranteed to align the memory somehow (eg, 16 bytes on MSVC, don't know about how much is required though). I'm pretty sure you don't have such guarantees with `new`. – Alexandre C. Mar 01 '12 at 22:17
  • I'm only sure about the C standard: it guarantees that `malloc` returns a pointer suitably aligned for any type; of course it has to since `malloc` has no way of knowing what type the pointer will be cast to. I have no idea about the C++ standard, but my claim is that there are no C++ *implementations* in the wild which will yield unaligned pointers from `new`. – Dietrich Epp Mar 01 '12 at 22:17
  • cf. http://stackoverflow.com/questions/506518/is-there-any-guarantee-of-alignment-of-address-return-by-cs-new-operation . Indeed, most implementations will likely align the memory, but it seems that the C++ standard does not mandate anything, so technically it could crash. – Alexandre C. Mar 01 '12 at 22:18
  • Thank you Dietrich Epp. this is half of what i needed. it will store integer to first 4 bytes. Now, how to store an string in next bytes? – user1243815 Mar 01 '12 at 22:21
  • Hm, the answer you linked to says otherwise "The pointer returned shall be suitably aligned so that it can be converted to a pointer of any complete object type and then used to access the object or array in the storage allocated" -- ergo, no crash, never. – Dietrich Epp Mar 01 '12 at 22:22
  • @user1243815: Think about it this way, I just showed you how to weld your car's differential for better performance, and now you are asking how weld the pistons in place so the car runs smoother? In the wrong hands, C++ is a tool for crashing programs, and you should **NOT** use untyped memory like you ask. – Dietrich Epp Mar 01 '12 at 22:25
  • @DietrichEpp: What I want to do is using new to allocate memory to a pointer. Then use some other arrays to store types, offsets and lengths to handle data. Is this way not secure? If not, is there any way to do that in better way? I want to implement something like arrays in PHP: ` $m[0] = 123456; // integer $m[1] = "John"; // string with 4 bytes length. $m[2] = "John Smith";// string with 10 Bytes length $m[3] = "funcName";// like function pointer in C/C++ $m[4](); // call function by pointer ` – user1243815 Mar 01 '12 at 22:55
  • 1
    @user1243815: Why do you think you need to do this? Have you found a problem with C/C++ memory management that leads you to this "solution"? – Blastfurnace Mar 01 '12 at 22:58
  • @Blastfurnace: both, yes and no! I want to struggle with C++ limitations & implement better features for learning, and maybe future usage. I think arrays are not much powerful in C++ (in comparison with PHP). I know this will make code slow to execute, but it is not necessary in my case. so, Please tell me how to solve the problem, if you can. thanks. – user1243815 Mar 01 '12 at 23:15
  • 1
    @user1243815: My best suggestion is to get a good C++ book and learn the language better, I'm not sure you understand what's available in the language and library. I also gently suggest you forget any idea about trying to program C++ like PHP. – Blastfurnace Mar 01 '12 at 23:39
  • I dont want to "program C++ like PHP" I just want to learn C++ in my way, as I learned PHP. I have about 10 programming books for 6-7 languages, but the only language that i am professional in, is PHP, which I have learned in my way. Please just tell me how to solve my problem, not about how to walk like idots... – user1243815 Mar 01 '12 at 23:59
0

Please read my comment, I think you need to know more about C and low level native programmng.

Is there a specific application for that format?

to assign a structure to memory you can do somethinglike

struct my_format{
    int first;
    char second[5];
};

int main()
{
     struct my_format *mfp=
          malloc(sizeof(struct my_format));
     mfp->first=123456;
     free(mfp);
}

or whatever this doesn't deal with memory specifics (IE exact positions of vars) vur doing so is just plain bad in almost all ways.

111111
  • 15,686
  • 6
  • 47
  • 62