重新分配一个数组(指针) [英] Reallocate an array (pointers)

查看:72
本文介绍了重新分配一个数组(指针)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我不知道为什么我的重新分配功能(myadd)会破坏我的

数组。我正在努力解决它,它花了我几个小时。也许

你可以查看我的代码并解释我的错误。 (我仍然有指针问题
)。

我保留了我的代码原样。

谢谢!


#include" stdafx.h" / *包含stdio.h * /

#include< stdlib.h>

#include< string.h>

void * alloca(int n);

char * readr(void);

void myadd(char ** A);

void myfree(void * A);


/ *主程序* /

int main(无效){

/ *动态分配的数组指针:* /

char **名称;

int i,n = 2;


names =(char ** )alloca(n);

for(i = 0; i< n; i ++){

names [i] = readr();

printf(" txt:%s \ n",names [i]);

}


myadd(姓名);

myfree(姓名);

返回0;

}

/ *函数* /

void myfree(void * AA){/ *释放所有分配的内存* /

int i;

void ** A =(void **)AA;

for(i = 0; A [i]; i ++)/ *当A [i] = NULL * /

free(A [i]);
免费(AA);

}


void * alloca(int n){

void ** A;

/ *避免n == 0:* /

if(n == 0)n = 1;

if ((A =(void **)calloc(n + 1,sizeof(* A)))== NULL)返回NULL;

A [n] = NULL;

返回A;

}


char * readr(无效){

char * buf;

int i;

char c,BB [256] = {''\ 0''};

fflush(stdin);

/ *最多读取255个字符。最后一个字符是''\ 0'':* /

for(i = 0;((c = getchar())!=''\ n'')&& i< ; 255; i ++)

BB [i] = c;

fflush(stdin);

/ * i + 1:启用myfree( )释放所有内存:* /

if((buf =(char *)calloc(i + 1,sizeof(char)))== NULL){

printf(哎呀,不能分配mem。!\ n);

}

else {

strcpy(buf) ,BB);

返回buf;

}

}

/ *应该重新分配名称并添加一个条目:* /

void myadd(char ** A){

int n;

/ *计数行数组:* /

for(n = 0; A [n]; n ++);

/ *它应该重新分配名字但似乎打破它* /

如果((A =(char **)realloc(A,(n + 2)* sizeof(* A)))== NULL)

printf(哎呀,不能重新分配mem。!\ n);

A [n] = readr();

A [n + 1] = NULL;

}

Hi,
I have no idea why my reallocation function (myadd) is breaking up my
array. I''m trying to solve it and it took me few hours already. Maybe
you can look at my code and explain me what is wrong. (I still have
problems with pointers).
I left my code as it is.
Thank you!

#include "stdafx.h" /* contains stdio.h */
#include <stdlib.h>
#include <string.h>
void *alloca(int n);
char *readr(void);
void myadd(char **A);
void myfree(void *A);

/* Main program */
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;

names=(char **)alloca(n);
for(i=0;i<n;i++){
names[i]=readr();
printf("txt: %s\n", names[i]);
}

myadd(names);
myfree(names);
return 0;
}
/* Functions */
void myfree(void *AA){ /* Free all allocated memory */
int i;
void **A=(void **)AA;
for(i=0; A[i]; i++) /* Finish freeing when A[i]=NULL */
free(A[i]);
free(AA);
}

void *alloca(int n){
void **A;
/* Avoid n==0:*/
if(n==0) n=1;
if((A=(void **)calloc(n+1, sizeof(*A)))==NULL) return NULL;
A[n]=NULL;
return A;
}

char *readr(void){
char *buf;
int i;
char c,BB[256]={''\0''};
fflush(stdin);
/* Read 255 characters max. Last char is ''\0'': */
for(i=0; ((c=getchar())!=''\n'')&&i<255; i++)
BB[i]=c;
fflush(stdin);
/* i+1: enable myfree() to free all memory: */
if((buf=(char *)calloc(i+1,sizeof(char)))==NULL){
printf("Oops, can''t allocate mem.!\n");
}
else{
strcpy(buf,BB);
return buf;
}
}
/* Should reallocate "names" and add one more entry to it :*/
void myadd(char **A){
int n;
/* Count rows of array :*/
for(n=0;A[n];n++);
/* It is supposed to reallocate "names" but it seems to break it */
if((A=(char **)realloc(A, (n+2)*sizeof(*A)))==NULL)
printf("Oops, can''t reallocate mem.!\n");
A[n]=readr();
A[n+1]=NULL;
}

