Thread: Help: malloc and const variables??

  1. #1
    Registered User
    Join Date
    Apr 2004

    Help: malloc and const variables??

    what happens if we allocate more than MAX_SIZE in below situation. Is the result unpredictable??

    How is this related to corrupting "const" declarations??

    Are they(malloc and const) both allocated from heap???


    const int p=100; <-- can a malloc like below corrupt a const declaration ???

    int MAX_SIZE=10;
    int *inptr[MAX_SIZE];

    intptr[0] = alloc_and_set();
    intptr[1] = alloc_and_set();
    inptr[9] = alloc_and_set(); <-- reaches MAX_SIZE???
    inptr[10] = alloc_and_set(); <-- Is this valid???
    inptr[11] = alloc_and_set(); <-- Is this valid???
    inptr[12] = alloc_and_set(); <-- Is this valid???
    inptr[13] = alloc_and_set(); <-- Is this valid???
    inptr[14] = alloc_and_set(); <-- Is this valid???


    int * alloc_and_set(){
    int *t;
    t= (int*) malloc(sizeof(int));
    *t = 10;
    return t;


  2. #2
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    You would probably get a segmentation fault for trying to access an array outside its bounds. So I would suggest not to do this.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Just like any array, if you go out of bounds, you risk killing your program and other BadThingsTM.

    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2004

    what does it has to do with const??

    Why is it corrupting the const declaration??? Isn't supposed to be protected?? Is the initialised variables also allocated in the Heap???

  5. #5
    Join Date
    Aug 2001
    Groningen (NL)
    The protection means that code cannot change the value of variable p directly by assigning a value to it. However, if you write data beyond the bound of an array or at some other place which you haven't allocated, then it is possible that you write data at the place ware variable p is stored.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > Why is it corrupting the const declaration?
    Once you have ANY undefined behaviour (like stepping off the end of the array), then ALL bets are off - your program is broken.
    Attempting to rationalise things with 'but its const' is not going to help you.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text (const char*)
    By Drogin in forum C Programming
    Replies: 14
    Last Post: 12-23-2008, 01:16 PM
  2. Wierd Malloc Problem
    By mohankarthik in forum C Programming
    Replies: 11
    Last Post: 09-17-2008, 02:14 PM
  3. Difference between const char * and char const *
    By explorecpp in forum C Programming
    Replies: 4
    Last Post: 08-09-2008, 04:48 AM
  4. C program, catching special keypresses.
    By naeo in forum Windows Programming
    Replies: 1
    Last Post: 07-23-2007, 01:08 AM
  5. Replies: 7
    Last Post: 06-16-2006, 09:23 PM