大家好.我正在做一个涉及动态内存分配,指针,类和异常的链表练习.有人愿意批评它并告诉我我做错了什么以及我应该在风格和上面列出的那些主题方面做得更好吗?
/*
Linked List exercise
*/
#include <iostream>
#include <exception>
#include <string>
using namespace std;
class node{
public:
node * next;
int * data;
node(const int i){
data = new int;
*data = i;
}
node& operator=(node n){
*data = *(n.data);
}
~node(){
delete data;
}
};
class linkedList{
public:
node * head;
node * tail;
int nodeCount;
linkedList(){
head = NULL;
tail = NULL;
}
~linkedList(){
while (head){
node* t = head->next;
delete head;
if (t) head = t; …Run Code Online (Sandbox Code Playgroud) 如果我int动态分配对象的内存位置,如下所示:
int *x = new int;
完成它之后,想要释放堆上的内存,我将执行以下操作:
delete x;
现在,如果我没有做到以下几点:
x = NULL;
会x指向另一个地址吗?更新: another而不是many
说我没做x = NULL,做了另一个 delete x;,会发生什么?
请告诉我为什么以下代码甚至在以下代码上运行strict C-99 compiler:
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
int a[n];
a[1]=10;
a[2]=5;
printf("%d %d",a[1],a[2]);
}
Run Code Online (Sandbox Code Playgroud)
变量声明必须在C中的任何其他语句之前发生吗?如果我们想要一个动态分配的数组,我们必须使用内存分配函数,malloc()但它是如何获取和输入整数并分配该大小的数组?
当我正在尝试学习C++时,我正在玩指针和动态内存,并且在编译时我不断收到此错误.
error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
int * ageP;
ageP = new (nothrow) int;
if (ageP == 0)
{
cout << "Error: memory could not be allocated";
}
else
{
cout<<"What is your age?"<<endl;
cin>> ageP; <--this is the error line
youDoneIt(ageP);
delete ageP;
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?在此先感谢您的帮助.
我终于决定继续学习C++,并在一本增加数组大小的书中遇到了下面的代码.该函数采用指向具有原始大小的数组的指针,并返回一个大小为double的新数组.
int *doubleArraySize(int *p_array, int *p_size) {
*p_size *= 2;
int *p_new_array = new int[*p_size];
for(int i = 0; i < *p_size; i++)
p_new_array[i] = p_array[i];
delete[] p_array;
return p_new_array;
}
Run Code Online (Sandbox Code Playgroud)
当我们到达for循环时,值*p_size已经加倍.这意味着(至少对我而言)当我们访问时,p_array[i]我们最终会进入不属于的内存区域p_array.这是一个问题吗?这个代码可以崩溃吗?如果没有,我错过了什么?
所以考虑到这个c结构:
typedef struct {
int* arr1;
int* arr2;
} myStruct;
Run Code Online (Sandbox Code Playgroud)
这个答案描述了使用单个malloc来同时分配a myStruct和它的数组:
myStruct* p = malloc(sizeof(*p) + 10 * sizeof(*p->arr1) + 10 * num * sizeof(*p->arr2);
if(p != NULL) {
p->arr1 = (int*)(p + 1);
p->arr2 = p->arr1 + 10;
}
Run Code Online (Sandbox Code Playgroud)
我想知道有没有类似的方法来做到这一点new?
显然,我希望能够分配到我在运行时收到的大小,就像使用C示例一样.
据我了解,delete[]用于释放分配的内存空间new.free()也可以用来释放那个内存空间.那么在释放内存free()代替时我将面临哪些类型的问题delete[]?
在C++中使用以下代码:
class Foo {
vector<Foo*> otherFoos;
};
int _tmain(int argc, _TCHAR* argv[])
{
Foo* data = new Foo[5];
delete data;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Visual Studio 2013.我不知道我的代码有什么问题.
在程序开始时,我需要为未知数量的字符串(大小未知)动态分配内存,以便以后使用。要获得用户的字符串数,我需要:
int main(int argc, char *argv[]){
int number = atoi(argv[1]);
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都很好。现在,“ number”保存着用户在命令行上输入的用于执行代码的数字。现在是我不太了解的部分。现在,我需要动态存储字符串的长度以及字符串的内容。例如,我希望程序运行如下:
Enter the length of string 1: 5
Please enter string 1: hello
Enter the length of string 2: ...
Run Code Online (Sandbox Code Playgroud)
为此,我认识到必须创建一个字符串数组。但是,我不太了解指针的概念,而没有。我想要的可能是如何简化此过程?
我一直在研究学校的一些练习题,其中之一是必须将输入的整数写到动态分配的字符串中。该代码可以很好地完成工作,直到释放分配的内存(发生堆损坏)为止。
有人可以解释为什么会发生这种情况以及我在做什么错吗?
int main() {
char *string = NULL;
char **string2 = &string;
Conversion(string2);
printf("Entered number converted to string: %s", string);
free(string);
return 0;
}
int Conversion(char **string) {
int num = 0, temp = 0, dcount = 0;
printf("Enter number: ");
scanf(" %d", &num);
temp = num;
while (temp != 0) {
temp /= 10;
dcount++;
}
*string = (char*)malloc(dcount*sizeof(char));
if (*string == NULL) {
printf("Error during memory allocation!");
return -1;
}
sprintf(*string, "%d", num);
return 0;
}
Run Code Online (Sandbox Code Playgroud) c string pointers segmentation-fault dynamic-memory-allocation
c++ ×7
c ×4
pointers ×3
arrays ×2
string ×2
free ×1
heap ×1
io ×1
linked-list ×1
malloc ×1
memory-leaks ×1
new-operator ×1
visual-c++ ×1