推荐答案

Piotrek写道:
Piotrek wrote:

>

我不知道为什么我的重新分配功能(myadd)正在分解

我的数组。我正在努力解决它,它花了我几个小时。

也许你可以查看我的代码并解释我的错误。 (我

仍然有指针问题)。

我保留了我的代码原样。


#include" stdafx .H" / *包含stdio.h * /
>
I have no idea why my reallocation function (myadd) is breaking up
my array. I''m trying to solve it and it took me few hours already.
Maybe you can look at my code and explain me what is wrong. (I
still have problems with pointers).
I left my code as it is.

#include "stdafx.h" /* contains stdio.h */



然后使用stdio.h。你从未列出过stdafx.h。

Then use stdio.h. You never listed stdafx.h.


#include< stdlib.h>

#include< string.h>


void * alloca(int n);

char * readr(void);

void myadd(char ** A);

void myfree(void * A);


/ *主程序* /

int main(void){

/ *动态分配指针数组:* /

char **名称;

int i,n = 2;

names =(char **)alloca(n);
#include <stdlib.h>
#include <string.h>

void *alloca(int n);
char *readr(void);
void myadd(char **A);
void myfree(void *A);

/* Main program */
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;

names=(char **)alloca(n);



Alloca返回void *。你不需要(或想要)演员。

Alloca returns void*. You don''t need (or want) the cast.


for(i = 0; i< n; i ++){
for(i=0;i<n;i++){



尝试在项目之间添加一些空格。 for(i = 0; i< n; i ++)"

Try including a few spaces between items. "for (i = 0; i < n; i++)"


names [i] = readr();

printf(" txt:%s \ n",names [i]);

}


myadd(姓名);

myfree(姓名);

返回0;

}


/ *函数* /
names[i]=readr();
printf("txt: %s\n", names[i]);
}

myadd(names);
myfree(names);
return 0;
}

/* Functions */



最好在main之前加上这些,从而省去了

原型。这种技术减少了愚蠢的可能性。

错误。

Better to precede main with these, thus dispensing with the
prototypes. This technique reduces the possibility of silly
errors.


void myfree(void * AA){/ *释放所有分配的内存* /

int i;

void ** A =(void **)AA;
void myfree(void *AA){ /* Free all allocated memory */
int i;
void **A=(void **)AA;



再一次,没有演员。你正在用这些指针做一些有趣的事情(和

不必要)。

Once again, no cast. You are doing something funny (and
unnecessary) with these pointers.


for(i = 0; A [i]; i ++) / *当A [i] = NULL * /

免费(A [i]);

免费(AA);

时完成释放}


void * alloca(int n){

void ** A;

/ *避免n == 0: * /

if(n == 0)n = 1;

if((A =(void **)calloc(n + 1,sizeof(* A) ))== NULL)返回NULL;
for(i=0; A[i]; i++) /* Finish freeing when A[i]=NULL */
free(A[i]);
free(AA);
}

void *alloca(int n){
void **A;
/* Avoid n==0:*/
if(n==0) n=1;
if((A=(void **)calloc(n+1, sizeof(*A)))==NULL) return NULL;



无演员。

No cast.


A [n] = NULL;

返回A;

}
A[n]=NULL;
return A;
}



我现在厌倦了阅读。


-

< http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

< http://www.securityfocus .com /专栏作家/ 423>

< http://www.aaxnet.com/editor/edit043.html>

< http://kadaitcha.cx /vista/dogsbreakfast/index.html>

cbfalconer at maineline dot net


-

通过免费的Usenet帐户发布来自 http://www.teranews.com

I got tired of reading by now.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com


我使用dev-c ++编译并破坏了你的代码,并添加了一段我的测试

代码,但我不认为你的数组被分解了。

你的意思是什么?

int main(无效){

/ *动态分配的指针数组:* /

char **名字;

int i,n = 2;


names =(char **)alloca(n);

if(names == NULL)

{

printf(" error,names is null!\ n");

退出(1);

}

for(i = 0; i< n; i ++){

names [i] = readr( );

printf(" txt:%s \ n",names [i]);

}


myadd(姓名);

/ *添加我的测试代码* /

i = 0;

while(names [i]){

/ * names [i] = readr(); * /

printf(" txt:%s \ n",names [i ++]);

}

myfree(姓名);

返回0;

}


