#include <iostream>
using namespace std;
void arrSelectSort(int *[], int), showArrPtr(int *, int);
void showArray(int [] , int);
int main()
{
int numDonations;
int *arrPtr;
cout << "What was the number of donations?: ";
cin >> numDonations;
arrPtr = new int[numDonations];
cout << "What were the donations values?: ";
for (int count = 0; count < numDonations; count++)
cin >> arrPtr[count];
arrSelectSort(arrPtr, 3);
cout << "The donations, sorted in ascending order are: \n";
showArrPtr(arrPtr, 3);
cout << "The donations, in their …Run Code Online (Sandbox Code Playgroud) 该程序是学生数据库.这部分代码存储了学生专业(计算机科学与信息技术).我无法弄清楚代码的作用.我从未在动态内存分配中遇到过这种格式.好的,第一行是指向数组的指针吗?那么第二行的指针呢?
string ** major;
major = new string*[3];
for(int i = 0; i < 3; i++)
major[i] = new string[2];
major[0][0] = "IT";
major[0][1] = "Information Technology";
major[1][0] = "CS";
major[1][1] = "Computer Science";
Run Code Online (Sandbox Code Playgroud) 我有这个代码
int e;
scanf("%d",&e);
int ar[e];
Run Code Online (Sandbox Code Playgroud)
这是动态分配吗?它看起来真的像我可以在运行时分配内存.
我用它来获取用户输入的元素数量,然后由用户再次填充.
嗨所以我正在创建一个程序,它使用哈希来存储文本文件中的单词及其出现次数.这按预期工作.我遇到的问题来自于释放分配的内存.
这是我的哈希
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include"hash.h"
/*
struct listnode{
char * word;
int count;
};
*/
void hashCreate(struct listnode * hashTable[], int size){
int i;
for(i=0;i<size;i++){
hashTable[i]=(struct listnode *)malloc(sizeof(struct listnode));
hashTable[i]->count=0;
}
}
int hash(char * data, int size) {
unsigned long hash = 5381;
char * p;
for (p = data; *p != '\0'; ++p) {
hash = (hash * 33) + *p;
}
return (int)(hash % size);
}
void hashAdd(char * data, struct listnode * hashTable[], int …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) 释放后指针的值是否为NULL?
int* p = malloc(sizeof(*p));
free(p);
if(p==NULL)
printf("Null\n");
else
printf("Not null\n");
Run Code Online (Sandbox Code Playgroud)
输出:
Not null
Run Code Online (Sandbox Code Playgroud)
好吧,我假设没有;
无论如何,我今天早些时候提出了一个问题:
在这里查看: C - 如何释放动态分配的内存?
List* head1 = NULL;
insertFront(&head1, 1);
insertFront(&head1, 2);
print(head1);
while (head1)
{
List *temp = head1;
head1 = head1->next;
free(temp);
}
if(head1 == NULL)
printf("Null\n");
else
printf("Not null\n");
Run Code Online (Sandbox Code Playgroud)
这种情况下的输出:
Null
Run Code Online (Sandbox Code Playgroud)
在这种情况下,在释放head1(节点也)后,head1变为null,不是吗?
最后,我错过了一些概念吗?
head1为null,但p不是.
我的问题是:
为什么head1和p之间的值不同?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main(int argc,char **argv)
{
FILE *fp;
char lineBuf[100],**p=NULL,temp[100];
int cnt,j,i;
if(argc!=2)
{
puts("ERROR:Invalid no.of arguments");
return;
}
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("'%s' file doesn't exist\n",argv[1]);
}
cnt=0;
while(fgets(lineBuf,100,fp)) //...........loop1
{
cnt++;
p=realloc(p,sizeof(char*)*(cnt));
if(p==NULL)
{
puts("memory unavailable");
return;
}
p[cnt-1]=calloc(1,strlen(lineBuf));
strcpy(p[cnt-1],lineBuf); //................statement1
}
fclose(fp);
for(i=0;i<cnt;i++)
{
for(j=i+1;j<cnt;j++)
{
if(strcmp(p[i],p[j])>0)
{
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
fp=fopen(argv[1],"w");
for(i=0;i<cnt;i++)
fputs(p[i],fp);
fclose(fp);
puts("sorting done");
}
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main(int argc,char **argv)
{
FILE …Run Code Online (Sandbox Code Playgroud) 我正在运行以下代码,其中我声明了一个动态2D数组,然后继续在列索引处分配值高于实际为动态数组分配的数字列.但是,当我这样做时,代码运行完美,我没有得到错误,我相信我应该得到.
void main(){
unsigned char **bitarray = NULL;
bitarray = new unsigned char*[96];
for (int j = 0; j < 96; j++)
{
bitarray[j] = new unsigned char[56];
if (bitarray[j] == NULL)
{
cout << "Memory could not be allocated for 2D Array.";
return;// return if memory not allocated
}
}
bitarray[0][64] = '1';
bitarray[10][64] = '1';
cout << bitarray[0][64] << " " << bitarray[10][64];
getch();
return;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出的链接在这里(值实际上是准确分配的,但不知道为什么).
c++ pointers undefined-behavior dynamic-memory-allocation visual-c++
我有两个问题.
freeC 中的函数如何工作?这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int *p;
p = (int *)malloc(sizeof(int));
*p = 10;
int *q = p;
printf("p:%d\n", p);
printf("q:%d\n", q);
printf("*p:%d\n", *p);
printf("*q:%d\n", *q);
free(p);
printf("Memory freed.\n");
p = (int *)malloc(sizeof(int));
*p = 19;
printf("p:%d\n", p);
printf("q:%d\n", q);
printf("*p:%d\n", *p);
printf("*q:%d\n", *q);
}
Run Code Online (Sandbox Code Playgroud)
为什么输出是这样的?
p:2067804800
q:2067804800
*p:10
*q:10
Memory freed.
p:2067804800
q:2067804800
*p:19
*q:19
Run Code Online (Sandbox Code Playgroud) 我正在努力写一个char *作为参数传递。我想从函数write_char()向char *写一些字符串。使用以下代码,我遇到了分段错误。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void write_char(char* c){
c = (char*)malloc(11*(sizeof(char)));
c = "some string";
}
int main(){
char* test_char;
write_char(test_char);
printf("%s", test_char);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 查看C和C++代码中的用户输入(for循环内部).我们在C++的用户输入中使用*(p + i),在C中使用(p + i).什么原因在C中缺少*?Plz解释!看一下COMMENT行...里面的for循环
#include <iostream>
using namespace std;
int main()
{
int n,i;
cout << "Ent size" << endl;
cin>>n;
int *p = new int [n];
for(i=0;i<n;i++)
cin>>*(p+i);//LOOK AT THIS LINE
cout<<"------------\n\n";
for(i=0;i<n;i++)
cout<<*(p+i)<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Ent size!\n");
scanf("%d",&n);
int *p=(int *)malloc(n*sizeof(int));
int i;
for(i=0;i<n;i++)
scanf("%d",(p+i));//LOOK AT THIS LINE
printf("-------\n\n");
for(i=0;i<n;i++)
printf("%d\n",(p+i));
return 0;
}
Run Code Online (Sandbox Code Playgroud)