是否可以定义char可变长度的 a ?
我有一个长度为 25的char“名称”(struct命名“人”的成员),但我希望它是值 1 和 25 之间的可变长度,因为我想生成char具有不同大小的随机字符串而不是始终具有相同的长度 (25)。该方法的参数之一是sizeof(n.name)。
注:n是一个struct(struct person n)。
在struct“人”的定义是这样的:
struct person{
int c;
char name[25];
};
Run Code Online (Sandbox Code Playgroud)
任何人?
c struct char dynamic-memory-allocation variable-length-array
我有一个指向字符串的指针数组。
char **array;
Run Code Online (Sandbox Code Playgroud)
我以这种方式声明它而不是 char *array[N] 因为这个数组不会有静态数量的元素。
以这种方式声明数组,每次添加新元素(指向字符串的指针)时,我可能都必须重新分配它的大小。
int main(void)
{
char **array;
char *word = "lolol";
char *word2 = "blabla";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你能给我一个关于我应该如何在数组中“创建空间”以存储指向这些字符串的指针的例子吗?
我正在尝试调整字符数组的大小,我遵循了: 在运行时调整字符 [] 的大小
然后:
我做过这样的事情:
// this crashes in runtime:
const long SIZE_X = 1048576;
char* Buffsz = new char(sizeof(char));
for(int i = 0; i < (SIZE_X - 2); i++)
{
Buffsz[i] = 'a';
if(realloc(Buffsz, sizeof(char) * i) == NULL) // autoallocate memory
cout << "Failled to reallocate memory!" << endl;
}
Run Code Online (Sandbox Code Playgroud)
但如果我这样做:
// this works without problems.
const long SIZE_X = 1048576;
char* ABuffsz = new char[SIZE_X];
for(int i = 0; i < (SIZE_X - 2); i++)
{ …Run Code Online (Sandbox Code Playgroud) 我正在实现一个堆栈分配器并重载operator new[]某个类以使用我自己的分配器。然后我注意到operator new[]为分配的数组中的元素数量分配了内存。
例如:
test_class* test_arr = new test_class[5];
从我的分配器请求 8 个字节 + 5*sizeof(test_class) 并且在前 8 个字节中它存储数组的大小,在这种情况下为 5。
为什么这样做?跟踪分配的内存量是我的分配器的工作。对于那部分,它真的没有意义,是吗?那么有什么意义呢?另外,我可以(还是不应该?)我以某种方式“关闭它”?
我的问题是关于 C 中的动态内存分配。我被要求动态分配一个nlong数组,并返回指向该数组第一个元素的指针。我有一些代码来测试这个输出,但内存分配失败。
long* make_long_array(long n)
{
int i;
int *a;
a = (int*)malloc(sizeof(int)*n);
if (a == NULL) {
printf("ERROR: Out of memory\n");
return 1;
}
for (i = 0; i < n; *(a + i++) = 0);
return *a;
}
Run Code Online (Sandbox Code Playgroud)
我在两行上收到一个错误说
'错误:返回使指针来自整数而不进行强制转换'
这发生在线路上
return 1;
Run Code Online (Sandbox Code Playgroud)
和
return *a;
Run Code Online (Sandbox Code Playgroud)
我不完全确定如何解决这个问题。我认为错误return 1;在于我在寻找指针时试图返回一个整数?但我不确定如何修复它以返回指针。任何帮助将非常感激。
考虑以下代码片段:
void foo() {
int arraySize = getMyComputedValue();
int dynamicArray[arraySize];
fillDynamicArray(&dynamicArray[0]);
for (int i = 0; i < arraySize; i++) {
// Do something with data
}
}
void bar() {
int arraySize = getMyComputedValue();
int* dynamicArray = new int[arraySize];
fillDynamicArray(dynamicArray);
for (int i = 0; i < arraySize; i++) {
// Do something with data
}
delete[] dynamicArray;
dynamicArray = NULL;
}
Run Code Online (Sandbox Code Playgroud)
两者都在内存中创建一个包含整数的不同长度的动态区域。我发现第一个示例foo()(至少在我的构建环境中)只能使用 GCC 进行编译。
两者之间的确切区别是什么?第一个示例仅仅是一个 GNU 扩展,它是底部示例的简写,允许编译器确定取消分配的正确时间是什么时候?或者第一个例子是否完全按照代码说的那样在堆栈上分配内存?
如我所见,new在免费存储中分配一个未命名的对象,并返回一个指向该未命名对象的指针。
因此,我们可以创建对该对象的引用:
int* pi = new int(100);
int& ri = *pi;
Run Code Online (Sandbox Code Playgroud)
我感兴趣的是,我可以在免费存储区上创建一个对象,而无需将return分配给new指针,而可以通过将对de的引用值分配给引用来对未命名对象new的引用:
int& x = *(new int(7));
cout << x << endl; // 7
x += 34;
cout << x << endl; // 41
Run Code Online (Sandbox Code Playgroud)
我想知道我是否可以这样做,还是一个坏主意?
注意:在生产中,我尽量避免处理原始内存,而是使用智能指针和STL容器。
假设我们知道我们将需要的向量的大小(比如“n”)。
使用是否vector<int> Array(n);比Array.push_back(element)一一使用有任何改进?
推荐哪个,为什么?
我刚刚开始使用 C++ 的标准库,我开始的第一件事是std::vector. 我对capacity()向量中的 有点困惑。我知道在每个 之后push_back(),capacity向量的 以指数幂变化,但在下面的输出中capacity,即使在插入之后,有时也会保持相同的值。有人可以向我解释内部工作吗?
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> v;
int capacity=v.capacity();
cout<<"Capacity before push_back(): "<<capacity<<endl;
for(int i=0;i<10;i++){
v.push_back(i);
cout<<"Capacity: "<<v.capacity()<<endl;
}
for(auto j=v.begin();j!=v.end();j++){
cout<<*j<<endl;
}
cout<<"Size of vector: "<<v.size()<<endl;
cout<<"Final Capacity of vector: "<<v.capacity()<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Capacity before push_back(): 0
Capacity: 1
Capacity: 2
Capacity: 4
Capacity: 4
Capacity: 8
Capacity: 8
Capacity: 8
Capacity: 8
Capacity: 16
Capacity: 16
0 …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
size_t sz = atol(argv[1]);
char *arr = malloc(sz);
sleep(10);
}
Run Code Online (Sandbox Code Playgroud)
我编译了这段代码并尝试运行它,用于pmap查看程序的内存映射。
当我使用一些大数字时1024000,我会得到这样的映射:
3901: ./alloc_program 1024000
0000560192f43000 4K r---- alloc_program
0000560192f44000 4K r-x-- alloc_program
0000560192f45000 4K r---- alloc_program
0000560192f46000 4K r---- alloc_program
0000560192f47000 4K rw--- alloc_program
0000560192fac000 132K rw--- [ anon ]
00007f75b69e9000 1004K rw--- [ anon ] <---- I believe this is the allocated memory
00007f75b6ae4000 148K r---- libc-2.31.so
00007f75b6b09000 1504K r-x-- libc-2.31.so
00007f75b6c81000 …Run Code Online (Sandbox Code Playgroud)