" Piotrek" <无***** @ noreply.com??????:f3**********@news.task。 gda.pl ...
I compiled and runed ur code with dev-c++,and added a slice of my test
code,but I don''t think ur array is broken up.
what did u mean?
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;

names=(char **)alloca(n);
if(names == NULL)
{
printf("error,names is null!\n");
exit(1);
}
for(i=0;i<n;i++){
names[i]=readr();
printf("txt: %s\n", names[i]);
}

myadd(names);
/*add my test code*/
i = 0;
while(names[i]){
/*names[i]=readr();*/
printf("txt: %s\n", names[i++]);
}
myfree(names);
return 0;
}

"Piotrek" <no*****@noreply.com??????:f3**********@news.task. gda.pl...



我不知道为什么我的重新分配功能(myadd)正在分解我的

数组。我正在努力解决它,它花了我几个小时。也许你

可以查看我的代码并解释我的错误。 (我仍然有问题

指针)。

我保留了我的代码原样。

谢谢!


#include" stdafx.h" / *包含stdio.h * /

#include< stdlib.h>

#include< string.h>


void * alloca(int n);

char * readr(void);

void myadd(char ** A);

void myfree(void * A);


/ *主程序* /

int main(无效){

/ *动态分配指针数组:* /

char **名称;

int i,n = 2;


names = (char **)alloca(n);

for(i = 0; i< n; i ++){

names [i] = readr();

printf(" txt:%s \ n",names [i]);

}


myadd(姓名) ;

myfree(姓名);

返回0;

}


/ *函数* /

void myfree(void * AA){/ *免费所有分配的内存* /

int i;

void ** A =( void **)AA;

for(i = 0; A [i]; i ++)/ *当A [i] = NULL * /

免费时完成释放( A [i]);

免费(AA);

}


void * alloca(int n){

void ** A;

/ *避免n == 0:* /

if(n == 0)n = 1;

if((A =(void *) *)calloc(n + 1,sizeof(* A)))== NULL)返回NULL;

A [n] = NULL;

返回A;

}


char * readr(void){

char * buf;

int i;

char c,BB [256] = {''\ 0''};

fflush(stdin);

/ *阅读255最多字符最后一个字符是''\ 0'':* /

for(i = 0;((c = getchar())!=''\ n'')&& i< ; 255; i ++)

BB [i] = c;

fflush(stdin);

/ * i + 1:启用myfree( )释放所有内存:* /

if((buf =(char *)calloc(i + 1,sizeof(char)))== NULL){

printf(哎呀,不能分配mem。!\ n);

}

else {

strcpy(buf) ,BB);

返回buf;

}

}


/ *应重新分配 ;名称"并添加一个条目:* /

void myadd(char ** A){

int n;

/ *计数行数组:* /

for(n = 0; A [n]; n ++);

/ *它应该重新分配名字但似乎打破它* /

如果((A =(char **)realloc(A,(n + 2)* sizeof(* A)))== NULL)

printf(哎呀,不能重新分配mem。!\ n);

A [n] = readr();

A [n + 1] = NULL;

}
Hi,
I have no idea why my reallocation function (myadd) is breaking up my
array. I''m trying to solve it and it took me few hours already. Maybe you
can look at my code and explain me what is wrong. (I still have problems
with pointers).
I left my code as it is.
Thank you!

#include "stdafx.h" /* contains stdio.h */
#include <stdlib.h>
#include <string.h>
void *alloca(int n);
char *readr(void);
void myadd(char **A);
void myfree(void *A);

/* Main program */
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;

names=(char **)alloca(n);
for(i=0;i<n;i++){
names[i]=readr();
printf("txt: %s\n", names[i]);
}

myadd(names);
myfree(names);
return 0;
}
/* Functions */
void myfree(void *AA){ /* Free all allocated memory */
int i;
void **A=(void **)AA;
for(i=0; A[i]; i++) /* Finish freeing when A[i]=NULL */
free(A[i]);
free(AA);
}

void *alloca(int n){
void **A;
/* Avoid n==0:*/
if(n==0) n=1;
if((A=(void **)calloc(n+1, sizeof(*A)))==NULL) return NULL;
A[n]=NULL;
return A;
}

