C program Quincy Compiler keeps Crashing after use of malloc

This is a discussion on C program Quincy Compiler keeps Crashing after use of malloc within the C Programming forums, part of the General Programming Boards category; Hello, my quincy compiler compiles the program correctly, but it crashes as soon as I put my data in. Why ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    C program Quincy Compiler keeps Crashing after use of malloc

    Hello, my quincy compiler compiles the program correctly, but it crashes as soon as I put my data in. Why might this be?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    #define min(a,b) ((a) > (b) ? (b) : (a))
    #define max(a,b) ((a) > (b) ? (a) : (b))
    
    int main (void)
    
    {
    
    
    	int i, size;
    	int *set;
    	int max=0;
    	int sum;
    	float average=0;
    	int min=max;
    
    printf ("please give me the size\n");
    scanf("%d", &size);
    
    set = malloc(size *sizeof(int));
    
    printf ("please give me the numbers\n");
    scanf("%d", &set[i]);
    
    
    free(set);
    set=NULL
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    i is not initialized.


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

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    It looks like you forgot about a loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You should be getting several warnings, make sure you compile with the warnings set to max. Additionally, the minimum program in C is:
    Code:
    int main(void){
    
         return (0);
    }
    Last edited by AndrewHunter; 08-16-2011 at 11:26 PM. Reason: ...
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    i is not initialized.
    Quzah.
    Or incremented, at any point. Even if by fluke i was in range it would always write to the same slot in the array.

    Code:
    i= 0;
    printf ("please give me the numbers\n");
    
    while (i++ < size)
       scanf("%d", &set[i]);
    Last edited by CommonTater; 08-17-2011 at 03:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashing b/c of malloc, can someone please help?
    By matthayzon89 in forum C Programming
    Replies: 2
    Last Post: 11-14-2010, 02:53 PM
  2. Program Crashing - please help!
    By Surfin_Bird in forum C Programming
    Replies: 6
    Last Post: 03-23-2005, 11:34 AM
  3. Compiler Crashing... WHY?
    By kippwinger in forum C++ Programming
    Replies: 6
    Last Post: 07-03-2003, 01:10 AM
  4. malloc crashing program
    By VanJay011379 in forum C Programming
    Replies: 2
    Last Post: 02-08-2003, 02:52 PM
  5. help plz crashing program
    By mill1000 in forum C++ Programming
    Replies: 3
    Last Post: 08-23-2002, 10:15 AM

Tags for this Thread


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