Thread: help! (int*)+offset

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    67

    help! (int*)+offset

    To start off, here's my code:

    Code:
    // setting stuff up
    std::cout.setf(std::ios::hex);
    std::cout.unsetf(std::ios::dec);
    int int_size = sizeof(int);
    
    int commands = 5;
    int max_bytes = 3;
    
    int byte_array = (int)malloc(int_size*commands*max_bytes);
    int* pbyte_array = &byte_array;
    
    cout << (0x0012FF3C+(int_size*1)) << "\n\n";
    	
    for(int i=0x0;i<commands*max_bytes;i+=0x1)
    {
    	cout << pbyte_array << "\t+\t";
    	cout << i*int_size << "\t=\t";
    	cout << pbyte_array+(i*int_size) << "\n";
    }
    Okay so it compiles fine, but running gives me
    Code:
    12ff40
    
    0012FF3C	+	0	=	0012FF3C
    0012FF3C	+	4	=	0012FF4C
    0012FF3C	+	8	=	0012FF5C
    0012FF3C	+	c	=	0012FF6C
    .................
    0012FF3c	+	38	=	0013001C
    Basically it's adding 16 each time. If I change it to

    Code:
    cout << pbyte_array+1
    it adds 4 to the output (instead of 1 as it should). I notice if I change pbyte_array declaration to
    Code:
    int pbyte_array = (int)(&byte_array)
    then it works fine (although the hex output becomes lowercase o_O). I have checked the output of int_size and it's 4 as it should be. If I type the address directly into the loop it does the same thing as it does with the changed pbyte_array declaration.

    Basically I want a a pointer/address and the addition of an offset.
    I'm running Visual C++ 2008

    Thanks!

    Oh and also I couldn't get arrays to work properly oddly, which is why I created this code in the first place. It kept saying it couldn't convert (int*) to (int).
    Last edited by RobotGymnast; 01-06-2008 at 12:49 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    sizeof(int) = 4. Since you have your array declared as type int, adding one to the pointer will take you to the next int, four bytes away. Since you add (i*int_size), you're adding 4, which means that you go four ints down the line, sixteen bytes away.

    PS: int* and int aren't the same thing. What were you trying to do.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So use a type which has a size of 1 (say unsigned char), not 4 (as is the case with your ints).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    Thanks, I got it working.. just changed it to
    Code:
    cout << pbyte_array+i << "\n";

    I know they aren't the same.. which is why it confused me, I was just going

    Code:
    int testarray[];
    testarray = new int[5];
    my compiler errors are

    Code:
    'testarray': unknown size.
    '=': cannot convert from 'int *' to 'int []'
    changing it to

    Code:
    int[] testarray
    didn't help either.. more errors

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Obvious way to do that:
    Code:
    int testarray[5];
    Way to do that for people who insist on new:
    Code:
    int *testarray;
    testarray = new int[5];
    You can't declare an array without giving it a size.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    Okay thanks... I did get a warning with
    Code:
    int testarray[5];
    though. Thanks anyway though!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You obviously don't know what pointers are and what the heap is or how to allocate memory.
    When allocating on the heap, we keep track of the memory by the use of pointers.
    Anyway, I suggest you stay away from pointer + n, because it's confusing and is obviously confusing you too. Use pointer[n] instead.

    Quote Originally Posted by RobotGymnast View Post
    Okay thanks... I did get a warning with
    Code:
    int testarray[5];
    though. Thanks anyway though!
    What warning?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unsigned Long returning signed value
    By dinklebaga in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 06:07 AM
  2. My memory management solution
    By cboard_member in forum Game Programming
    Replies: 20
    Last Post: 08-23-2006, 09:07 AM
  3. offset
    By Rhidian in forum C Programming
    Replies: 6
    Last Post: 04-14-2005, 08:57 AM
  4. Relocation in .obj -files
    By willkoh in forum C++ Programming
    Replies: 6
    Last Post: 04-06-2005, 01:59 PM
  5. I still do not understand offset, please give me examples
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2002, 09:01 AM