为字符串动态分配内存的一般方法 [英] General method for dynamically allocating memory for a string

查看:100
本文介绍了为字符串动态分配内存的一般方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上搜索了malloc和动态malloc;但是,我仍然不知道或者很容易看到什么是将内存分配给char的一般方法*

变量我想要分配我在里面找到的子字符串一个

字符串。


任何想法?

I have searched the internet for malloc and dynamic malloc; however, I still
don''t know or readily see what is general way to allocate memory to char *
variable that I want to assign the substring that I found inside of a
string.

Any ideas?

推荐答案

" smnoff" < 34 ************** @ hotmail.comwrites:
"smnoff" <34**************@hotmail.comwrites:

我在互联网上搜索了malloc和动态malloc;但是,我仍然不知道或者很容易看到什么是将内存分配给char的一般方法*

变量我想要分配我在里面找到的子字符串一个

字符串。
I have searched the internet for malloc and dynamic malloc; however, I still
don''t know or readily see what is general way to allocate memory to char *
variable that I want to assign the substring that I found inside of a
string.



如果要为字符串分配内存,请将

非空字符的数量加一,再加上malloc ,然后将

字符串复制到其中,确保空终止结果。


如果你可以更具体地说明你在尝试什么要做,

也许我们可以提供一些细节帮助。

-

我希望,有一天,学会阅读。

似乎比写作更难。

- 理查德希思菲尔德

If you want to allocate memory for a string, pass the number of
non-null characters in it, plus one, to malloc, then copy the
string into it, being sure to null-terminate the result.

If you can be more specific about what you''re trying to do,
perhaps we can help with some details.
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield


smnoff发布:
smnoff posted:

我在互联网上搜索了malloc和动态malloc;但是,我还是不知道或者很容易看到什么是分配内存的一般方法

到char *变量我想要分配我找到的子字符串

在一个字符串里面。


有什么想法吗?
I have searched the internet for malloc and dynamic malloc; however, I
still don''t know or readily see what is general way to allocate memory
to char * variable that I want to assign the substring that I found
inside of a string.

Any ideas?



未经检查的代码,可能包含一两个错误:


#include< stdlib.h>

#include< stddef.h>

#include< assert.h>


char * to_release;


void ReleaseLastString(无效)

{

免费(to_release);

}


char const * CreateSubstring(char const * const p,

size_t const istart,size_t const iend)

{

int assert_dummy =(断言(!! p),断言(!! istart),断言(!! iend),0);


if(!p [iend + 1])return to_release = 0,p + istart;


to_release = malloc(iend - istart + 1);


memcpy(to_release,p,iend - istart);


to_release [iend - istart] = 0;


return to_release;

}


int main()

{

puts(CreateSubstring(" abcdefghijklmnop",2,7));

ReleaseLastString();


puts(CreateSubstring (abcdefghijklmnop,4,15));

ReleaseLastString();

}


-


Frederick Gotham


Unchecked code, may contain an error or two:

#include <stdlib.h>
#include <stddef.h>
#include <assert.h>

char *to_release;

void ReleaseLastString(void)
{
free(to_release);
}

char const *CreateSubstring(char const *const p,
size_t const istart,size_t const iend)
{
int assert_dummy = (assert(!!p),assert(!!istart),assert(!!iend),0);

if(!p[iend+1]) return to_release = 0, p + istart;

to_release = malloc(iend - istart + 1);

memcpy(to_release,p,iend - istart);

to_release[iend - istart] = 0;

return to_release;
}

int main()
{
puts(CreateSubstring("abcdefghijklmnop",2,7));
ReleaseLastString();

puts(CreateSubstring("abcdefghijklmnop",4,15));
ReleaseLastString();
}

--

Frederick Gotham




" Frederick Gotham" < fg ******* @ SPAM.com在留言中写道

新闻:r9 ******************* @ news.indigo .ie ...

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:r9*******************@news.indigo.ie...

smnoff发布:
smnoff posted:

>我在互联网上搜索了malloc和动态malloc;但是,我仍然不知道或者很容易看到将内存分配给char *变量的一般方法是什么,我想在字符串中分配我找到的子字符串。

任何想法?
>I have searched the internet for malloc and dynamic malloc; however, I
still don''t know or readily see what is general way to allocate memory
to char * variable that I want to assign the substring that I found
inside of a string.

Any ideas?




未经检查的代码,可能包含一两个错误:


#include< stdlib.h>

#include< stddef.h>

#include< assert.h>


char * to_release;


void ReleaseLastString(无效)

{

免费(to_release);

}


char const * CreateSubstring(char const * const p,

size_t const istart,size_t const iend)

{

int assert_dummy =(assert(!! p),assert(!! istart),assert(!! iend),0);



Unchecked code, may contain an error or two:

#include <stdlib.h>
#include <stddef.h>
#include <assert.h>

char *to_release;

void ReleaseLastString(void)
{
free(to_release);
}

char const *CreateSubstring(char const *const p,
size_t const istart,size_t const iend)
{
int assert_dummy = (assert(!!p),assert(!!istart),assert(!!iend),0);




为什么上面的行显示有双重感叹号?




Why are there double exclamation marks in the line show above?



这篇关于为字符串动态分配内存的一般方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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