需要帮助理解使用堆栈和堆内存的指针和字符串
Need help in understanding Pointers and Strings using stack and heap memory
我试图理解当指针、字符串和函数与 heap/stack 内存组合时的底层过程。我能够理解和学习,但我最终出现了两个错误,我没有找到原因。
我的问题出在这里:
// printf("%s\n", *ptrToString); // Gives bad mem access error if heap memory used
// printf("%s\n", ptrToString); // Output is wrong if stack was used for memory, and prints some hex values instead
谁能解释一下我在这里缺少什么?另外,我想询问一些关于我的代码的反馈,并提出我们可以做出的任何改进。
谢谢
完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define USE_STACK_MEMORY 0
char* NewString(char string[])
{
unsigned long num_chars;
char *copy = NULL;
// Find string length
num_chars = strlen(string);
// Allocate memory
#if USE_STACK_MEMORY
copy = alloca(sizeof(copy) + num_chars + 1); // Use stack memory
#else
copy = malloc(sizeof(copy) + num_chars + 1); // Use heap memory
#endif
// Make a local copy
strcpy(copy, string);
// If we use stack then it returns a string literal
return copy;
}
int main(void)
{
char *ptrToString = NULL;
ptrToString = NewString("HI");
printf("%s\n", ptrToString);
// printf("%s\n", *ptrToString); // Gives bad mem access error if heap memory used
// printf("%s\n", ptrToString); // Output is wrong if stack was used for memory, and prints some hex values instead
#if !USE_STACK_MEMORY
if ( ptrToString ) {
free(ptrToString);
}
#endif
return 0;
}
第一次打印读取指针指向的值。然后它将此值解释为指向字符串的指针。这意味着您的字符串的第一个值将被解释为字符串所在的地址。
第二次打印对于堆栈内存是错误的,因为您使用 alloca
分配的内存会在您的 NewString 方法 returns 后立即自动释放。
来自 alloca 的手册页:
The alloca() function allocates size bytes of space in the stack frame
of the caller. This temporary space is automatically freed when the
function that called alloca() returns to its caller.
我试图理解当指针、字符串和函数与 heap/stack 内存组合时的底层过程。我能够理解和学习,但我最终出现了两个错误,我没有找到原因。
我的问题出在这里:
// printf("%s\n", *ptrToString); // Gives bad mem access error if heap memory used
// printf("%s\n", ptrToString); // Output is wrong if stack was used for memory, and prints some hex values instead
谁能解释一下我在这里缺少什么?另外,我想询问一些关于我的代码的反馈,并提出我们可以做出的任何改进。
谢谢
完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define USE_STACK_MEMORY 0
char* NewString(char string[])
{
unsigned long num_chars;
char *copy = NULL;
// Find string length
num_chars = strlen(string);
// Allocate memory
#if USE_STACK_MEMORY
copy = alloca(sizeof(copy) + num_chars + 1); // Use stack memory
#else
copy = malloc(sizeof(copy) + num_chars + 1); // Use heap memory
#endif
// Make a local copy
strcpy(copy, string);
// If we use stack then it returns a string literal
return copy;
}
int main(void)
{
char *ptrToString = NULL;
ptrToString = NewString("HI");
printf("%s\n", ptrToString);
// printf("%s\n", *ptrToString); // Gives bad mem access error if heap memory used
// printf("%s\n", ptrToString); // Output is wrong if stack was used for memory, and prints some hex values instead
#if !USE_STACK_MEMORY
if ( ptrToString ) {
free(ptrToString);
}
#endif
return 0;
}
第一次打印读取指针指向的值。然后它将此值解释为指向字符串的指针。这意味着您的字符串的第一个值将被解释为字符串所在的地址。
第二次打印对于堆栈内存是错误的,因为您使用 alloca
分配的内存会在您的 NewString 方法 returns 后立即自动释放。
来自 alloca 的手册页:
The alloca() function allocates size bytes of space in the stack frame of the caller. This temporary space is automatically freed when the function that called alloca() returns to its caller.