在本地函数中分配并初始化指针

发布于 2024-12-06 12:20:07 字数 627 浏览 1 评论 0原文

我想分配内存并将其填充到指针,这是函数参数之一,但我认为我没有得到一些重要的东西,请帮助我。

所以,如果我这样做,一切都会正常:

void alloc(char **p, int n)
{
*p = new char[n];
}

int main() {
char * ptr = NULL;
int n = 10;
alloc(&ptr, n);
for(int i = 0; i<(n - 1); i++)
    ptr[i] = '1';
ptr[n - 1] = '\0';

printf("%s", ptr);
return 0;
}

现在我想将分配的内存也初始化到

void alloc(char **p, int n)
{
*p = new char[n];
for(int i = 0; i<(n - 1); i++)
    *p[i] = '1';
*p[n - 1] = '\0';
} 

int main() {
char * ptr = NULL;
int n = 10;
alloc(&ptr, n); 
printf("%s", ptr);
return 0;
}

程序崩溃函数中。我不明白为什么。请问有人可以解释一下吗?

I want to allocate memory and fill it to the pointer, that are one of the function parameter, but I think that I don't get some important thing, help me please.

So, If I do that everything works fine:

void alloc(char **p, int n)
{
*p = new char[n];
}

int main() {
char * ptr = NULL;
int n = 10;
alloc(&ptr, n);
for(int i = 0; i<(n - 1); i++)
    ptr[i] = '1';
ptr[n - 1] = '\0';

printf("%s", ptr);
return 0;
}

Now I want to initialize the allocated memory also into the function

void alloc(char **p, int n)
{
*p = new char[n];
for(int i = 0; i<(n - 1); i++)
    *p[i] = '1';
*p[n - 1] = '\0';
} 

int main() {
char * ptr = NULL;
int n = 10;
alloc(&ptr, n); 
printf("%s", ptr);
return 0;
}

Program crashes. I don't get why. Please, can someone explain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

好听的两个字的网名 2024-12-13 12:20:07

请尝试使用 (*p)[i](*p)[n - 1]。优先级规则导致 *p[i] 被计算为 *(p[i])

Try (*p)[i] and (*p)[n - 1] instead. The precedence rules cause *p[i] to be evaluated as *(p[i]).

岁月如刀 2024-12-13 12:20:07

试试这个:

((*p)[i]) = '1';

您的操作员评估顺序有问题。

Try this:

((*p)[i]) = '1';

you have a problem with operator's evaluation order.

我最亲爱的 2024-12-13 12:20:07

可能是因为这个:

*p[i]

被解释为好像你写了这个:

*(p[i])

而不是好像你写了这个,这可能就是你的意思:

(*p)[i]

Probably because this:

*p[i]

is interpreted as if you had written this:

*(p[i])

not as if you had written this, which is what you probably meant:

(*p)[i]
爱*していゐ 2024-12-13 12:20:07

这应该可以解决问题,

void
alloc( char*& p, int n )
{
    p = new char[n];
    std::fill_n( p, n - 1, '1' );
    p[n - 1] = '\0';
}

如果您坚持使用指针作为参数,请更改 pall
在函数中 (*p) (并且不要忘记检查空指针
如果你通过了,就做一些合理的事情。

但是,考虑到所有因素,您最好使用
std::string——这个函数甚至不是必需的,因为你
可以写成 std::string( n, '1' )

This should do the trick,

void
alloc( char*& p, int n )
{
    p = new char[n];
    std::fill_n( p, n - 1, '1' );
    p[n - 1] = '\0';
}

If you insist on using a pointer as argument, change all of the p
in the function to (*p) (and don't forget to check for a null pointer
and do something reasonable if you're passed one.

All things considered, however, you'd be better off using
std::string—this function wouldn't even be necessary, as you
could write std::string( n, '1' ).

£噩梦荏苒 2024-12-13 12:20:07

试试这个

#include <stdio.h>   // for NULL

void alloc(char **p, int n)
{
    *p = new char[n];
    for(int i = 0; i<(n - 1); i++)
        *((*p) + i) = '1';
    *((*p) + n - 1) = '\0';
}

int main() {
    char * ptr = NULL;
    int n = 10;
    alloc(&ptr, n);
    printf("%s", ptr);

    // never forget delete pointers
    delete [] ptr;
    return 0;

}

Try this

#include <stdio.h>   // for NULL

void alloc(char **p, int n)
{
    *p = new char[n];
    for(int i = 0; i<(n - 1); i++)
        *((*p) + i) = '1';
    *((*p) + n - 1) = '\0';
}

int main() {
    char * ptr = NULL;
    int n = 10;
    alloc(&ptr, n);
    printf("%s", ptr);

    // never forget delete pointers
    delete [] ptr;
    return 0;

}
夕嗳→ 2024-12-13 12:20:07

这是运营商优先级问题。在你的 alloc 函数中尝试一下:

for(int i = 0; i<(n - 1); i++)
    (*p)[i] = '1';
(*p)[n - 1] = '\0';

Its a precedence of operators issue. Try this in your alloc function:

for(int i = 0; i<(n - 1); i++)
    (*p)[i] = '1';
(*p)[n - 1] = '\0';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文