How to store data at a given location

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scoobydoo666
    New Member
    • Dec 2007
    • 10

    How to store data at a given location

    Hi
    How do I store data at a given memory location? For eg: I want to store 25 at location 1000. How do I do it? I tried using placement new(). But it is giving segmentation fault during runtime.

    Thanks
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You can't store at locartion 1000 becuse that is outside your process address space.

    You can use placement new to store at a named address only if that address is in your process address space.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      Pending googling 'process address space' which appears to be ahead of RAM and finding out how your machine allocates it--- if it does? You could store your data in a vector and then get its first element address using &. Then instruct the program to store the data at that location which would be superfluous and of questionable ethics but it could be proved by a later address query that it is the appropriate address. I omit QED for reasons of modesty.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by whodgson
        You could store your data in a vector and then get its first element address using &. Then instruct the program to store the data at that location which would be superfluous and of questionable ethics but it could be proved by a later address query that it is the appropriate address.
        Questionable indeed. That's not how to implement a private heap.

        The OS provides the correct calls. For Windows, you start with CreateProcessHe ap().

        Comment

        • scoobydoo666
          New Member
          • Dec 2007
          • 10

          #5
          Hi
          My question is I want to store data at a given location (inside address space). Is it possible? Placement new store data at predefined location. Programmer cannot specify the location. Is there any other way?

          Thanks

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Of course the programmer can specify the location:
            [code=cpp]
            int main()
            {
            int buffer[1000]; //4000 bytes

            char* str = new (buffer)char[100];
            char* str1 = new (buffer+100)cha r[25];
            }
            [/code]

            Of course, it's your problem to manage the memory buffer.

            Comment

            • Sreeramu
              New Member
              • Apr 2007
              • 11

              #7
              It is not possible using c ...we can do this with embedded c ...

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Of course you can do this in C.

                Just don't use malloc(). It uses the CRT heap.

                You can write your own memory manager.

                Comment

                Working...