重新分配一个数组(指针)

编程入门 行业动态 更新时间:2024-10-27 17:13:02
本文介绍了重新分配一个数组(指针)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不知道为什么我的重新分配功能(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; }

我现在厌倦了阅读。 - < www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt> < http://www.securityfocus /专栏作家/ 423> < www.aaxnet/editor/edit043.html> < kadaitcha.cx /vista/dogsbreakfast/index.html> cbfalconer at maineline dot net - 通过免费的Usenet帐户发布来自 www.teranews

I got tired of reading by now. -- <www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt> <www.securityfocus/columnists/423> <www.aaxnet/editor/edit043.html> <kadaitcha.cx/vista/dogsbreakfast/index.html> cbfalconer at maineline dot net -- Posted via a free Usenet account from www.teranews

我使用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??????: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??????: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> 写道: On Mon, 28 May 2007 22:32:34 +0200, Piotrek <no*****@noreply> wrote: ><我不知道为什么我的重新分配功能(myadd)会破坏我的数组。我正在努力解决它,它花了我几个小时。也许你可以查看我的代码并解释我的错误。 (我仍然有指针问题)。 >Hi,I have no idea why my reallocation function (myadd) is breaking up myarray. I''m trying to solve it and it took me few hours already. Maybeyou can look at my code and explain me what is wrong. (I still haveproblems 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

更多推荐

重新分配一个数组(指针)

本文发布于:2023-07-29 08:40:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239159.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   指针   分配

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!