char *readr(void){
char *buf;
int i;
char c,BB[256]={''\0''};
fflush(stdin);
/* Read 255 characters max. Last char is ''\0'': */
for(i=0; ((c=getchar())!=''\n'')&&i<255; i++)
BB[i]=c;
fflush(stdin);
/* i+1: enable myfree() to free all memory: */
if((buf=(char *)calloc(i+1,sizeof(char)))==NULL){
printf("Oops, can''t allocate mem.!\n");
}
else{
strcpy(buf,BB);
return buf;
}
}
/* Should reallocate "names" and add one more entry to it :*/
void myadd(char **A){
int n;
/* Count rows of array :*/
for(n=0;A[n];n++);
/* It is supposed to reallocate "names" but it seems to break it */
if((A=(char **)realloc(A, (n+2)*sizeof(*A)))==NULL)
printf("Oops, can''t reallocate mem.!\n");
A[n]=readr();
A[n+1]=NULL;
}



2007年5月28日星期一22:32 :34 + 0200,Piotrek< no ***** @ noreply.com>

写道:
On Mon, 28 May 2007 22:32:34 +0200, Piotrek <no*****@noreply.com>
wrote:

><我不知道为什么我的重新分配功能(myadd)会破坏我的
数组。我正在努力解决它,它花了我几个小时。也许
你可以查看我的代码并解释我的错误。 (我仍然有指针问题)。
>Hi,
I have no idea why my reallocation function (myadd) is breaking up my
array. I''m trying to solve it and it took me few hours already. Maybe
you can look at my code and explain me what is wrong. (I still have
problems with pointers).



您的代码有很多错误并调用未定义的行为。

Your code has numerous errors and invokes undefined behavior.


>我将代码保留为是的。
谢谢!

#include" stdafx.h" / *包含stdio.h * /
#include< stdlib.h>
#include< string.h>

void * alloca(int n);
char * readr(void);
void myadd(char ** A);
void myfree(void * A);

