Help: malloc and const variables??

This is a discussion on Help: malloc and const variables?? within the C Programming forums, part of the General Programming Boards category; Hi, what happens if we allocate more than MAX_SIZE in below situation. Is the result unpredictable?? How is this related ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Help: malloc and const variables??

    Hi,
    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???


    Thanks,
    Tharakan.

    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
    Posts
    131
    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
    Posts
    14,826
    Just like any array, if you go out of bounds, you risk killing your program and other BadThingsTM.

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

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    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
    Location
    Groningen (NL)
    Posts
    2,386
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 03:14 PM
  3. Difference between const char * and char const *
    By explorecpp in forum C Programming
    Replies: 4
    Last Post: 08-09-2008, 05:48 AM
  4. C program, catching special keypresses.
    By naeo in forum Windows Programming
    Replies: 1
    Last Post: 07-23-2007, 02:08 AM
  5. Replies: 7
    Last Post: 06-16-2006, 10:23 PM

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