Thread: Memory Allocation

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    Memory Allocation

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
        
        int i,*ary;
        ary=(int*)malloc(0*sizeof(int));  //assign memory
        
    
        for(i=0;i<5;i++){  //input 5numbers
            printf("\n Please input number %d : ",i);
            scanf("%d",ary+i); 
        }            
       
        for(i=0;i<5;i++){  //output the numbers
            printf("\n%d -> %d",i,ary[i]);
        }
     
        free(ary);
    }
    In the above program,I assign 0 memory by using ary=(int*)malloc(0*sizeof(int));
    Since the memory is zero, it should not be able to read any values and output it rite? this program somehow inputs and outputs all the numbers.
    Wheres' the error? any one knows?
    thanx.
    Last edited by rahulsk1947; 02-24-2006 at 09:25 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    PEBKAC. It's up to the programmer to know better.

    Undefined behavior sometimes annoyingly does what you think defined behavior ought.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I tried this out.

    Here's the result from different compilers:
    Borland - Crashes after trying to input the first number.
    GCC (MinGW) - Works fine.
    MSVC++ - Works fine up until the part when it tries to free the memory where it crashes.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    hmm,that' means my Dev c++ compiler sucks. the first time it gives me a problem.

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Nothing wrong with your compiler, in fact MinGW is probably one of the best. Besides, you shouldn't allow your program to do anything that causes it to perform in an undefined way. Make sure that the value passed to malloc() is never 0 with a simple if-statement.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
        
        int i,*ary;
        int size=3;
        ary=(int*)malloc(size*sizeof(int));
        
        for(i=0;i<5;i++){
            printf("\n Please input number %d : ",i);
            scanf("%d",ary+i); 
        }            
        
        
        for(i=0;i<5;i++){
            printf("\n%d -> %d",i,ary[i]);
        }
        
        free(ary);
    
        
    }
    the visual C++ free compiler from the MS website is funy.this programing works fine for n=3 and not for n=2.
    it shouldn't work for anything n < 5.

    Worry,that i may create big programs,and these compiler's would work fine when its supose to show an error. later would be hard to debugg.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by rahulsk1947
    Worry,that i may create big programs,and these compiler's would work fine when its supose to show an error. later would be hard to debugg.
    The compiler is not supposed to show an error, and yes it would be hard to debug.

    C is a sharp tool. It can do exactly what you want, or it can remove a limb. The employer of the tools is the one who makes the difference.

    (And don't use any particular implementation as a measuring stick.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rahulsk1947
    Worry,that i may create big programs,and these compiler's would work fine when its supose to show an error. later would be hard to debugg.
    The compiler is not supposed to show any error in response to code that exhibits undefined behaviour.

    The problem is that your code exhibits undefined behaviour. Undefined behaviour means that anything is allowed to happen. The scope of "anything" (by definition) is pretty wide, and can include;

    1) a program that appears to run correctly;
    2) a program that crashes as soon as you run it;
    3) a program that deletes all files on your hard drive.

    The reason it is undefined behaviour is that it is often technically very difficult (and in some cases, impossible) for a compiler (or it's supporting library) to detect that an error has occurred. If some occurrence can't even be detected, it is also rather difficult to define what happens in response to that occurrence.

    Yes, it is easy to see that your code is doing something wrong. But compilers have a lot more difficulty making such judgements than humans do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM