简体   繁体   English

如何找到字符串文字的地址?

[英]How can I find the address of a String Literal?

Suppose I have the following:假设我有以下内容:

char *a = "Learning CPP";
char *b = "Learning CPP";

Can I say that the total memory used was sizeof (a) and not 2*sizeof (stringliteral) ?我可以说使用的总内存是 sizeof (a) 而不是 2*sizeof (stringliteral) 吗?

Cause my understanding of String literals is that one copy of the string is stored.因为我对字符串文字的理解是存储了字符串的一个副本。 But however,但无论如何,

Isn't it stored in between a, a+1, a+2 ..... a + 12 address in memory and also b, b + 1, b + 2 ... b + 12 in memory (12 is sizeof string)?是不是存储在 a, a+1, a+2 ..... a + 12 内存地址和 b, b + 1, b + 2 ... b + 12 之间(12 是 sizeof细绳)?

How can I find the address of a string literal ?如何找到字符串文字的地址?

You have found the address of a string literal.找到字符串文字的地址。 The value of a string literal is the address of its first character .字符串文字的值是它的第一个字符的地址 You assigned that value to a and b .您将该值分配给ab

Can I say that the total memory used was sizeof (a) and not 2*sizeof (a) ?我可以说使用的总内存是 sizeof (a) 而不是 2*sizeof (a) 吗?

First off the question is malformed.首先这个问题格式不正确。 sizeof(a) is the size of the pointer. sizeof(a)是指针的大小。

You intended to ask:你想问:

Can I say that the total memory used for the string literals is the size required for one copy of the string, and not the size required for two copies of the string?我可以说用于字符串文字的总内存是字符串的一个副本所需的大小,而不是字符串的两个副本所需的大小吗?

No. That's an implementation detail of the compiler.不,这是编译器的一个实现细节。 It may choose to intern the strings or not at its whim.它可以选择实习字符串或不随心所欲。 If you modify one of the strings, it is implementation-defined whether the other string is observed to be modified.如果您修改其中一个字符串,则是否观察到另一个字符串被修改由实现定义。

This is a consequence of the more general fact that if you modify one of the strings, it is implementation-defined what happens, period.这是一个更普遍的事实的结果,即如果您修改其中一个字符串,它是由实现定义的会发生什么,周期。 Anything can happen.任何事情都可能发生。

my understanding of string literals is that one copy of the string is stored.我对字符串文字的理解是存储一个字符串副本。

Your understanding is mistaken.你的理解是错误的。 That is not a guarantee of the language.这不是语言的保证。 That's an optimization that a compiler can choose to make or not.这是编译器可以选择进行或不进行的优化。

Isn't it stored in between a, a+1, a+2 ..... a + 12 address in memory and also b, b + 1, b + 2 ... b + 12 in memory (12 is sizeof string)?是不是存储在 a, a+1, a+2 ..... a + 12 内存地址和 b, b + 1, b + 2 ... b + 12 之间(12 是 sizeof细绳)?

I cannot understand this question.我无法理解这个问题。 What is the "it" that isn't being stored?没有被存储的“它”是什么?

The following code will show you where the variables are and where the strings are以下代码将向您显示变量所在的位置以及字符串所在的位置

printf( "variable a is at address %p\n", &a );
printf( "variable b is at address %p\n", &b );
printf( "the string that a points to is at address %p\n", a );
printf( "the string that b points to is at address %p\n", b );

Note that the last two lines may or may not print the same address, depending on the compiler.请注意,最后两行可能会也可能不会打印相同的地址,具体取决于编译器。 There is no requirement that the compiler create only one string.不要求编译器只创建一个字符串。

You can check it yourself.你可以自己检查一下。 For example例如

if ( a == b ) std::cout << "The string literals are coincide" << std::endl;
else std::cout << "The string literals occupy different areas of memory" << std::endl;

Usually compilers have options that allow to control whether two identical string literals will occupy the same memory or each literal will be allocated in its own area of memory.通常编译器有一些选项可以控制两个相同的字符串文字是占用相同的内存还是每个文字都将分配在自己的内存区域中。

Can I say that the total memory used was sizeof (a) and not 2*sizeof (a) ?我可以说使用的总内存是 sizeof (a) 而不是 2*sizeof (a) 吗? --> No, sizeof(a) is sizeof(char *) = 4 --> 不,sizeof(a) 是 sizeof(char *) = 4

Isn't it stored in between a, a+1, a+2 ..... a + 12 address in memory and also b, b + 1, b + 2 ... b + 12 in memory (12 is sizeof string)?是不是存储在 a, a+1, a+2 ..... a + 12 内存地址和 b, b + 1, b + 2 ... b + 12 之间(12 是 sizeof细绳)? --> Using the gcc compiler, when the strings are equal, a and b are the same address. --> 使用gcc编译器,当字符串相等时,a和b是同一个地址。 When the strings are different, I find different a and b are different addresses.当字符串不同时,我发现不同的 a 和 b 是不同的地址。 (hey, this was a fun exercise..I learned something new) (嘿,这是一个有趣的练习..我学到了一些新东西)

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
char const* p ="Hello"; 
printf("%d",p); //prints the address of the first byte. ie of 'H' 
cout<<p[0] // prints H
cout<<p[1] //prints e
}

You can do this in C or C++ to determine the address of a string literal.您可以在 C 或 C++ 中执行此操作以确定字符串文字的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM