插入所有值后,我期望最终的 printf 语句给出方程的最终结果
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *carModel;
float tankRange;
float gasCost;
float costTank;
float gallonsTank; //gallons in full tank
float mpg; //miles per gallon
float costMile;
carModel = malloc(256);
printf("What is the model of car? " );
scanf("%255s", &carModel); // Don't read more than 255 chars
printf("How many miles can be driven on a full tank? " );
scanf("%f", &tankRange);
printf("What is the gas cost per gallon? " );
scanf("%f", &gasCost);
printf("How much does it …Run Code Online (Sandbox Code Playgroud) 我只是想知道为什么
students[x].firstName=(char*)malloc(sizeof(char*));
Run Code Online (Sandbox Code Playgroud)
这个不需要字符串的长度。
完整代码(来自互联网上的某个地方):
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
typedef struct
{
char* firstName;
char* lastName;
int rollNumber;
} STUDENT;
int numStudents=2;
int x;
STUDENT* students = (STUDENT *)malloc(numStudents * sizeof *students);
for (x = 0; x < numStudents; x++)
{
students[x].firstName=(char*)malloc(sizeof(char*));
printf("Enter first name :");
scanf("%s",students[x].firstName);
students[x].lastName=(char*)malloc(sizeof(char*));
printf("Enter last name :");
scanf("%s",students[x].lastName);
printf("Enter roll number :");
scanf("%d",&students[x].rollNumber);
}
for (x = 0; x < numStudents; x++)
printf("First Name: %s, Last Name: %s, Roll number: …Run Code Online (Sandbox Code Playgroud) 与此相关的问题: 如何读取未知长度的输入字符串?
几个问题:
这条线if(!str)return str是什么意思?
为什么是int ch整数?它不应该是一个炭?
size完全给定函数的目的是什么?
问一个单独的问题因为我理解了这个方法,而不是引擎盖下的一些东西.
现在,当我在块数组中打印每个元素时,每个元素都具有相同的地址.例如:
ints: 20 bytes stored at 0xbffa84fc
doubles: 80 bytes stored at 0xbffa84fc
chars: 8 bytes stored at 0xbffa84fc
Students: 1008 bytes stored at 0xbffa84fc
Run Code Online (Sandbox Code Playgroud) 我有一个char***,我动态地为它分配了一些内存.但是,当我尝试为其分配一些值时,我会遇到分段错误.我使用的尺寸不是太大.它在我创建char [768] [1024] [3]时有效但在我用相同的精确值动态添加它时不起作用.这是我的代码片段:
pic = new char**[height];
for(int i = 0; i < height; i++)
{
pic[i] == new char*[width];
for(int j = 0; j< width; j++)
{
pic[i][j] == new char[3];
}
}
pic[0][0][0] = 'a';//seg fault here
exit(1);
Run Code Online (Sandbox Code Playgroud) c++ segmentation-fault multidimensional-array dynamic-memory-allocation
首先请允许我为格式化和困难的代码道歉.我是C和Stack的新手.这里的大多数混乱代码可能与问题无关,但必须包含在上下文中.
在第一次调用realloc之后,下面的代码会遇到分段错误(在注释中注明).return_file->target_line只是一个3D数组,i是3D数组第一维的元素计数.所以我在它上面调用realloc来存储额外的2D数组(类型char **).
NULL 故意省略了内存分配的返回b/c开发协议明确规定所有内存分配都会成功(我对此有疑问).
我正在使用自己的内存检查程序.我得到的错误代码是:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ac63fb in reallochook () from /lib64/libc.so.6
Run Code Online (Sandbox Code Playgroud)
我已经看了很长时间,但似乎无法找到问题所在.
Mockfile *read_mockfile(const char filename[]) {
Mockfile *return_file = NULL;
FILE *input;
if(filename != NULL && (input = fopen(filename, "r")) != NULL) {
char **split_tmp, line[MAX] = {0};
return_file = malloc(sizeof(Mockfile));
return_file->rule_count = 0;
/*read lines*/
while(fgets(line, MAX, input) != NULL){
if(line[0] != '#' && line[0] != '\n'){
int j, i = return_file->rule_count;
split_tmp = split(line); …Run Code Online (Sandbox Code Playgroud) 在C++中,如果我有一个动态分配的基本类型数组,是否有必要使用delete []来防止内存泄漏?例如,
char * x = new char[100];
delete x; // is it required to call delete [] x?
struct A {
...
};
A *p = new A[30];
delete [] p; // delete p would cause memory leakage
Run Code Online (Sandbox Code Playgroud)
请评论.
c++ memory-leaks new-operator dynamic-memory-allocation delete-operator
为什么C++中的new []操作符实际上创建了一个长度为+ 1的数组?例如,请参阅以下代码:
#include <iostream>
int main()
{
std::cout << "Enter a positive integer: ";
int length;
std::cin >> length;
int *array = new int[length]; // use array new. Note that length does not need to be constant!
//int *array;
std::cout << "I just allocated an array of integers of length " << length << '\n';
for (int n = 0; n<=length+1; n++)
{
array[n] = 1; // set element n to value 1
}
std::cout << "array[0] " << …Run Code Online (Sandbox Code Playgroud) 那里!我是c ++的新手,遇到了删除指向数组的指针的问题.如果你搜索"如何正确删除指向数组的指针",之前有一个类似的问题.结论是,保持使用new和使用的习惯是一个好习惯delete.如果您使用new <data-type> []分配momery,delete[]应该遵循.
我对这个答案不满意.计算机在执行delete或delete[]执行时会做什么?所以,我试过这个.
#include <iostream>
using namespace std;
int main()
{
int* pvalue = NULL;
pvalue = new int[3];
cout << "address of pvalue:" << pvalue << endl;
for (int i = 0; i < 3; ++i)
{
*(pvalue+i) = i+1;
}
for (int i = 0; i < 3; ++i)
{
cout << "Value of pvalue : " << *(pvalue+i) << endl;
}
delete pvalue; …Run Code Online (Sandbox Code Playgroud) 我无法解决为什么new只能通过指针访问分配的堆栈中的内存,而堆中的内存(静态分配)可以正常访问.
是否与堆中的几乎所有内存都有某种顺序并且堆栈中的内存有点随机这一事实有关?(如果我刚才所说的是真的.)
动态记忆对我来说似乎是如此模糊和神秘,所以任何能够帮助我更好地理解它的人都会非常感激.
c ×5
c++ ×5
new-operator ×3
pointers ×2
c++11 ×1
malloc ×1
memory-leaks ×1
printf ×1
scanf ×1
string ×1