C开始 [英] C beginer

查看:55
本文介绍了C开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一些事情,需要你的帮助来澄清疑问:

我有一个字符指针初始化如下:

char * ptr =" TestString" ;;

当我通过printf打印这个ptr时,它会正确打印字符串。

但是当我使用delete ptr删除这个ptr时,它会抛出断言。

以下是我的问题:

1)当我做char * ptr =" TestString" ;;它分配了足够的内存吗?

2)printf如何正确访问该值?上面的

赋值是否复制了堆中的值?


感谢您投入任何光线。

I am trying out a few things, need ur help to clear a doubt:
I have a character pointer initialised as below:
char *ptr = "TestString";
When i print this ptr thru printf , it prints the string properly.
But when i delete this ptr using delete ptr, it throws assertion.
following are my questions:
1) When i do char *ptr = "TestString"; does it allocate sufficient
memory?
2) How did the printf accessed the value properly? did the above
assignment copy the values in heap?

Thanks for any light thrown into this.

推荐答案

Le 08-11-2005,CBegin< st ******** @ gmail.com>一个écrit*:
Le 08-11-2005, CBegin <st********@gmail.com> a écrit*:
我正在尝试一些事情,需要你的帮助来澄清一个疑问:
我有一个字符指针初始化如下:
char * ptr =" ;的TestString英寸;


ptr是指向自动只读的指针

char数组TestString。

当我打印时这个ptr通过printf,它正确打印字符串。
但是当我使用delete ptr删除这个ptr时,它会抛出断言。


请注意,删除是一个C ++关键字,而不是C关键字。

但是免费的错误将完全相同。

以下是我的问题:
1)当我做char * ptr =" TestString" ;;它分配了足够的内存吗?


是的。但是分配是在自动变量的内存中完成的,

和free必须仅在动态内存上调用(使用

malloc,calloc,realloc获得)。

2)printf如何正确访问该值?上面的
赋值是否复制堆中的值?
I am trying out a few things, need ur help to clear a doubt:
I have a character pointer initialised as below:
char *ptr = "TestString";
ptr is a pointer that point on the automatic read-only
char array "TestString".
When i print this ptr thru printf , it prints the string properly.
But when i delete this ptr using delete ptr, it throws assertion.
Notice that delete is a C++ keyword, not C one.
But the error would be quite the same with free.
following are my questions:
1) When i do char *ptr = "TestString"; does it allocate sufficient
memory?
Yes. But allocation is done in the memory of automatic variables,
and free must be called only on dynamic memory (obtained with
malloc, calloc, realloc).
2) How did the printf accessed the value properly? did the above
assignment copy the values in heap?



printf获取数组上的指针TestString。所以,它可以

访问它。数组未被复制。


Marc Boyer



printf get a pointer on the array "TestString". So, it could
access it. The array is not copied.

Marc Boyer




Marc Boyer写道:

Marc Boyer wrote:
Le 08-11-2005,CBegin< st ******** @ gmail.com> écrit:
Le 08-11-2005, CBegin <st********@gmail.com> a écrit :
我正在尝试一些事情,需要你的帮助来澄清疑问:
我有一个字符指针初始化如下:
char * ptr =" TestString" ;;
ptr是指向自动只读
char数组TestString的指针。
I am trying out a few things, need ur help to clear a doubt:
I have a character pointer initialised as below:
char *ptr = "TestString";
ptr is a pointer that point on the automatic read-only
char array "TestString".



非常感谢,


这是什么内部发生的:

char * ptr =" TestString";

扩展到:

const char temp [] =" TestString";

ptr = temp; // ptr保存只读缓冲区的地址

//

所以当我尝试免费p时,它实际上会尝试清理分配给
$ b的内存$ b临时变量,它不存储在堆中,但存储在堆栈中。


我是否正确?


Thanks a lot,

Is this whats happening internally:
char *ptr = "TestString";
gets expanded to:
const char temp[] = "TestString";
ptr = temp; // ptr holds the address of read only buffer
//
so when i try free p, it actually tries to clean the memory assigned to
temp variable, which is not stored in heap, but on stack.

Am i correct?

当我通过printf打印此ptr时,它会打印出来正确的字符串。
但是当我使用delete ptr删除这个ptr时,它会抛出断言。
When i print this ptr thru printf , it prints the string properly.
But when i delete this ptr using delete ptr, it throws assertion.



请注意,delete是一个C ++关键字,而不是C关键字。
但是这个错误与free是完全一样的。



Notice that delete is a C++ keyword, not C one.
But the error would be quite the same with free.

以下是我的问题:
1)当我做char * ptr =" TestString" ;;它分配了足够的内存吗?
following are my questions:
1) When i do char *ptr = "TestString"; does it allocate sufficient
memory?



是的。但是分配是在自动变量的内存中完成的,而且必须只在动态内存上调用free(使用malloc,calloc,realloc获得)。



Yes. But allocation is done in the memory of automatic variables,
and free must be called only on dynamic memory (obtained with
malloc, calloc, realloc).

2)printf如何正确访问该值?上面的
赋值是否复制堆中的值?
2) How did the printf accessed the value properly? did the above
assignment copy the values in heap?



printf获取数组上的指针TestString。所以,它可以访问它。数组未被复制。

Marc Boyer



printf get a pointer on the array "TestString". So, it could
access it. The array is not copied.

Marc Boyer






Le 08-11-2005,CBegin< st ********@gmail.com> aécrit*:
Le 08-11-2005, CBegin <st********@gmail.com> a écrit*:
Marc Boyer写道:
Marc Boyer wrote:
Le 08-11-2005,CBegin< st ******** @ gmail.com> écrit:
Le 08-11-2005, CBegin <st********@gmail.com> a écrit :
>我正在尝试一些事情,需要你的帮助来澄清疑问:
>我有一个字符指针初始化如下:
> char * ptr =" TestString";
ptr是指向自动只读
char数组TestString的指针。
> I am trying out a few things, need ur help to clear a doubt:
> I have a character pointer initialised as below:
> char *ptr = "TestString";
ptr is a pointer that point on the automatic read-only
char array "TestString".


非常感谢, char * ptr =" TestString";
扩展为:
const char temp [] =" TestString";
ptr = temp; // ptr保存只读缓冲区的地址


Thanks a lot,

Is this whats happening internally:
char *ptr = "TestString";
gets expanded to:
const char temp[] = "TestString";
ptr = temp; // ptr holds the address of read only buffer




你是对的(除了''const'':这是正确的

想法,但我不确定这是完全相同的,这是

为什么我使用''只读''而不是''const'')。

//
所以当我尝试免费p时,它实际上会尝试清理分配给
临时变量的内存,该变量不存储在堆中,而是存储在堆栈中。

我是对的吗?



You are right (except perhaps the ''const'': this is the right
idea, but I am not sure this is exactly the same, this is
why I have use ''read-only'' instead of ''const'').
//
so when i try free p, it actually tries to clean the memory assigned to
temp variable, which is not stored in heap, but on stack.

Am i correct?




是的,除了据我所知,C标准中没有堆栈。

但是,有很多实施中的一个。


Marc Boyer



Yes, except that, to my knowledge, there is no stack in the C standart.
But, there is one in a lot of implementations.

Marc Boyer


这篇关于C开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