Thread: crash problem with dynamic vectors

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    4

    crash problem with dynamic vectors

    i don't understand why this program crash if i'll insert a number more of 5 times... i want to allocate dynamically a vector if the input of the user exceeds 5 times... this is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    int * vettore, n;
    unsigned int len=1;
    
    
    vettore = (int *) malloc (5 * sizeof(int));
    
    
    printf("Gimme a positive integer (0 to terminate): ");
    scanf("%d", &n);
    
    
    for( len=1; n != 0; len++, vettore++ ){
        if (len > 5){
            vettore = realloc (vettore, len * sizeof(int));
            *vettore = n;
            printf("\nGimme a positive integer (0 to terminate): ");
            scanf("%d", &n);
        }
        else{
            *vettore = n;
            printf("\nGimme a positive integer (0 to terminate): ");
            scanf("%d", &n);
    
    
        }
    }
    
    
    vettore--;
    
    
    while(len>1){
        printf("%d ", *vettore--);
        len--;
    }
    
    
    printf("\n\n");
    system("PAUSE");
    return 0;
    }
    p.s.= if the user's input is less than 5 times no problem... so i think there is a problem that i can't see with the realloc

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's not an issue with realloc(). It has to do with your coding style. Your for loop is designed to move the vettore pointer. This is problematic since you are also using memory management functions. realloc(), along with free(), expects the pointer to the start of the memory block.

    If ptr does not match a pointer returned earlier by calloc(), malloc(), or realloc() or if the space has previously been deallocated by a call to free() or realloc(), the behavior is undefined.
    This whole description of realloc() is informative: realloc

  3. #3
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    You also don't need to cast malloc In C, this isn't C++.

    As well I would also check the return value of scanf() and make sure it is a positive integer. As it stands any non-zero value will be accepted as input.

    lastly, you have 3 calls to printf() prompting the user for input. Repetition like that usually means you can put it in a function...or maybe you can design it another way to cut down on those calls.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array of vectors
    By axr0284 in forum C++ Programming
    Replies: 8
    Last Post: 02-26-2006, 12:01 AM
  2. Why this (dynamic array) doesn't crash ?
    By soothsayer in forum C Programming
    Replies: 13
    Last Post: 01-26-2006, 10:01 PM
  3. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 05:10 PM
  4. Dynamic array sizing causes crash
    By Mithoric in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2003, 07:46 AM
  5. dynamic mem allocation prgm crash
    By rip1968 in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2002, 05:09 PM