了解指针在结构和的malloc [英] Understanding pointers in a structure and malloc
问题描述
我刚学C(读山姆自学℃下24小时)。我已经通过指针和内存分配得到,但现在我想了解一下它们的结构中。
I am just learning C (reading Sam's Teach Yourself C in 24 hours). I've gotten through pointers and memory allocation, but now I'm wondering about them inside a structure.
我写了下面的小程序来玩耍,但我不知道这是否正常与否。编译的Linux系统上用什么都不缺编 -Wall
标记的gcc,但我不知道那是100%可靠的。
I wrote the little program below to play around, but I'm not sure if it is OK or not. Compiled on a Linux system with gcc with the -Wall
flag compiled with nothing amiss, but I'm not sure that is 100% trustworthy.
它是确定改变指针的分配大小如我在下面做还是我可能在相邻的存储踏步?我做了一些前/在结构变量后,试图检查,但不知道是否可行,如果结构元素被连续存储在内存中(我猜这样以来指针的结构可以传递给的函数,并通过指针位置操纵结构)。另外,我怎么能访问指针位置的内容,并通过它遍历这样我就可以确保没有得到覆盖,如果它是连续的?我估计有一件事我问的是我怎么能调试内存搞乱这种方式来知道它是没有违反什么?
Is it ok to change the allocation size of a pointer as I have done below or am I possibly stepping on adjacent memory? I did a little before/after variable in the structure to try to check this, but don't know if that works and if structure elements are stored contiguously in memory (I'm guessing so since a pointer to a structure can be passed to a function and the structure manipulated via the pointer location). Also, how can I access the contents of the pointer location and iterate through it so I can make sure nothing got overwritten if it is contiguous? I guess one thing I'm asking is how can I debug messing with memory this way to know it isn't breaking anything?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct hello {
char *before;
char *message;
char *after;
};
int main (){
struct hello there= {
"Before",
"Hello",
"After",
};
printf("%ld\n", strlen(there.message));
printf("%s\n", there.message);
printf("%d\n", sizeof(there));
there.message = malloc(20 * sizeof(char));
there.message = "Hello, there!";
printf("%ld\n", strlen(there.message));
printf("%s\n", there.message);
printf("%s %s\n", there.before, there.after);
printf("%d\n", sizeof(there));
return 0;
}
我在想自己是不是正确的,因为大小我的有
没有change.kj
I'm thinking something is not right because the size of my there
didn't change.kj
亲切的问候,
推荐答案
不是真的好,你有内存泄漏,您可以使用 的Valgrind的在运行时(在Linux上)检测到它。
Not really ok, you have a memory leak, you could use valgrind to detect it at runtime (on Linux).
您的编码:
there.message = malloc(20 * sizeof(char));
there.message = "Hello, there!";
第一次分配调用的malloc(3)。首先,当调用的malloc
你应该总是它是否有故障。但事实上它通常成功。因此,更好地code。至少:
The first assignment call malloc(3). First, when calling malloc
you should always test if it fails. But indeed it usually succeeds. So better code at least:
there.message = malloc(20 * sizeof(char));
if (!there.message)
{ perror("malloc of 20 failed"); exit (EXIT_FAILURE); }
第二次分配将字面常量字符串的地址您好,有!
到同一个指针 there.message
,你已经失去了第一个值。你可能想复制字符串常量
The second assignment put the address of the constant literal string "Hello, there!"
into the same pointer there.message
, and you have lost the first value. You probably want to copy that constant string
strncpy (there.message, "Hello, there!", 20*sizeof(char));
(你可以只使用的strcpy(3),但提防缓冲区溢出)
您可以得到一个新的副本(堆)使用一些字符串的的strdup(3)(和GNU libc中也已 asprintf(3 ) ...)
You could get a fresh copy (in heap) of some string using strdup(3) (and GNU libc has also asprintf(3) ...)
there.message = strdup("Hello, There");
if (!there.message)
{ perror("strdup failed"); exit (EXIT_FAILURE); };
最后,这是很好的味道免费
在程序结束堆内存。
(但操作系统会苏preSS在 _exit进程空间( 2)时间。
At last, it is good taste to free
at program end the heap memory.
(But the operating system would supress the process space at _exit(2) time.
了解更多的 C语言编程的,内存管理,<一个HREF =http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29相对=nofollow>垃圾收集。或许可以考虑使用贝姆保守的GC
Read more about C programming, memory management, garbage collection. Perhaps consider using Boehm's conservative GC
C指针只是一个内存地址区。应用程序需要知道它们的大小。
A C pointer is just a memory address zone. Applications need to know their size.
PS。在C语言手册内存管理是棘手的,甚至对经验丰富的老将的程序员。
这篇关于了解指针在结构和的malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!