我一直在从事一些C项目,想知道是否创建一个自定义结构,例如,Student定义一个自定义结构类型的变量,并使用malloc为其分配内存,它是否也为变量的属性单独分配内存或者它们都保存在同一个空间中?如果是,如果我为每个属性单独使用 malloc 分配内存会有什么区别吗?
例如:
typedef struct {
unsigned int ID;
char *first_name;
char *last_name;
int num_grades;
float *grades;
unsigned short int days_absent;
char *memo;
} Student;
int main() {
// Declare students array
Student *students = NULL;
int students_size = 0;
// Allocate memory for students array
students = (Student *) malloc(sizeof(Student));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有个疑问。假设我有一个使用 malloc 分配的缓冲区,如下所示:
uint16_t buffer_length = 200; // !size is dynamic!, never static
uint8_t* buffer = (uint8_t*) malloc(200*sizeof(uint8_t));
// buffer then is populated somehow from 0 to 199
Run Code Online (Sandbox Code Playgroud)
现在我有一个 std::queue ,我需要 FIFO 几个分配的缓冲区,如下所示:
std::vector<uint8_t> buffer_vector(buffer, buffer + buffer_length);
std::queue<std::vector<uint8_t>> fifo_queue;
fifo_queue.push(buffer_vector);
Run Code Online (Sandbox Code Playgroud)
考虑到我决定从 uint8_t* 转移到 std::vector 因为我可以在单个元素中存储更多缓冲区信息(长度)。现在我想从队列中取出项目:
std::vector<uint8_t> taken_item = fifo_queue.front(); // reads the FIFO item
fifo_queue.pop(); // removes from FIFO
uint8_t* taken_item_buffer_ptr = taken_item.data(); // takes the buffer ptr
uint16_t taken_item_buffer_length = taken_item.capacity(); // takes the size
Run Code Online (Sandbox Code Playgroud)
现在我可以处理从 FIFO 返回的缓冲区,问题是,我是否必须释放返回的指针?例如如下: …
在我的程序中,我使用了malloc()和 指针,但我没有用来free()释放这些内存,我已经使用-fsanitize=address标志进行了编译,但它说没有内存泄漏。据我所知,如果我分配内存,我还必须在程序结束时释放内存,否则会出现内存泄漏。
#include <stdio.h>
#include <stdlib.h>
void print2D(int rowSize, int **cols_size, int **arr) {
for(int i = 0; i < rowSize; i++) {
for(int j = 0; j < (*cols_size)[i]; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int** make2D(int rowSize, int **colSize) {
int** arr = (int**) malloc(sizeof(int*) * rowSize);
for(int i=0; i < rowSize; i++) {
arr[i] = (int*) malloc(sizeof(int) * 2);
}
*colSize = (int*) malloc(sizeof(int*) * 2);
(*colSize)[0] = …Run Code Online (Sandbox Code Playgroud) 似乎不可能typedef在我的程序中使用和合并 malloc 函数:更具体地说:
组.h
#include "headers.h"
#include "info.h"
#include "sub_list.h"
typedef struct group_entity *group;
group new_group(int id);
void free_group(group group);
Run Code Online (Sandbox Code Playgroud)
组.c
#include "groups.h"
struct group_entity {
int gId;
info gFirst;
info gLast;
subinfo gSub;
};
group new_group(int id) {
group new_group = malloc(sizeof(group));
if (!new_group) {
return NULL;
}
new_group->gId = id;
new_group->gFirst = NULL;
new_group->gLast = NULL;
new_group->gSub = NULL;
return new_group;
}
void free_group(group group) {
free(group);
}
Run Code Online (Sandbox Code Playgroud)
头文件.h
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include …Run Code Online (Sandbox Code Playgroud) 我最近了解到,取消引用未与某个对象 ( ) 对齐的指针uint32_t* foo = (uint32_t*)7; *foo = 5;实际上是未定义的行为:
C11 第 6.2.8 节:对象对齐:
完整的对象类型具有对齐要求,这对可以分配该类型的对象的地址施加了限制。对齐是实现定义的整数值,表示可以分配给定对象的连续地址之间的字节数。对象类型对该类型的每个对象施加对齐要求:可以使用
_Alignas关键字请求更严格的对齐。
好的,非常有趣。但malloc似乎根本不关心对齐:
7.22.3.4
malloc函数概要
#include <stdlib.h>
void *malloc(size_t size);
描述该
malloc函数为大小由 size 指定且值不确定的对象分配空间。
退货该
malloc函数返回空指针或指向已分配空间的指针。
因此:执行以下操作是否不太可能会引发未定义的行为?
uint32_t* a = malloc(10*sizeof(uint32_t));
*a = 7;
Run Code Online (Sandbox Code Playgroud)
毕竟,我们无法保证 的返回值与malloc任何内容对齐。
我是 C 新手。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void demo() {
char* s = malloc(10);
strcpy(s, "foo");
free(s);
}
int main()
{
demo();
}
Run Code Online (Sandbox Code Playgroud)
这个程序会泄漏几个字节的内存吗?
据我所知,我正确使用 malloc 将字符串分配为字符指针。
\n char* test = (char*) malloc (sizeof (char) * 4);\n\n test[0] = 't';\n test[1] = 'e';\n test[2] = 's';\n test[3] = 't';\n\n std::cout << test << "\\n";\nRun Code Online (Sandbox Code Playgroud)\n当我打印字符串时,末尾有多余的 ASCII 字符。
\ntest\xc2\xb2\xc2\xb2\xc2\xb2\xc2\xb2\xc3\x9fE\xe2\x95\x9c\xce\xb5\xe2\x94\xac\xc3\xb4\nRun Code Online (Sandbox Code Playgroud)\n我的预感是“test”中的字符占用的内存小于我分配的最大内存。\n我看到的额外文本可能是多余的内存,尚未写入任何有意义的内容。不过,我不一定知道分配时的确切长度,因为该字符串将动态组装。
\n我的问题是如何重构它以减少多余的内容。如果我知道字符串的字符长度,我可以以某种方式截断最终产品吗?有没有更好的方法来分配我不知道的空间?有没有办法限制打印的字符数?
\n有人可以解释一下吗?
struct node
{
int data;
struct node * link;
}
main()
{
struct node *p, *list, *temp;
list = p = temp = NULL;
.........................
.........................
}
addbeg()
{
int x;
temp=malloc(sizeof(struct node));
scanf("%d", &x);
temp->data=x;
temp->link = list;
list=temp;
}
Run Code Online (Sandbox Code Playgroud)
这是一个通过C语言在链表中输入数据的代码.代码不完整,但我认为它足以达到目的.请解释基本上这些代码的编码:
temp=malloc(sizeof(struct node));
Run Code Online (Sandbox Code Playgroud)
和
temp->link = list;
list=temp;.
Run Code Online (Sandbox Code Playgroud) 我做了一个更大问题的小方案.我尝试做的是将一个字符串传递给一个函数,该函数将从中生成一个新的字符串.但是我遇到了一些问题.
我已将字符串定义为
typedef char string[1024];
Run Code Online (Sandbox Code Playgroud)
然后我有一个函数,它接受一个字符串,并创建一个新的字符串,填充旧的字符串和一个点字符
string* add_sth_to_string(char* msg){
string* newStr=malloc(sizeof(string)); // malloc a newStr
strcpy(*newStr, msg); // copying msg to newStr
char buff[1024]; // making a buffer
strcat(buff, "."); // adding a dot to buffer
strcat(buff, *newStr); // adding msg to buffer
strcpy(*newStr, buff); // copying buffer to newStr
return newStr;
}
Run Code Online (Sandbox Code Playgroud)
然后在main中我尝试每次使用此函数3次换一个新字符串:
for (i=0; i<3; i++){
string* newStr;
newStr=add_sth_to_string("test");
printf("str: %s\n", *newStr);
free(newStr);
// can even printf here
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的奇怪输出:
str: .test
str: .test.test
str: .test.test.test
Run Code Online (Sandbox Code Playgroud)
当我希望得到:
str: …Run Code Online (Sandbox Code Playgroud) 我正在编写一个"简单"代码来进行FFT.主要问题发生在DLpart部分:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <complex>
#include <algorithm>
#define SWAP(a,b) tempr=(a);(a) = (b); (b) = tempr
//although maybe i should make my own swap function rather than a define swap
using namespace std;
vector<double> bitReversal(vector<double> data, int nn,int* j);
vector<double> Xcreator(double xSteps);
vector< double > DLpart(vector<double> data,int nn,int j);
void arrayGuarder (vector<double>totals, string fileName,double xSteps);
vector<double> cosineCrafter(double xSteps,double numWaves);
main(int argc, char **argv){
vector<double> input;
int j = 1; …Run Code Online (Sandbox Code Playgroud)