当它们在内存中表示时,C++对象是否与C结构相同?
例如,使用C,我可以这样做:
struct myObj {
int myInt;
char myVarChar;
};
int main() {
myObj * testObj = (myObj *) malloc(sizeof(int)+5);
testObj->myInt = 3;
strcpy((char*)&testObj->myVarChar, "test");
printf("String: %s", (char *) &testObj->myVarChar);
}
Run Code Online (Sandbox Code Playgroud)
我认为C++不允许+为内置char *类型重载运算符.
所以我想创建自己的轻量级字符串类,它没有额外的开销std::string.我认为std::string是连续的代表:
(int)length, (char[])data
Run Code Online (Sandbox Code Playgroud)
我想要完全相同的功能,但没有前缀长度(节省8字节开销).
这是我用来测试的代码,但它导致了段错误
#include <iostream>
using namespace std;
class pString {
public:
char c;
pString * pString::operator=(const char *);
};
pString * pString::operator=(const char * buff) {
cout << "Address of this: " << (uint32_t) this << …Run Code Online (Sandbox Code Playgroud) 我试图实现一个dinamically增加数组realloc.我创建数组malloc,然后调用我的add函数,这将数组大小增加1.这是代码:
#include <stdio.h>
#include <stdlib.h>
int *foo;
int quantity;
void add(int number) {
foo = (int*) realloc(foo, sizeof(foo) + sizeof(int));
foo[quantity] = number;
quantity++;
}
void debugFoo() {
for (int i = 0; i < quantity; i++) {
printf("foo[%i] = %i\n", i, foo[i]);
}
printf("\n");
}
int main() {
quantity = 3;
foo = (int*) malloc(quantity * sizeof(int));
foo[0] = 1;
foo[1] = 2;
foo[2] = 3;
debugFoo();
add(20);
debugFoo();
add(2);
debugFoo();
return 0; …Run Code Online (Sandbox Code Playgroud) 关于我的代码,下面有一些混淆.
我使用malloc()分配内存,释放它,然后使用与参数相同的指针ptr调用realloc(),但不要使用realloc的返回地址.
此代码编译并运行精细打印预期的字符串.我没想到它因为ptr之前被释放了.
但是,如果我将realloc()的返回地址分配给ptr而不是注释掉,那么当然会出现运行时错误,因为ptr将为NULL.
如果我只是将之前释放的ptr作为参数传递,为什么它可以工作.它显然再次分配内存?提前致谢.
注意:此代码是故意编写的,目的只是为了尝试了解后台发生的情况.当然不建议取消引用悬空指针等.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* ptr;
ptr = (char*)malloc(20);
strcpy(ptr, "Hello");
printf("Address before free : %d\n", ptr);
printf("%s\n", ptr);
free (ptr);
printf("Address after free : %d\n", ptr);
printf("%s\n", ptr);
realloc(ptr, 30);
//ptr = realloc(ptr, 30); // causes runtime problem as expected
strcpy(ptr, "Hello");
printf("Address after realloc : %d\n", ptr);
printf("%s\n", ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 据我所知,没有为没有确切的替代realloc的C在C++喜欢new的malloc.但是,当我使用realloc in C++来改变new运算符分配的内存时,它工作正常.
像我在下面的代码中那样使用这两个(new和realloc)是否安全,或者它可能导致一些问题?
#include <iostream>
#include <cstdlib>
int main()
{
int size = 5;
int * arr = new int[size]{ 1,3,5,7,9 };
for (int i = 0; i < size; i++)
std::cout << *(arr + i) << (i < size - 1 ? ' ' : '\n');
size++;
arr = (int*)realloc(arr, size * sizeof(int));
*(arr + size - 1) = 11; …Run Code Online (Sandbox Code Playgroud) 看看我用这个简单的代码找到了什么:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *string;
int main(){
string = (char *) malloc(50*sizeof(char));
strcpy(string, "initi1l wording cont2ining forty-nine ch3r4cters.");
printf("BEFORE: %s\n", string);
string = (char *) realloc(string, 24*sizeof(char));
printf("AFTER: %s\n", string);
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
出口是:
BEFORE: initi1l wording cont2ining forty-nine ch3r4cters.
AFTER: initi1l wording cont2inia
Run Code Online (Sandbox Code Playgroud)
请注意字符串末尾的'a'!我不知道它来自哪里,也许在堆中的某个地方.它不是来自原始数据块.最初我使用realloc()与结构数组,它显然以更重要的方式破坏数据.
我该如何解决这个问题?
我是C的初学者.我在想,这是怎么回事malloc.这是一个示例代码,我在试图理解它的工作时写了.
码:
#include<stdio.h>
#include<stdlib.h>
int main() {
int i;
int *array = malloc(sizeof *array);
for (i = 0; i < 5; i++) {
array[i] = i+1;
}
printf("\nArray is: \n");
for (i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
free(array);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
Array is:
1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,我只为1个元素分配了空间,但是数组现在包含5个元素.因此,当程序顺利运行而没有任何错误时,目的是什么realloc().
谁有人解释原因?
提前致谢.
#include <stdio.h>
#include <stdlib.h>
int main() {
system("clear");
int *pt = malloc(2 * sizeof *pt);
int *tmp = NULL;
int i;
pt[0] = 44;
pt[1] = 9;
printf("pt[0] : %d\n", pt[0]);
printf("pt[1] : %d\n", pt[1]);
tmp = realloc(pt, 3 * sizeof *pt);
if (!tmp) {
printf("merde alors\n");
} else {
pt = tmp;
for (i = 0; i < 5; i++) {
pt[i] = i + 1;
printf("pt[%d] : %d\n", i, pt[i]);
}
}
//the compiler should give me an …Run Code Online (Sandbox Code Playgroud) 我希望能够让用户输入他们想要做的事情.还有其他选择,但现在我正在研究"插入".另外两个选项是"搜索"和"删除".
int main() {
char *input = malloc(sizeof(char) * 6);
printf("%s", "WELCOME TO USER'S SKIP LIST!\n\nWhat do you wish to do? ");
scanf("%6s", input);
if (strcmp(input, "insert") == 0) {
printf("%s", "What is the item? ");
input = realloc(input, (size_t)scanf("%s", input));
}
}
Run Code Online (Sandbox Code Playgroud)
我最初分配足够的内存,input有6个字符.这是因为其他两个选项也只有6个字符.进入后,他们insert必须输入一个具有任意数量字符的项目,因此我想input根据他们为项目输入的内容重新分配内存.因此,如果他们进入Nintendo Switch,将重新分配15个字符.我的执行realloc()方式是否正确?