我编写了一个程序,它读取简单的数据声明并返回分配给该变量的内存量。
每个输入行应包含 - 一个类型名称,它必须是以下之一:char、int、short、long、float 或 double。 - 一个或多个单独的声明规范,以逗号分隔。 - 标记行尾的分号。
在我的代码中,我还添加了对文件的使用(见下文)。
我想问你如何使用 sizeof 代替我对要分配的数据类型的大小进行编码的方式。我也想问你我在文件中的使用是否正确。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void bytesPerValue(char str[], char* filename) ;
int theSizeOf(char *str);
int strToNumber(char *str);
void main()
{
char str[50];
gets(str);
bytesPerValue(str,"input.txt");
}
void bytesPerValue(char str[], char* filename)
{
int i = 0, j = 0;
int temp = 1;
int size;
char* tempChar = (char*)malloc((strlen(str))*sizeof(tempChar));
FILE *f=fopen(filename,"w");
if (f==NULL)
exit(1);
while (str[i]!=' ' || str[i]=='*') //checking the type of the variables//
{
tempChar[j] = str[i];
i++;
j++;
}
tempChar[j] = '\0';
size = theSizeOf(tempChar);
j = 0;
i++;
while (str[i] != ';')
{
if (isalpha(str[i]) || str[i]=='_') // for variables and arrays//
{
while (str[i] != ',' && str[i] != ';') //runs until ', ' or '; ' //
{
if (str[i]==' ')
{
while (str[i]==' ')
i++;
}
if (str[i] == '[') //checks if it is array//
{
printf("%c", str[i]);
i++;
while (str[i] != ']')
{
tempChar[j] = str[i]; //copies the value in the string//
i++;
j++;
}
tempChar[j] = '\0';
temp = strToNumber(tempChar); //converting to number in order to valuate the bytes//
}
printf("%c", str[i]);
i++;
if (isspace(str[i]))
{
while (isspace(str[i]))
i++;
}
}
fprintf(f," requires %d bytes \n", temp*(sizeof(temp)));
}
if (str[i] == '*') //for pointers//
{
while (str[i] != ',' && str[i] != ';')
{
printf("%c", str[i]);
i++;
if (str[i]==' ')
{
while (str[i]==' ')
i++;
}
}
fprintf(f," requires %d bytes \n", 4);
}
if (str[i] != ';')
i++;
}
fclose(f);
}
int theSizeOf(char* str) // checking the size of the variable
{
if (strcmp(str, "int")==0 || strcmp(str, "long")==0 || strcmp(str, "float")==0)
return 4;
else if (strcmp(str, "char")==0)
return 1;
else if (strcmp(str, "double")==0)
return 8;
else if (strcmp(str, "short")==0)
return 2;
else
return 0;
}
int strToNumber(char* str) //converting the string to number//
{
int temp=1;
int num=0;
int t;
int i;
int length = strlen(str);
for (i = length-1; i >= 0; i--)
{
t = str[i] - '0';
num += t * temp;
temp *= 10;
}
return num;
}
最佳答案
how can I use sizeof instead the way I coded the size of data types to be allocated?
size_t theSizeOf(const char* str) {
if (strcmp(str, "char" )==0) return sizeof(char);
if (strcmp(str, "short" )==0) return sizeof(short);
if (strcmp(str, "int" )==0) return sizeof(int);
if (strcmp(str, "long" )==0) return sizeof(long);
if (strcmp(str, "float" )==0) return sizeof(float);
if (strcmp(str, "double")==0) return sizeof(double);
return 0;
}
或者使用数组
size_t theSizeOf(const char* str) {
const struct {
const char *type;
size_t size;
} types[] = {
{"char", sizeof(char) },
{"int", sizeof(int) },
// Add others as needed.
{"double", sizeof(double) },
};
for (size_t i=0; i< sizeof types /sizeof types[0]; i++) {
if (strcmp(str, types[i].type)==0) return types[i].size;
}
return 0;
}
I also would like to ask you whether my use in file is right.
很多问题比如
// No need for cast
// buffer allocation 1 too short
// wrong sizeof argument
char* tempChar = (char*)malloc((strlen(str))*sizeof(tempChar));
// instead use
char* tempChar = malloc(strlen(str) + 1);
// potential infinite loop as code does not check for null character
while (str[i]!=' ' || str[i]=='*') {
...
// Does not handle negative numbers nor overflow of `temp`.
strToNumber()
关于c - 读取简单数据声明并响应分配给该变量的内存量的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44315983/