为什么 malloc() 更改 char 指针自己的地址
why malloc() changes char pointers own address
我想编写一个函数,它可以使用目标指针来分配内存并将源复制到其中。
如果我尝试在函数内为目标分配内存,函数内的 char *dest 会获得一个完整的新地址,我指的是指针本身的地址。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void ownStrCpy(char *dest, char *src){
printf("%p = address inside ptr variable c inside function before malloc\n", dest);
dest = (char*)malloc(strlen(src)*sizeof(char)+1);
printf("%p = address inside ptr variable c inside function after malloc\n", dest);
printf("%p = address from ptr variable c inside function after malloc\n", &dest);
for(int i=0; i < strlen(src); i++){
dest[i] = src[i];
}
dest[strlen(src)+1] = '[=10=]';
}
int main()
{
char *c;
char a = 'a';
//c = NULL;
c = &a;
printf("%p = address from variable a\n", &a);
printf("%p = address from ptr variable c\n", &c);
printf("%p = address inside ptr variable c\n", c);
ownStrCpy(c, "Example String");
printf("%p = address inside ptr variable c after function call\n", c);
char *c2;
printf("\n%p = address from variable c2\n", &c2);
printf("%p = address inside ptr variable c2\n", c2);
c2 = (char*)malloc(10*sizeof(char)+1);
printf("%p = address inside ptr variable c2\n", c2);
printf("%p = address from ptr variable c2 after malloc\n", &c2);
//free(c);
free(c2);
return 0;
}
这就是输出。
000000000061FE17 = address from variable a
000000000061FE18 = address from ptr variable c
000000000061FE17 = address inside ptr variable c
000000000061FE17 = address inside ptr variable c inside function before malloc
0000000000A46E30 = address inside ptr variable c inside function after malloc
000000000061FDE0 = address from ptr variable c inside function after malloc
000000000061FE17 = address inside ptr variable c after function call
000000000061FE08 = address from variable c2
000000000000003E = address inside ptr variable c2
0000000000A46E50 = address inside ptr variable c2
000000000061FE08 = address from ptr variable c2 after malloc
如您所见,'the address from ptr variable c inside function after malloc' 已更改为不存在的地址?
我尝试更改将指针移交给函数的方式,尝试了双指针,尝试以不同方式访问函数内部的指针,但没有任何效果。
我知道我可以在 main 函数中 return 指向指针变量 'c' 的指针,但这不是我想要的。 main 函数内部的部分在为 c2 调用 malloc() 后保持不变的部分有何不同?
指针只是一个数字。考虑一下:
int dest = 123;
dest = 456;
dest
将是 456。
现在考虑这个。
char *c;
char a = 'a';
c = &a;
char *dest = c;
dest = malloc(10);
dest
会包含 &a
吗?不,它将包含 malloc
.
的结果
这就是 ownStrCpy
正在做的事情。它立即用 malloc
.
的内容覆盖 dest
你可以用双指针做你想做的事情。
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
void ownStrCpy(char **dest, const char *src) {
size_t len = strlen(src);
// Allocate new memory.
// char is defined to be sizeof 1
// don't cast the result of malloc
char *copy = malloc(len+1);
// Copy to the new memory.
for(int i=0; i <= len; i++) {
copy[i] = src[i];
}
copy[len+1] = '[=12=]';
// `dest` points at c.
// `*dest` is the value of c, currently `&a`.
// `*dest = copy` changes the value of `c` to `copy`.
*dest = copy;
}
int main() {
char *c;
char a = 'a';
c = &a;
ownStrCpy(&c, "Example String");
printf("c = %s", c);
}
通过传入&c
你可以改变ownStrCpy
里面的c
的值。
但是,如果您要在函数内分配新内存,只需 return 新指针。
char *ownStrCpy(const char *src) {
size_t len = strlen(src);
// Allocate new memory.
// char is defined to be sizeof 1
// don't cast the result of malloc
char *copy = malloc(len+1);
// Copy to the new memory.
for(int i=0; i <= len; i++) {
copy[i] = src[i];
}
copy[len+1] = '[=13=]';
return copy;
}
int main() {
char *c;
char a = 'a';
c = &a;
c = ownStrCpy("Example String");
printf("c = %s", c);
}
并且您刚刚实施了 strdup
。
我想编写一个函数,它可以使用目标指针来分配内存并将源复制到其中。
如果我尝试在函数内为目标分配内存,函数内的 char *dest 会获得一个完整的新地址,我指的是指针本身的地址。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void ownStrCpy(char *dest, char *src){
printf("%p = address inside ptr variable c inside function before malloc\n", dest);
dest = (char*)malloc(strlen(src)*sizeof(char)+1);
printf("%p = address inside ptr variable c inside function after malloc\n", dest);
printf("%p = address from ptr variable c inside function after malloc\n", &dest);
for(int i=0; i < strlen(src); i++){
dest[i] = src[i];
}
dest[strlen(src)+1] = '[=10=]';
}
int main()
{
char *c;
char a = 'a';
//c = NULL;
c = &a;
printf("%p = address from variable a\n", &a);
printf("%p = address from ptr variable c\n", &c);
printf("%p = address inside ptr variable c\n", c);
ownStrCpy(c, "Example String");
printf("%p = address inside ptr variable c after function call\n", c);
char *c2;
printf("\n%p = address from variable c2\n", &c2);
printf("%p = address inside ptr variable c2\n", c2);
c2 = (char*)malloc(10*sizeof(char)+1);
printf("%p = address inside ptr variable c2\n", c2);
printf("%p = address from ptr variable c2 after malloc\n", &c2);
//free(c);
free(c2);
return 0;
}
这就是输出。
000000000061FE17 = address from variable a
000000000061FE18 = address from ptr variable c
000000000061FE17 = address inside ptr variable c
000000000061FE17 = address inside ptr variable c inside function before malloc
0000000000A46E30 = address inside ptr variable c inside function after malloc
000000000061FDE0 = address from ptr variable c inside function after malloc
000000000061FE17 = address inside ptr variable c after function call
000000000061FE08 = address from variable c2
000000000000003E = address inside ptr variable c2
0000000000A46E50 = address inside ptr variable c2
000000000061FE08 = address from ptr variable c2 after malloc
如您所见,'the address from ptr variable c inside function after malloc' 已更改为不存在的地址?
我尝试更改将指针移交给函数的方式,尝试了双指针,尝试以不同方式访问函数内部的指针,但没有任何效果。
我知道我可以在 main 函数中 return 指向指针变量 'c' 的指针,但这不是我想要的。 main 函数内部的部分在为 c2 调用 malloc() 后保持不变的部分有何不同?
指针只是一个数字。考虑一下:
int dest = 123;
dest = 456;
dest
将是 456。
现在考虑这个。
char *c;
char a = 'a';
c = &a;
char *dest = c;
dest = malloc(10);
dest
会包含 &a
吗?不,它将包含 malloc
.
这就是 ownStrCpy
正在做的事情。它立即用 malloc
.
dest
你可以用双指针做你想做的事情。
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
void ownStrCpy(char **dest, const char *src) {
size_t len = strlen(src);
// Allocate new memory.
// char is defined to be sizeof 1
// don't cast the result of malloc
char *copy = malloc(len+1);
// Copy to the new memory.
for(int i=0; i <= len; i++) {
copy[i] = src[i];
}
copy[len+1] = '[=12=]';
// `dest` points at c.
// `*dest` is the value of c, currently `&a`.
// `*dest = copy` changes the value of `c` to `copy`.
*dest = copy;
}
int main() {
char *c;
char a = 'a';
c = &a;
ownStrCpy(&c, "Example String");
printf("c = %s", c);
}
通过传入&c
你可以改变ownStrCpy
里面的c
的值。
但是,如果您要在函数内分配新内存,只需 return 新指针。
char *ownStrCpy(const char *src) {
size_t len = strlen(src);
// Allocate new memory.
// char is defined to be sizeof 1
// don't cast the result of malloc
char *copy = malloc(len+1);
// Copy to the new memory.
for(int i=0; i <= len; i++) {
copy[i] = src[i];
}
copy[len+1] = '[=13=]';
return copy;
}
int main() {
char *c;
char a = 'a';
c = &a;
c = ownStrCpy("Example String");
printf("c = %s", c);
}
并且您刚刚实施了 strdup
。