Malloc的基本用法 [英] Basic Use of Malloc
问题描述
您好。我是一个不成熟的业余爱好者,我希望开始使用malloc,因为我已经告诉我,从可执行文件中运行可执行文件比管理内存使用的优雅方式更少。我需要基本的例子,所以我和
我的编译器可以用malloc来熟悉。请举例说明
不包含嵌套作业等,因为我会很容易丢失
。作为我的基本风格的一个例子,看看如何创建一个数组并指定一个指针:
unsigned long arraysize = 50;
char chArray [arraysize];
char * ptrChArray;
ptrChArray = chArray;
I需要一些malloc正确使用的例子,它们具有相同的基本风格。非常感谢您的参与。 kentinjapan
kent lavallie写道:
嗨。我是一个不成熟的业余爱好者,我希望开始使用malloc,因为我已经被告知从可执行文件中运行可执行文件比管理内存使用更不优雅。我需要基本的例子,所以我和
我的编译器可以与malloc一起使用。请举例说明
不包含嵌套作业等,因为我很容易迷路。作为我的基本风格的一个例子,看看我是如何创建一个数组并指定一个指针:
unsigned long arraysize = 50;
char chArray [arraysize] ;
char * ptrChArray;
ptrChArray = chArray;
我需要一些malloc正确使用的例子,它们具有相同的基本风格。非常感谢您的参与。 kentinjapan
此外,您可以尝试
http://www.c-for-dummies.com/lessons/chapter.17
当我刚接触C时,我发现_C for Dummies_材料很有用。
- 智能
" kent lavallie" <柯********** @ yahoo.ca>在消息中写道
news:d6 ************************** @ posting.google.c om ...嗨。我是一个不成熟的业余爱好者,我希望开始使用malloc,因为我已经被告知从可执行文件中运行可执行文件比管理内存使用更不优雅。我需要基本的例子,所以我和
我的编译器可以与malloc一起使用。请举例说明
不包含嵌套作业等,因为我很容易迷路。作为我的基本风格的一个例子,看看我是如何创建一个数组并指定一个指针:
unsigned long arraysize = 50;
char chArray [arraysize] ;
char * ptrChArray;
ptrChArray = chArray;
我需要一些malloc正确使用的例子,它们具有相同的基本风格。非常感谢您的参与。 kentinjapan
这是一个例子:
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
int main()
{
char * buffer;
char * string1 =" Hello";
buffer = malloc(30);
strcpy(buffer,string1);
strcat(缓冲区,World。);
puts(buffer);
返回0;
}
-
Jeff
2003年9月9日星期二20:56:56 -0700,kent lavallie写道:
我需要一些malloc正确使用的例子这是基本相同的基本风格。非常感谢您的参与。 kentinjapan
#include< stdlib.h>
#include< stdio.h>
int main()
{
/ *声明一个指针,TYPE可以是任何东西:int struct double what * /
TYPE *数组;
/ *分配内存以保存50个任何类型数组指向* /
array = malloc(50 * sizeof(* array) );
if(!array){
printf(无法分配内存\ n);
退出( 1);
}
do_something(数组);
/ *我们需要一个更大的数组* /
TYPE * tmp;
/ * Realloc分配一个新块,并复制内容
(尽可能* /
tmp = realloc(数组,70 * sizeof(*数组));
if(!tmp){
printf(" couldn''t reallocate memory\\\
;
退出(1);
}
array = tmp;
do_something_else(数组);
我们完成后
/ *释放内存* /
免费(数组);
返回0;
}
hth
-
NPV
" Linux是Lego,因为Windows是Fisher Price。 - J Frink医生
Hi. I am an amature c hobbyist and I wish to begin using malloc as I
have been told that running executables from executables is a less
than elegant way to manage memory use. I need basic examples so me and
my compiler can get familliar with malloc. Please give examples that
do not contain nested assignments and the like as I will get lost
quite easily. As an example of my rudimentary style take a look at how
I create an array and assign a pointer to it:
unsigned long arraysize = 50;
char chArray[arraysize];
char *ptrChArray;
ptrChArray = chArray;
I need a few examples of malloc''s proper use that are in the same
rudimentary style. Thank you so much for your time. kentinjapan
kent lavallie wrote:
Hi. I am an amature c hobbyist and I wish to begin using malloc as I
have been told that running executables from executables is a less
than elegant way to manage memory use. I need basic examples so me and
my compiler can get familliar with malloc. Please give examples that
do not contain nested assignments and the like as I will get lost
quite easily. As an example of my rudimentary style take a look at how
I create an array and assign a pointer to it:
unsigned long arraysize = 50;
char chArray[arraysize];
char *ptrChArray;
ptrChArray = chArray;
I need a few examples of malloc''s proper use that are in the same
rudimentary style. Thank you so much for your time. kentinjapan
Also, you might try
http://www.c-for-dummies.com/lessons/chapter.17
I found the _C for Dummies_ material helpful when I was new to C.
--Steve
"kent lavallie" <ke**********@yahoo.ca> wrote in message
news:d6**************************@posting.google.c om...Hi. I am an amature c hobbyist and I wish to begin using malloc as I
have been told that running executables from executables is a less
than elegant way to manage memory use. I need basic examples so me and
my compiler can get familliar with malloc. Please give examples that
do not contain nested assignments and the like as I will get lost
quite easily. As an example of my rudimentary style take a look at how
I create an array and assign a pointer to it:
unsigned long arraysize = 50;
char chArray[arraysize];
char *ptrChArray;
ptrChArray = chArray;
I need a few examples of malloc''s proper use that are in the same
rudimentary style. Thank you so much for your time. kentinjapan
This is an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *buffer;
char *string1 = "Hello ";
buffer = malloc(30);
strcpy(buffer, string1);
strcat(buffer, "World.");
puts(buffer);
return 0;
}
--
Jeff
On Tue, 09 Sep 2003 20:56:56 -0700, kent lavallie wrote:
I need a few examples of malloc''s proper use that are in the same
rudimentary style. Thank you so much for your time. kentinjapan
#include <stdlib.h>
#include <stdio.h>
int main()
{
/* Declare a pointer, TYPE can be anything: int struct double whatever */
TYPE *array;
/* Allocate memory to hold 50 of whatever the type array points to */
array = malloc(50 * sizeof(*array));
if (! array) {
printf("Couldn''t allocate memory\n");
exit(1);
}
do_something(array);
/* We need a bigger array */
TYPE *tmp;
/* Realloc allocates a new block, and copies the contents
(as far as possible */
tmp = realloc(array, 70 * sizeof(*array));
if (! tmp) {
printf("Couldn''t reallocate memory\n");
exit(1);
}
array = tmp;
do_something_else(array);
/* release the memory when we''re done */
free(array);
return 0;
}
hth
--
NPV
"Linux is to Lego as Windows is to Fisher Price." - Doctor J Frink
这篇关于Malloc的基本用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!