Thread: help with pointers

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    help with pointers

    Hi,
    I need help with this program . I am doing something wrong with pointer addition.

    Code:
    
    /* program allocates 16 MB of memory on heap and writes to every 2MB interval*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    #define INTERVAL  (2UL * 1024 *1024) /* 2MB interval*/
    
    int main(int argc, char *argv[])
    {
    
    unsigned long p = malloc(atoi(argv[1]));
    assert(p);
    
    int i = 0;
    
    unsigned long interval  =  INTERVAL;
    
    int number_of_chunks  = (atoi(argv[1])/INTERVAL);
    
    for(i = 0; i < number_of_chunks;  i++){
    	p[i *INTERVAL] = (char) 1; /* write 1 at start location of each chunk */
    	}
    
    free(p);
    return 0;
    }
    Getting a seg fault after i = 3 and gdb says each iternation is incrementing the pointer 10MB instead of 2MB.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Why are you defining p as an unsigned long? It should be a char *. As an unsigned long the expression:
    p[i * INTERVAL]
    which is equivalent to
    *(p + i * INTERVAL)
    accesses the byte i * INTERVAL * sizeof(unsigned long), which is 4 to 8 times farther into the array then you mean to be!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    It shouldn't even compile since p is not a pointer, so it can't be dereferenced. If your compiler allows it anyway, get a better compiler, or at least turn on warnings.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Quote Originally Posted by christop View Post
    It shouldn't even compile since p is not a pointer, so it can't be dereferenced. If your compiler allows it anyway, get a better compiler, or at least turn on warnings.
    Yes u are right. I think it was an overseen error as a result of an unchecked keystroke in vim. But the actual code had a pointer declared.
    Thank you oogabooga. I think that will work. I thought all the memory addresses in RAM need to be unsigned long. But yeah unsigned char should work.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by livin View Post
    Yes u are right. I think it was an overseen error as a result of an unchecked keystroke in vim. But the actual code had a pointer declared.
    Thank you oogabooga. I think that will work. I thought all the memory addresses in RAM need to be unsigned long. But yeah unsigned char should work.
    A pointer is whatever size an address needs to be on the system, which might be smaller or larger than an unsigned long. A variable declared as a "char *" is a pointer (address) that refers to a char or to an array of char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM