Like Tree4Likes
  • 2 Post By whiteflags
  • 2 Post By hex_dump

crash problem with dynamic vectors

This is a discussion on crash problem with dynamic vectors within the C Programming forums, part of the General Programming Boards category; i don't understand why this program crash if i'll insert a number more of 5 times... i want to allocate ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    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
    zmone and Salem like this.

  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.
    zmone and Salem like this.

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-25-2006, 11:01 PM
  2. Why this (dynamic array) doesn't crash ?
    By soothsayer in forum C Programming
    Replies: 13
    Last Post: 01-26-2006, 09:01 PM
  3. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 04:10 PM
  4. Dynamic array sizing causes crash
    By Mithoric in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2003, 06:46 AM
  5. dynamic mem allocation prgm crash
    By rip1968 in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2002, 05:09 PM

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