初学者qn..malloc [英] beginner qn..malloc

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

问题描述



i想创建一个字符串数组。我知道数组中包含的项目数量b / b,但每个字符串的大小不同

并且在编译时不会知道。


例如:在

char * mynames []

mynames [0] =" mathew"

mynames [1] =" gordon_brown"

mynames [2] =" F:\ gordon \ docs \ mycv.txt"


如何为这个阵列分配内存?我知道malloc()和

免费()需要使用但不确定,因为我只是一个初学者

thanx in adv

gordon

解决方案

nodrogbrown写道:


hi

i want创建一个字符串数组。我知道数组将包含的项目数量,但每个字符串的大小将不同

并且在编译时不会知道时间。


例如:在

char * mynames []


mynames [0] =" mathew" ;

mynames [1] =" gordon_brown"

mynames [2] =" F:\ gordon \ docs \ mycv.txt"


如何为这个阵列分配内存?我知道malloc()和

免费()需要使用但不确定,因为我只是一个初学者

thanx in adv

gordon



你可以使用循环:


for(ctr = 0; ctr< ARR_SIZE; ctr ++){

mynames [ctr] = malloc(elements);

if(!mynames [ctr]){

handle_error();

}

else {

elements = get_next_array_size();

}

}


我希望对象和例程名称是自描述的。


nodrogbrown写道:


hi

i想要创建一个字符串数组。我知道数组将包含的项目数量b / b $ b $每个字符串的大小将是是不同的

并且在编译时不会知道。


例如:在

char * mynames []


mynames [0] =" mathew"

mynames [1] =" gordon_brown"

mynames [2] =" F:\ gordon \ docs \ mycv.txt"


如何为这个阵列分配内存?我知道malloc()和

free()需要使用但不确定因为我只是初学者



类似:


#include< stdlib.h>


enum {NumberOfNames = 42};


int main(void)

{

char * mynames [NumberOfNames] = {NULL};


size_t stringSize = someValue;


mynames [0] = malloc(stringSize);


stringSize = someOtherValue;


mynames [1] = malloc(stringSize);


/ * do stuff * /


for(unsigned n = 0; n< NumberOfNames; free(mynames [n ++]));

}


-

Ian Collins。


nodrogbrown写道:


hi

i想创建一个字符串数组。我知道这个数字数组将包含的项目是否为
,但每个字符串的大小将不同

并且不会在编译时已知。


例如:在

char * mynames []


mynames [0] = " mathew"

mynames [1] =" gordon_brown"

mynames [2] =" F:\ gordon \ docs \ mycv.txt"


如何为这个阵列分配内存?我知道malloc()和

免费()需要使用但不确定,因为我只是一个初学者

thanx in adv

gordon



其他人给了你一种解决方案。但是,如果所有

你的字符串来自字符串文字,那么有一个更简单的解决方案:


char * mynames [] =

{" mathew"," gordon_brown"," F:\ gordon \ docs \ mycv.txt"};


您可能需要更多复杂的解决方案,但由于你是一个初学者,我不想错过更简单的

解决方案满足你需求的可能性。 />


hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
thanx in adv
gordon

解决方案

nodrogbrown wrote:

hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
thanx in adv
gordon

You can use a loop:

for (ctr = 0; ctr < ARR_SIZE; ctr++) {
mynames[ctr] = malloc(elements);
if (!mynames[ctr]) {
handle_error();
}
else {
elements = get_next_array_size();
}
}

I hope the object and routine names are self descriptive.


nodrogbrown wrote:

hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner

Something like:

#include <stdlib.h>

enum { NumberOfNames = 42 };

int main(void)
{
char* mynames[NumberOfNames] = {NULL};

size_t stringSize = someValue;

mynames[0] = malloc( stringSize );

stringSize = someOtherValue;

mynames[1] = malloc( stringSize );

/* do stuff */

for( unsigned n = 0; n < NumberOfNames; free( mynames[n++] ) );
}

--
Ian Collins.


nodrogbrown wrote:

hi
i want to create an array of strings .I know the number of items that
the array will contain but the size of each string will be different
and will not be known at compile time.

eg: in
char * mynames[]

mynames[0]="mathew"
mynames[1]="gordon_brown"
mynames[2]="F:\gordon\docs\mycv.txt"

how can i allocate the memory for this array? I know malloc() and
free() need to be used but not sure since i am only a beginner
thanx in adv
gordon

Other people have given you one kind of solution. However, if all of
your strings come from string literals, there''s a much simpler solution:

char * mynames[] =
{"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};

You probably need the more complicated solution, but since you''re a
beginner I didn''t want to miss out on the possibility that the simpler
solution would meet your needs.


这篇关于初学者qn..malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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