Tag: 动态

gcc链接选项-L:如何指定动态库路径的其他方法

如果我用“-L”编译我的源代码。 可以找到动态库libmd5.so。 gcc main.c -g -O2 -Wall -o main -L. -lmd5 -lcr 但是如果我离开“-L。” – 选项,链接器就找不到动态库。 如何在不调用“-L”的情况下进行更改? (附加信息libmd5.so和libmd5.so.1.0.1位于/ home / user / ba)

如何释放函数返回的指针?

#include #include #include char* f(void) { char *x; x = malloc(sizeof(char) * 4); strcpy(x, “abc”); return(x); } int main(void) { char *a; a = f(); printf(“%s”, a); free(a); return(0); } 是否必须释放函数中的变量x ? 如果是这样,当我需要退货时,这怎么可能呢?

连接2个矩阵

我有一个动态分配的矩阵,我想创建另一个,这是第一个矩阵,但旁边有另一个副本。 例如,我有矩阵: 11 22 我的新矩阵将是: 1 1 1 1 2 2 2 2 我该如何连接它们? 这是我在C中的代码: #include #include #include int **create_matrix(int row, int col) { int **matrix = malloc(sizeof (int*)*row); int i; for (i = 0; i < row; i++) { matrix[i] = malloc(sizeof (int)*col); } return matrix; } void matrix_input(int **matrix, int row, int col) […]

C动态字符串长度

在C中创建动态字符串有不同的方法(长度不断变化)。 经过一些谷歌搜索,这样做的主要方法是使用realloc() 。 我实现这一点的方法是使用每个节点具有32字节块的链表。 我想知道除了使用realloc()和链接列表之外是否有更好的解决方法,以及每种方法的缺点和优点。 编辑我这样做的原因是因为我从套接字recv()接收动态数据,并且正在寻找一种灵活的存储方式,而不需要分配大量不需要的数据。

具有C语言function的realloc结构

我的C程序崩溃了,我太新了,无法弄明白。 到目前为止它非常简单,我想代码足以弄清楚出了什么问题。 我只是想逐行读取文件。 一旦我内存不足,我会将结构的内存加倍。 如果这还不够,我会提供您需要的任何其他信息。 非常感谢您的帮助,因为我已经被困了好几个小时了。 /* John Maynard 1000916794 7/15/2013 HW-06 */ #include #include #include #define N 100 struct course { char subject[11]; int catalogNum; int sectionNum; int enrollmentTotal; int enrollmentCap; }; void readFile(struct course *d, char* filename); void double_array_size(struct course *d, int new_size); int main(void) { char *filename = “hw06-data.csv”; struct course *d; […]

C动态中的字符数组是?

我在C中编写了一个简单的程序。一个程序输入一个String并显示它和长度。 #include int main() { char a[4]; printf(“Enter the name : “); gets(a); printf(“\nThe name enterd is : %s”,a); printf(“\nLength of string is : %d”,strlen(a)); getch(); return 0; } 该程序不包含警告或错误。 在运行时,我输入值“ melwinsunny ”作为输入。 没有错误,显示的结果是: Enter the name : melwinsunny The name entered is : melwinsunny length of string is : 11 为什么会这样? 我已经声明了长度为4的字符数组( char a […]

动态内存分配

我在为数组动态分配内存时遇到了麻烦。 我已经调试了几个小时,没有任何指针? 我发布了剩下的代码。 它应该简单地将交换第一行与第二行交换,第三行与第四行交换。 我得到了奇怪的结果,如: 输入字符串:hello 输入字符串:你好吗? 输入字符串:我很好,谢谢 输入字符串:再见 输入字符串:bai 输入字符串:xx ========================= 你好吗 !我很好谢谢 你好 !你好吗 再见 !白 我很好谢谢 !再见 白 !XX int count = 0; char *lines[MAX_LINES]; char *tmp[50]; printf(“Enter string: “); fgets(tmp, 50, stdin); lines[count] = (char *) malloc((strlen(tmp)+1) * sizeof(char)); strcpy(lines[count], tmp); while(strcmp(“xx\n”, lines[count])){ count++; printf(“Enter string: “); fgets(tmp, 50, stdin); lines[count] […]

静态Vs动态库

我读过有关静态和动态库的内容。 我的问题很少说明 dlopen dlclose: Benifit of dlopen is we can start the EXE with out loading the necessary libraries at the begining. Only when we need we will load the libratries and unload it from the memory. 这是动态链接库的行为。 我的问题是,如果我链接库libUtlities ld -o EXE main.o -lUtilities 当我运行EXE时,在我第一次使用这些function之前,libUtlities将被加载到内存中 which i observed in dbx (Solaris debugger) But will not […]

在C中传递多维数组

我目前正在努力学习C,我遇到了一个我无法解决的问题。 考虑: #include #include #include #define ELEMENTS 5 void make(char **array, int *array_size) { int i; char *t = “Hello, World!”; array = malloc(ELEMENTS * sizeof(char *)); for (i = 0; i < ELEMENTS; ++i) { array[i] = malloc(strlen(t) + 1 * sizeof(char)); array[i] = strdup(t); } } int main(int argc, char **argv) { char […]

scanf动态分配

无论出于何种原因,以下代码打印(null): #include #include int main(void) { char *foo; scanf(“%ms”, &foo); printf(“%s”, foo); free(foo); } 我正在尝试动态地为字符串分配内存,但正如我之前所说,我的程序只是输出(null)。 我通过使用getche和realloc创建一个函数来解决这个问题,但是由于我还必须编写用户输入退格键,制表符等所会发生的事情,这似乎几乎毫无意义。但正如我所说的那只是一项工作周围,​​我宁愿知道为什么上面的代码不起作用… 附加信息: 我正在使用Pelles C IDE v7.00并使用C11标准进行编译