Thread: C++ to C -> malloc

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    11

    C++ to C -> malloc

    ive easily used new & delete in c++, but in c im getting confused

    Code:
    int size = 1;
    int *arr = (int*)malloc(size*sizeof(int));
    a dynamic int array that fits only 1 number its size = 0?
    0 * anything = zero so don't use this in malloc?

    i've seen code with simply
    Code:
    int *arr = (int*)malloc(size);
    i assume this is incorrect because it also needs the size of the struct/etc?

    here is a simple example I wrote:

    Code:
    int main(){
        int size = 10;
        int i=0;
        char *arr = (char*)malloc(size*sizeof(char));
    
        for(;i<size;i++)
            arr[i] = (rand() % ('Z' - 'A'))+'A';
    
        arr[size] = '\0';
        printf("%s",arr);
        free(arr);
    
        getchar();getchar();getchar();
        return 0;
    }
    why does this program crash when i add: free(arr); ?
    when i change arr[size] = '\0'; to arr[size-1] = '\0'; it works

    Code:
    int main(){
        int size = 1;
        int i = 0;
        int *arr;
        int *temp;
    
        arr = (int*)malloc(size*sizeof(int));
        for(;i<size;i++){
            arr[i] = rand() % 256;
            printf("%d. %d",i,arr[i]);
        }
        ++size;
        temp = (int*)malloc(size*sizeof(int));
        for(i=0;i<size-1;i++){
            temp[i] = arr[i];
        }
        temp[size-1] = rand() % 256;
    
        free(arr);
        for(i=0;i<size;i++){
            printf("\n%d. %d",i,temp[i]);
        }
        free(temp);
    
        getchar();
        return 0;
    }
    do all dynamic arrays (int,structs,etc) hold an empty cell at the end?
    Last edited by hit; 10-29-2012 at 08:32 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hit
    a dynamic int array that fits only 1 number its size = 0?
    0 * anything = zero so don't use this in malloc?
    Err... size == 1, so I am not sure what you are talking about. If you mean to ask what would happen if 0 was assigned to size instead of 1, then the answer is that you will either get a null pointer returned by malloc, or you get a non-null pointer that you cannot use. By the way, I suggest:
    Code:
    int *arr = malloc(size * sizeof(*arr));
    You don't need to cast since this is not C++, and using sizeof(*arr) means that the code remains correct even if the type of arr changes.

    Quote Originally Posted by hit
    i assume this is incorrect because it also needs the size of the struct/etc?
    Not necessarily, e.g., sizeof(char) == 1.

    Quote Originally Posted by hit
    why does this program crash when i add: free(arr); ?
    Undefined behaviour. You write beyond the bounds of the dynamic array on this line:
    Code:
    arr[size] = '\0';
    Quote Originally Posted by hit
    do all dynamic arrays (int,structs,etc) hold an empty cell at the end?
    All dynamic arrays for which you designate an "empty cell" at the end and correctly implement it hold an empty cell at the end. All other dynamic arrays do not, except by coincidence.

    EDIT:
    Also, note that new and new[] may throw std::bad_alloc, but malloc, realloc and calloc may return null pointers. So, just as you would catch std::bad_alloc in more complete code, you should check for null pointers here.
    Last edited by laserlight; 10-29-2012 at 08:36 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    hmm i think i understand the problem, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc vs new
    By thescratchy in forum C++ Programming
    Replies: 5
    Last Post: 11-21-2010, 06:01 PM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. Replies: 7
    Last Post: 10-01-2008, 07:45 PM
  4. Using malloc
    By ulillillia in forum C Programming
    Replies: 34
    Last Post: 02-20-2008, 06:41 PM
  5. When and when not to use malloc()?
    By John.H in forum C Programming
    Replies: 5
    Last Post: 03-24-2003, 06:00 PM