code breaks.

I think the following test code produces an error on the execution of this code line: printf( "%d, ", p[i] ); (there are two printf of this kind, i mean the second one after the realloc, near the end of the source code)

I would like somebody to enlighten me if there is a mistake that i do not see...

What i did with my test code: allocate some memory(malloc) save some int numbers, then increase the size of the allocated memory(realloc) to save some additional int numbers. The first time(malloc) i choose( with scanf) the size of the allocated memory to be 5, then with a new scanf i resize it to 10.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <stdlib.h>
#include <cstdio>
 
using namespace std;

int main ()
{

    int *p, sz, i = 0;
    printf( "Select the total number of input elements: " );
    scanf( " %d", &sz );
    p = (int * ) calloc( sz, sizeof(int) );
    if ( p != NULL )
    {
        for ( i = 0; i < sz; i++ )
        {
            printf( "enter array value:\n" );
            scanf( " %d",(p+i) );
        }
        printf( "results:\n" );
        for ( i = 0; i < sz; i++ )
            printf( "%d, ", p[i] );
    }
    

        printf( "\nselect new size:" );
        scanf( " %d", &sz );
        p = ( int * )realloc( p, sz );
        if( p != NULL )
        {
            for ( i = 5; i < sz; i++ )
                p[i] = 6 + rand() % 12;
            for ( i = 0; i < sz; i++ )
                printf( "%d, ", p[i] );
        }


    free(p);
    return 0;
}
when i run the code it goes like this:

Select the total number of input elements: 5
enter array value:
1
enter array value:
2
enter array value:
3
enter array value:
4
enter array value:
0
results:
1,2,3,4,0,
select new size:10
1,2,3,4,0,11,17,14,15,22, <---- the momment i see this line the code breaks and i get a pop up window which says:
mytestprog.exe. has stopped working

windows can check online for a solution to the problem,
->Check online for a solution and close the program
-> close the program
Last edited on
The reason is on line 29, change it to

p = ( int * )realloc( p, sz * sizeof(int) );
The correct way of reallocating is like this -

p = (int *)realloc(p, sz * sizeof(int));


Look at the code posted below me.

Also, learn how to debug a program. It is super useful. Look it up on youtube or read about it whichever you prefer.
Last edited on
@TarikNeaj
What the OP is trying to achieve is this:
1
2
3
4
5
6
7
8
9
10
11
12
int old_sz = sz; // You need to save the current sz

        printf( "\nselect new size:" );
        scanf( " %d", &sz );
        p = ( int * )realloc( p, sz  * sizeof(int) );
        if( p != NULL )
        {
            for ( i = old_sz; i < sz; i++ ) // Note: old_sz
                p[i] = 6 + rand() % 12;
            for ( i = 0; i < sz; i++ )
                printf( "%d, ", p[i] );
        }
@Coder777

Yes sorry. I realized that and was about to edit my post to write something similar to yours :)
Topic archived. No new replies allowed.