Thread: Copy pointer address to buffer and convert it back

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Copy pointer address to buffer and convert it back

    Hi,

    I,m quite new to C and I was wondering the following:

    I have this large buffer in which I wish to store some data. One of the things I would like to store in this buffer is the address of a pointer instead of the value, like following example:

    Code:
    char size[7] = "000015";
    char* buffer = (char*) malloc(15 * sizeof(char));
    sprintf(buffer, "Lots of text...");
    
    int buffer_size = 6 + sizeof(char*);
    char* block_buffer = (char*) malloc(buffer_size * sizeof(char));
    Memory addresses are 4 bytes on my system, so I would like my block_buffer to contain "000015XXXX" where XXXX is the memory address of buffer.

    Any idea how i do this?

    And afterwards, when I split the block_buffer back up into size and address, how can i convert that address back to a char* containing the original "Lots of text..." data?

    If someone could explain to my if this is possible or not, or what would be the better approach, that would be a great help.

    Thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not really sure why you'd want to, and you'll potentially ruin what you are trying to make be a string, but what the heck. There are many ways to do this. Here's a fun one:
    Code:
    union u
    {
        unsignedfourbytedatatypeofyourchoice p;
        char *c;
    } x;
    x.c = block_buffer;
    x.p = &block_buffer;

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    >>I,m quite new to C and I was wondering the following:
    Perhaps you could tell us why you need to do this. There probably would be better way.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2
    Well, there is this cobol program which will call my C-routine, this program will supply 1 block of data as a parameter by reference. For the sake of the example this block will contain a size and a memory address (+ some other 'fields') "000015XXXX.....". This memory address points to a giant block of text which the user will be able to edit in my C-program, so lots of reallocs could occur. Afterwards i will need to update the referenced parameter with the new size and new memory address, so the Cobol will be able to read the new text block from memory.

    So the flow would be:

    1) Cobol allocates memory for some text with length 500 at address 0001.
    2) Cobol calls my C-routine with parameter "0005000001" by reference.
    3) My C-routine will need to retrieve the text from the "0001" address.
    4) User changes text, and fe adds 100 chars, so realloc to length 600 at address 0002.
    5) At the end of my routine I need to convert the text pointer back to a char representation of the memory address and update the parameter to "0006000002".
    6) Cobol continues with those values.

    At the moment I want a test C-routine which replaces the cobol and calls my other C-routine with a valid parameter. This is why I want to know how to do steps 3 and 5.

    I hope this is a bit clear, since it is quite a complicated problem, and I'm not sure if this is the right approach...

    Even though, thx for your input.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quite dont really understand the need for it. You could also use bitmask to do this too

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    If I understand the idea, you're passing a string back and forth. That string has two values - a length and address.

    If so, sprintf/sscanf is the way to go here. Assuming you control both programs the format is up to you so make it easy to parse.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Now why would you want to go and dig up Cobol's grave? Can't you let that language die in peace?
    A better approach would be to rewrite it in C.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proc-address in the buffer?
    By audinue in forum C Programming
    Replies: 11
    Last Post: 07-22-2008, 10:01 PM
  2. DrawDibDraw - Draw into back buffer instead of on screen
    By ulillillia in forum Windows Programming
    Replies: 6
    Last Post: 04-15-2007, 12:49 AM
  3. WM_PAINT + back buffer [GDI]
    By Kurisu33 in forum Windows Programming
    Replies: 10
    Last Post: 08-22-2006, 11:56 PM
  4. I'm back! need help with a simple copy program
    By Rune Hunter in forum C++ Programming
    Replies: 11
    Last Post: 10-16-2004, 07:52 PM
  5. copy int to buffer??
    By Gugge in forum C Programming
    Replies: 4
    Last Post: 03-19-2002, 12:17 PM