Like Tree2Likes
  • 1 Post By Bayint Naung
  • 1 Post By stahta01

Pointer points to different address after array initialisation

This is a discussion on Pointer points to different address after array initialisation within the C Programming forums, part of the General Programming Boards category; Hi all, I have a weird problem when dealing with pointers and arrays. In particular, I have the following code: ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    26

    Question Pointer points to different address after array initialisation

    Hi all,

    I have a weird problem when dealing with pointers and arrays. In particular, I have the following code:

    Code:
    #define OFFSET 0xC0000000
    #define VRT_ADD 0xC1628340
    #define PHYS_ADD (VRT_ADD - OFFSET)
    
    uint8_t *Pointer_A = (uint8_t*)PHYS_ADD;
    printk("Pointer_A is: 0x%X\n", Pointer_A);
    uint8_t i=0;
    uint8_t Array_A[15];
    
    for (i=0; i<=15; i++)
    {
    	Array_A[i]=0;
    }
    
    printk("Pointer_A is: 0x%X\n", Pointer_A);
    After the filling of Array_A with zeros, Pointer_A points to 0x1628300, i.e. to an address which locates 64 bytes lower in memory compared to the one originally pointing to. This was observed by looking at the printks before and after the array filling:

    Code:
    Pointer_A is: 0x1628340
    Pointer_A is: 0x1628300
    If that helps, gcc version is 4.4.1 (Ubuntu 4.4.1-4ubuntu9)

    Can anyone guess what is going wrong? Any help will be much appreciated!

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    for (i=0; i<=15; i++)
    Count carefully. how many times is it looped.
    and you're hacking kernel?
    Salem likes this.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,553
    Quote Originally Posted by limp View Post
    Hi all,
    Code:
    uint8_t Array_A[15];
    
    for (i=0; i<=15; i++)
    {
    	Array_A[i]=0;
    }
    I suggest NOT overwriting your own memory.
    As in Array_A[15] is NOT part of the array.

    Tim S.

    Use
    Code:
    for (i=0; i<15; i++)
    Salem likes this.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    26
    Quote Originally Posted by stahta01 View Post
    I suggest NOT overwriting your own memory.
    As in Array_A[15] is NOT part of the array.
    What a silly mistake! That (of course) solved the problem.

    Thanks for that mate!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking to which address pointer points
    By ThatDudeMan in forum C Programming
    Replies: 2
    Last Post: 11-28-2010, 03:45 AM
  2. pointer to pointer that points to a char array
    By steve1_rm in forum C Programming
    Replies: 2
    Last Post: 01-13-2009, 11:03 PM
  3. How to declare a pointer that points to another pointer?
    By yuzhangoscar in forum C Programming
    Replies: 1
    Last Post: 09-15-2008, 06:28 AM
  4. Pointer points to struct pointer
    By gogo in forum C Programming
    Replies: 4
    Last Post: 11-27-2001, 11:14 AM
  5. pointer initialisation
    By C_Coder in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21