简体   繁体   中英

Return value 3221225725, possible buffer overflow?

The aim of the exercise is to allocate "n" arrays whose dimensions goes from 2 to n+1 and give them random values to each element of each array So my idea was to use double pointers to pointers (that can be considered arrays in some way (?) ) Sorry for the italian language but it's an exercise for my school Anyhow, the code seems teorically correct but it can't do more than 2 cycles, for example it works for n=1,2 but from 3 and on it quits from the cycle and terminates the program, any suggestions to avoid it or to ?

Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100

/*The aim of the exercise is to allocate "n" arrays whose dimensions goes from 2 to n+1 and give them random values to each element of each array
So my idea was to use double pointers to pointers (that can be considered arrays in some way (?) ) Sorry for the italian language but it's an exercise for my school
Anyhow, the code seems teorically correct but it can't do more than 2 cycles, for example it works for n=1,2 but from 3 and on it quits from the cycle and terminates the program, any suggestions?*/

int main() {
        int n, i, j = 0, array[MAX];
        int *p1 = NULL, **p2 = NULL;
        srand(time(NULL));
        printf("Inserisci un numero naturale n per allocare un numero n di vettori\ndi lunghezza da 2 a n+1 :\n"); //Write a natural number n to allocate n arrays which contains from 2 to n+1 elements
        scanf("%d", &n);
        printf("\n\n");
        p2 = (int**)malloc(n*sizeof(int*)); //allocating the space for a double pointer
        for (i = 2; i <= n+1; i++) {
                *p2 = (int*)malloc(i*sizeof(int)); //allocating the space for the pointers
                printf("\nVettore #%d = ", i-1);
                for (j = 0 ;j < i ;j++) {
                        p2[i-2][j] = rand() % 10;
                        printf("%d ", p2[i-2][j]);

                }
                free(*p2);
        }



return 0;


}

The problem is here.

*p2 = (int*)malloc(i*sizeof(int)); //allocating the space for the pointers

You want to make an "array of arrays," which is why you are using a double pointer. int** p2 is meant to be an array of pointers to int* , pointers of integers.

So this looks good:

p2 = (int**)malloc(n*sizeof(int*)); //allocating space for an array of pointers

It is an array of n pointers to int* . Now we want each of these pointers to point to array as well. In the for loop, use

p2[i-2] = (int*)malloc(i*sizeof(int)); //allocating space for one array

This should work as you expect. Personally, I would use i and i+2 in my loops instead, but your choice is fine.

Note that you don't need to free the allocated memory. It goes away when your program ends. If your assignment requires free ing the space, then you need to write another loop to free the memory in each of the p2[i] arrays, and then free p2 .

Also, it is considered a bad idea to cast the return value from malloc() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM