当前位置: 动力学知识库 > 问答 > 编程问答 >

Pass a dynamic array from inside one function to other in C

问题描述:

If I created a dynamic array inside a function and insert values in it, and I want to use this array in other function, but I want its values not to be destroyed, should I declare it as a static and as an extern? If so how do I do this?

For example:

a.c

void func1(void)

{

char *filename;

file_name=(char *) malloc ((SIZE)*sizeof(char));

strcpy(file_name , other_file_name);

file_name[N-1] = '\0';

file_name[N-2] = 'x';

bla bla

}

b.c

void func2(void)

{

operations on file_name

}

my_main.c

#include <stdio.h>

MORE INCLUDES HERE

#include "a.h"

#include "b.h"

int main()

{

bla bla ...

return 0;

}

This is the purpose, but should I declare inside func1() static extern char *filename;?

And if it's the right way, what else should I do in order for it to work?

网友答案:

DO NOT NOT NOT declare that as an extern. Using a global variable in this context is very poor style. What you want to do is pass a pointer to the filename array as an argument to func2. When you call malloc, the OS allocates for you memory on the heap which is independent of your call stack. Therefore, even when func1 returns it is still there (until you call free).

for example

void func1(void)
{
char * filename;
filename = (char*)malloc((SIZE)*sizeof(char));
//do stuff
func2(filename);
}

void func2(char * filename)
{
//do stuff to filename
}
网友答案:

If you allocate memory via malloc you can simply return the pointer from your function. The allocated memory won't be released automatically. You have to use free to do that.

网友答案:

You can only use one storage class at a time - so you cannot use both static and extern to qualify a single variable.

With dynamically allocated arrays, it is crucial to know which code will release the allocated space. If you don't, you will have a memory leak. In small-scale programs, it may not 'matter' in the sense that the program will run despite leaking memory. However, in large programs, especially long-running programs (word processors, DBMS, etc), it matters crucially.

You can pass dynamically allocated arrays - or the pointer to the dynamically allocated array - to another function. If you do not want the other function to modify it, you should write the other function so it takes const SomeType *arg as the argument. The compiler will then ensure that your code does not modify the array.

Hence:

header.h

extern void func2(const char *filename);
extern void func1(void);

a.c

#include "header.h"
#include <stdlib.h>
#include <string.h>

extern const char *other_file_name; // Should be in a header!

void func1(void)
{
    char *filename;
    size_t N = strlen(other_file_name) + 1;
    filename = (char *)malloc(N);

    strcpy(filename, other_file_name); 
    file_name[N-1] = '\0'; 
    file_name[N-2] = 'x';

    func2(filename);
    free(filename);
} 

b.c

#include "header.h"

void func2(const char *filename)
{
    ...operations on filename...
}

main.c

#include "header.h"

int main(void)
{
    ...
    func1();
    ...
    func2("/etc/passwd");
    return 0;
}

Alternatively, but less desirably, you can make filename into a global variable. In that case, you should declare it in header.h. However, you cannot then have the compiler enforce the constraint that func2() should treat variable as a constant - one more reason not to use global variables.

See also SO 1433204 for a discussion of extern variables in C.

分享给朋友:
您可能感兴趣的文章:
随机阅读: