【发布时间】:2015-11-18 09:49:36
【问题描述】:
在开始之前,我应该说我已经看过几个关于这个主题的帖子(比如this one,但我仍然缺少一些东西。我对 C 很陌生,所以请多多包涵。我正在尝试构建一个函数将字符串从一个指针位置复制到另一个位置。
#include <stdio.h>
#include <malloc.h>
#include <string.h>
void astrncpy(char **base, char *copyme){
printf("Copying '%s' into a location currently featuring the following string: '%s'\n", copyme,*base);
printf("The location of our string to be copied (%s) is %d.\n", copyme, ©me);
printf("The location of our string to be replaced (%s) is %d.\n", *base, base);
//Declare string length variable
int new_len=strlen(copyme);
printf("Calculating new length for replaced memory allocation (%d).\n",new_len);
//Reallocate pointer array
*base=realloc(*base,sizeof(char)*new_len+1);
printf("Reallocating memory allocation.\n");
//Copy copyme content to base string location
strncpy(*base,copyme,new_len);
printf("The string at location %d is now %s\n", base, *base);
}
void main(){
//Declare iterator
int i;
//Generate strings
char first_lit[]="Fortran?";
char second[]="Now that's a name I've not heard in a long time.";
//Convert first string to array (so we can get at the pointer to the strings pointer)
char **first; //Declare pointer to pointer array that represents the first string
first=malloc(strlen(first_lit)*sizeof(char)); //Allocate space for the pointer array
*first=first_lit; //Assign values to the pointer locations
//Copy copyme into base
astrncpy(first,second);
}
当我尝试在astrncpy() 内重新分配时,核心转储。据我了解,如果指针数组不是 NULL,或者不是malloc() 的乘积,就会发生这种情况。我觉得我没有通过那次测试。任何有关正在发生的事情的指导将不胜感激。
对我构建输入 *base 的见解的奖励积分。我花了很多时间尝试确定**base 参数的可接受输入是什么(注意astrncpy() 的两个参数都在我正在处理的文本中给出)。但是,我不清楚为什么我不能使用first_lit 而不是必须构造first。字符串仍然是一个数组,不是吗?再次感谢您的帮助。
更新:我搁置了一段时间来做我实际上得到报酬的工作,但回到它,我无法动摇以下响应中的声明修改的想法没有必要。 (这是因为它们尚未包含在文本中。)无论如何,我仍然会对解决方案进行检查,因为它有效并且在多个方面都有帮助。但是,应该注意的是,以下方法也有效:
/*Program demonstrates string manipulation operations*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
void astrncpy(char **base, char *copyme){
printf("\nWe are inside the function, astrncpy.\n");
printf("\nCopying '%s' (*copyme) into a location currently featuring the following string: '%s' (**base)\n", copyme,*base);
printf("The location of our string to be copied (%s) is %p.\n", copyme, ©me);
printf("The location of our string to be replaced (%s) is %p.\n", *base, base);
//Declare string length variable
size_t new_len=strlen(copyme);
printf("Calculating new length for replaced memory allocation (%d).\n",new_len);
//Reallocate pointer array
printf("Reallocating memory block associated with base string to be replaced.\n");
*base=realloc(*base,sizeof(char)*(new_len+1));
//Copy copyme content to base string location
strncpy(*base,copyme,new_len);
printf("The string at location %p is now %s\n", base, *base);
}
void main(){
//Declare iterator
int i;
//Generate strings
char first_lit[]="Fortran?";
char second[]="Now that's a name I've not heard in a long time.";
//int testint=5;
//Capture elements of first_lit in pointer array (so we can get at the pointer to the strings pointer)
char *first; //Declare pointer to pointer array that represents the first string
first=malloc((strlen(first_lit)+1)*sizeof(char)); //Allocate space for the pointer array
strncpy(first,first_lit,strlen(first_lit)); //Assign values to the pointer locations
//Copy copyme into base
printf("Initiating copy operation...\n");
astrncpy(&first,second);
}
【问题讨论】:
-
您的链接问题有一个答案“唯一可以传递给 realloc 的指针是空指针以及之前由 calloc、malloc 或 realloc 返回的指针!”
-
注意:这是
int main(void)-- C 不是 Java。 -
first=malloc(strlen(first_lit)*sizeof(char))- 这完全是错误的尺寸计算 -
strncpy(*base,copyme,new_len);- 你忘了空终止字符串。要解决此问题,请使用strcpy(*base, copyme);。strncpy函数是 strcpy 的不安全版本,应避免使用
标签: c segmentation-fault realloc