realloc a Pointer,在哪里说' it; sa指针?
我使用Realloc有一个问题。我无法实现一系列int。
现在,Realloc已固定,我唯一的问题是我无法触发它。这是目前的:
int insereAIndice(int* Tab, int index, int insertion, int taille) {
Tab = realloc(Tab, taille * sizeof (*Tab));
int i;
for (i = taille-1; i <= index+1 ; i--) {
printf("%d", Tab[i]);
Tab[i] = Tab[i-1];
}
return *Tab;
}
这是“重要”行:
*Tab = realloc(*Tab, taille * sizeof (*Tab));
这是代码的其余部分:
main.c
#include "functions.h"
#include "functions.c"
/* TP 3 - ESIEE-IT Rémy JARDIN */
int main() {
int saisie, index, insertion, taille;
// Création du tableau avec des valeurs aléatoires.
printf("Creation du Tableau. \nNombre de caractere du tableau : ");
scanf("%d", &saisie);
int *Tab = ArrayCreate(saisie);
printf("Voici le tableau nouvellement creer : \n");
Affichage(Tab, saisie);
// Insertion d'une valeur 'insertion' à l'index 'index'. Avec décallage.
taille = saisie;
printf("\n ! Mode Insertion ! \nNouvel Endroit souhaiter a partir de 0 : ");
scanf("%d", &index);
printf("Nouvelle valeur souhaiter : ");
scanf("%d", &insertion);
insereAIndice(Tab, index, insertion, taille);
Affichage(Tab, saisie);
free(Tab);
return 0;
}
functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <stdio.h>
#include <stdlib.h>
int *ArrayCreate(int saisie);
int randomizer(void);
int insereAIndice(int* Tab, int index, int insertion, int taille);
int Affichage(int* Tab, int max);
#endif
functions.c
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int *ArrayCreate(int saisie) {
int i;
int *Tab = malloc( saisie * sizeof (*Tab));
if (Tab==NULL) {
printf("Not enough Memory");
exit (1);
}
for (i = 0; i < saisie; i++) {
Tab[i] = randomizer();
}
return Tab;
}
int randomizer() {
return 1 + rand() % 100;
}
int insereAIndice(int* Tab, int index, int insertion, int taille) {
*Tab = realloc(*Tab, taille * sizeof (*Tab));
int i;
for (i = taille; i < index ; i--) {
printf("\n - %d", Tab[i]);
}
return *Tab;
}
int Affichage(int* Tab, int max) {
// Affichage
int i;
printf("\n Resultats : ");
for (i = 0; i < max; i++) {
printf("%d - ", Tab[i]);
}
return *Tab;
}
,这就是GCC给我的:
functions.c: In function 'insereAIndice':
functions.c:26:20: warning: passing argument 1 of 'realloc' makes pointer from integer without a cast [-Wint-conversion]
*Tab = realloc(*Tab, taille * sizeof (*Tab));
^
In file included from functions.h:5:0,
from main.c:1:
f:\logiciel\mingw\include\stdlib.h:486:40: note: expected 'void *' but argument is of type 'int'
_CRTIMP __cdecl __MINGW_NOTHROW void *realloc (void *, size_t);
^~~~~~~
In file included from main.c:2:0:
functions.c:26:10: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
*Tab = realloc(*Tab, taille * sizeof (*Tab));
^
谢谢!
I have one problem using realloc. I am unable to realloc an array of int.
Now that the realloc is fixed, my only problem is that I am not able to trigger the for. Here is it at the moment:
int insereAIndice(int* Tab, int index, int insertion, int taille) {
Tab = realloc(Tab, taille * sizeof (*Tab));
int i;
for (i = taille-1; i <= index+1 ; i--) {
printf("%d", Tab[i]);
Tab[i] = Tab[i-1];
}
return *Tab;
}
Here is the "important" line:
*Tab = realloc(*Tab, taille * sizeof (*Tab));
And here is the rest of the code:
Main.c
#include "functions.h"
#include "functions.c"
/* TP 3 - ESIEE-IT Rémy JARDIN */
int main() {
int saisie, index, insertion, taille;
// Création du tableau avec des valeurs aléatoires.
printf("Creation du Tableau. \nNombre de caractere du tableau : ");
scanf("%d", &saisie);
int *Tab = ArrayCreate(saisie);
printf("Voici le tableau nouvellement creer : \n");
Affichage(Tab, saisie);
// Insertion d'une valeur 'insertion' à l'index 'index'. Avec décallage.
taille = saisie;
printf("\n ! Mode Insertion ! \nNouvel Endroit souhaiter a partir de 0 : ");
scanf("%d", &index);
printf("Nouvelle valeur souhaiter : ");
scanf("%d", &insertion);
insereAIndice(Tab, index, insertion, taille);
Affichage(Tab, saisie);
free(Tab);
return 0;
}
functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <stdio.h>
#include <stdlib.h>
int *ArrayCreate(int saisie);
int randomizer(void);
int insereAIndice(int* Tab, int index, int insertion, int taille);
int Affichage(int* Tab, int max);
#endif
functions.c
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int *ArrayCreate(int saisie) {
int i;
int *Tab = malloc( saisie * sizeof (*Tab));
if (Tab==NULL) {
printf("Not enough Memory");
exit (1);
}
for (i = 0; i < saisie; i++) {
Tab[i] = randomizer();
}
return Tab;
}
int randomizer() {
return 1 + rand() % 100;
}
int insereAIndice(int* Tab, int index, int insertion, int taille) {
*Tab = realloc(*Tab, taille * sizeof (*Tab));
int i;
for (i = taille; i < index ; i--) {
printf("\n - %d", Tab[i]);
}
return *Tab;
}
int Affichage(int* Tab, int max) {
// Affichage
int i;
printf("\n Resultats : ");
for (i = 0; i < max; i++) {
printf("%d - ", Tab[i]);
}
return *Tab;
}
And this is what GCC gives me:
functions.c: In function 'insereAIndice':
functions.c:26:20: warning: passing argument 1 of 'realloc' makes pointer from integer without a cast [-Wint-conversion]
*Tab = realloc(*Tab, taille * sizeof (*Tab));
^
In file included from functions.h:5:0,
from main.c:1:
f:\logiciel\mingw\include\stdlib.h:486:40: note: expected 'void *' but argument is of type 'int'
_CRTIMP __cdecl __MINGW_NOTHROW void *realloc (void *, size_t);
^~~~~~~
In file included from main.c:2:0:
functions.c:26:10: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
*Tab = realloc(*Tab, taille * sizeof (*Tab));
^
Thank you !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
realloc
的第一个参数是指针。选项卡
被声明为int *
,因此意味着*TAB
是int
,这就是为什么它不使用' t匹配参数的类型。这也意味着返回类型与您分配的内容不匹配。因此,通过指针而不是指向的指针。
但是在以下几行上,另一个问题变得很明显:
该数组已调整为具有
taille
元素,因此该循环读取了分配内存的末尾。您大概是为了分配而想要的:因此,您将不再阅读过去分配的内存(假设数组的大小更大),但是现在您正在阅读非直接化的
值。
The first argument to
realloc
is a pointer.Tab
is declared as anint *
, so that means*Tab
is anint
which is why it doesn't match the type of the parameter. This also means the return type doesn't match what you're assigning it to.So pass the pointer instead of what it points to.
But on the following lines another problem becomes apparent:
The array has been resized to have
taille
elements, so this loop reads past the end of allocated memory. You presumably want this instead for the allocation:So then you're not longer reading past allocated memory (assuming the array was resized to be larger), but now you're reading uninitialized
values.