/ *主程序* /
int main(void){
/ *动态分配的指针数组:* /

char ** names;

int i,n = 2;


names =(char **)alloca(n);
>I left my code as it is.
Thank you!

#include "stdafx.h" /* contains stdio.h */
#include <stdlib.h>
#include <string.h>
void *alloca(int n);
char *readr(void);
void myadd(char **A);
void myfree(void *A);

/* Main program */
int main(void){
/* Dynamically allocated array of pointers :*/
char **names;
int i,n=2;

names=(char **)alloca(n);



你需要检查alloca是否成功。

You need to check that alloca succeeded.


for(i = 0; i< n; i ++){

names [i] = readr();
for(i=0;i<n;i++){
names[i]=readr();



您需要检查读取器是否成功。

You need to check that readr succeeded.


printf(" txt:%s \ n",names [i]);

}


myadd(姓名);

myfree(姓名);
返回0;
}

/ *函数* /
void myfree(void * AA){/ *免费所有分配的内存* /

int i;

void ** A =(void **)AA;

for(i = 0; A [i]; i ++)/ *完成释放时A [i] = NULL * /

免费(A [i]);

免费(AA);
}

void * alloca(int n){

void ** A;
/ *避免n == 0:* /

if(n == 0) n = 1;

if((A =(void **)calloc(n + 1,sizeof(* A)))== NULL)返回NULL;
printf("txt: %s\n", names[i]);
}

myadd(names);
myfree(names);
return 0;
}
/* Functions */
void myfree(void *AA){ /* Free all allocated memory */
int i;
void **A=(void **)AA;
for(i=0; A[i]; i++) /* Finish freeing when A[i]=NULL */
free(A[i]);
free(AA);
}

void *alloca(int n){
void **A;
/* Avoid n==0:*/
if(n==0) n=1;
if((A=(void **)calloc(n+1, sizeof(*A)))==NULL) return NULL;



这是一个等待发生的问题。您正为n * 1

类型为void *的对象分配空间。在这种情况下,您将使用空格来存储类型为char *的
存储值。该标准保证void *和

char *具有相同的大小(和表示)。但是,如果你曾经使用这个函数来为不同类型的指针分配空间,那么你不知道你是否会分配足够的空间。 sizeof(int *)可能大于sizeof(void *)。


为什么使用calloc?你认为初始化指向所有

位0的指针有一些好处吗?

This is a problem waiting to happen. You are allocating space for n+1
objects of type void*. In this instance, you will use the space to
store values of type char*. The standard guarantees that void* and
char* have the same size (and representation). However, if you ever
use this function to allocate space for different types of pointers,
you have no idea if you will allocate enough space. It is entirely
possible for sizeof (int*) to be greater than sizeof (void*).

Why are you using calloc? Do you think initializing a pointer to all
bits 0 has some benefit?


A [n] = NULL;

返回A;
}

char * readr(void){

char * buf;
int i;

char c,BB [256] = {''\ 0''};

fflush(stdin);
A[n]=NULL;
return A;
}

char *readr(void){
char *buf;
int i;
char c,BB[256]={''\0''};
fflush(stdin);



这会调用未定义的行为。 fflush仅定义为输出

stream。

This invokes undefined behavior. fflush is defined only for output
streams.


> / *最多读取255个字符。最后一个字符是''\ 0'':* /

for(i = 0;((c = getchar())!=''\ n'')&& i< ; 255; i ++)
>/* Read 255 characters max. Last char is ''\0'': */
for(i=0; ((c=getchar())!=''\n'')&&i<255; i++)



getchar返回一个int。 c是一个char。 getchar可能会返回一个不适合char的值。如果是这样,这将调用

不受欢迎的(未定义或实现定义的)行为。

getchar returns an int. c is a char. It is possible for getchar to
return a value that will not fit in a char. If so, this would invoke
undesirable (either undefined or implementation defined) behavior.


BB [i] = c;

fflush(stdin);
/ * i + 1:启用myfree()释放所有内存:* /

if((buf =(char *)calloc( i + 1,sizeof(char)))== NULL){

printf(哎呀,不能分配mem。!\ n);

}

else {

strcpy(buf,BB);

返回buf;

}
BB[i]=c;
fflush(stdin);
/* i+1: enable myfree() to free all memory: */
if((buf=(char *)calloc(i+1,sizeof(char)))==NULL){
printf("Oops, can''t allocate mem.!\n");
}
else{
strcpy(buf,BB);
return buf;
}



如果calloc失败,则不返回任何内容。这将调用

未定义的行为。编译器没有抱怨在没有返回值的情况下到达函数的

结尾了吗?

If calloc failed, you do not return anything. This will invoke
undefined behavior. Did you compiler not complain about reaching the
end of the function without returning a value?


>}

/ *应该重新分配名称并添加一个条目:* /
void myadd(char ** A){

int n;
>}
/* Should reallocate "names" and add one more entry to it :*/
void myadd(char **A){
int n;



这是一个未初始化的自动变量。因此它的

值是不确定的。

This is an automatic variable that is not initialized. Therefore its
value is indeterminate.


> / *计算数组的行:* /

for (N = 0; A [n]的;在n ++);
>/* Count rows of array :*/
for(n=0;A[n];n++);



任何计算n中不确定值的尝试都会调用undefined

行为。

Any attempt to evaluate the indeterminate value in n invokes undefined
behavior.


> / *应该重新分配名称。但似乎打破它* /

if((A =(char **)realloc(A,(n + 2)* sizeof(* A)))== NULL)
>/* It is supposed to reallocate "names" but it seems to break it */
if((A=(char **)realloc(A, (n+2)*sizeof(*A)))==NULL)



n包含垃圾。这可能是消极的。 n + 2没有任何意义。

n contains garbage. It could be negative. n+2 has no meaning.


printf("哎呀,不能重新分配mem。!\ n");

A [n] = readr();
printf("Oops, can''t reallocate mem.!\n");
A[n]=readr();



当realloc失败时,你打印错误信息,然后进入

使用A,好像它包含一个有效的指针。这也会调用未定义的

行为。

When realloc fails, you print the error message but then procede to
use A as if it contained a valid pointer. This also invokes undefined
behavior.


A [n + 1] = NULL;
A[n+1]=NULL;



由于C经过值传递,当您从此函数返回时,A的值

将被丢弃。调用函数中名称的值是

不变。如果realloc成功并分配了一个新的
内存块(而不是重用现有的块),那么该值将不会超过分配区域。实际上,该值变为

不确定,并且在调用函数中使用它会调用

未定义的行为。

Since C passes by value, when you return from this function, the value
of A is discarded. The value of names in the calling function is
unchanged. If the realloc succeeded and allocated a new block of
memory (instead or reusing the existing block), that value will no
longer point to the allocated area. In fact, that value becomes
indeterminate and any use of it in the calling function invokes
undefined behavior.


>}
>}



删除del电子邮件


Remove del for email


这篇关于重新分配一个数组(指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