- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我从 valgrind 收到一个段错误,我不确定错误的含义或如何实际修复它。我希望对错误和可能的修复进行一些澄清。这是错误:我
==8063== 命令:./main stock.dat coins.dat==8063==
==8063== 大小 1 的无效读取
==8063== 在 0x4C2E1C7:strcpy(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so 中)
==8063== by 0x401046: setupNode (in/home/joshua/Documents/Assignment 2/main)
==8063== by 0x400E78: main (in/home/joshua/Documents/Assignment 2/main)
==8063== 地址 0x0 未被堆栈、malloc 或(最近)释放
==8063==
==8063==
==8063== 进程以信号 11 (SIGSEGV) 的默认操作终止
==8063== 访问不在地址 0x0 的映射区域内
==8063== 在 0x4C2E1C7:strcpy(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so 中)
==8063== by 0x401046: setupNode (in/home/joshua/Documents/Assignment 2/main)
==8063== by 0x400E78: main (in/home/joshua/Documents/Assignment 2/main)
==8063== 错误摘要:来自 1 个上下文的 1 个错误(抑制:0 来自 0)
#include "vm_menu.h"
#include "vm_type.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define NUMARGS 3
void addNodeBottom(struct stock_item *val, struct stock_item *head);
struct stock_item* setupNode(char* line);
char* openFile(char fileName[]);
int main(int argc, char * argv[])
{
struct vm vm;
struct menu_item menu_items[NUM_MENU_ITEMS];
/* The UNUSED() function is designed to prevent warnings while your
* code is only partially complete. Delete these 4 function calls once
* you are using the data structures declared above in your own code */
UNUSED(argc);
UNUSED(argv);
UNUSED(vm);
UNUSED(menu_items);
//creates a the first element for the stock_items type in the data structure.
struct stock_item *root;
struct stock_item *item;
item = (struct stock_item *) malloc( sizeof(struct stock_item) );
/* Now rootNode points to a node struct */
root = (struct stock_item *) malloc( sizeof(struct stock_item) );
/* List of things to do in this function: */
/* check command line arguments */
if(argc!=3){
printf("insuffcient arguments \n");
return EXIT_SUCCESS;
}
/*open stock file*/
char* fileName = argv[1];
FILE *file;
file = fopen(fileName,"r+");
/*read file*/
long lSize;
char *buffer;
fseek( file , 0L , SEEK_END);
lSize = ftell( file);
rewind( file );
/* allocate memory for entire content */
buffer = calloc( 1, lSize+1 );
if( !buffer ) fclose(file),fputs("memory alloc fails",stderr),exit(1);
/* copy the file into the buffer */
if( 1!=fread( buffer , lSize, 1 , file) )
fclose(file),free(buffer),fputs("entire read fails",stderr),exit(1);
fclose(file);
/*parse the file using | as the delmeter case*/
//declare all variables used to store the string in.
int counter = 0;
const char newLine[2] = "\n";
char *saveptr;
char *line;
int size;
//parse the id and set it first.
size = sizeof(strtok(buffer,newLine)); // allocated memory to store the id in.
line = malloc(size); //allocates the memory
int active = 0;
line = strtok_r(buffer,newLine, &saveptr); // copies the first line from the text file into a char array. .
while (!active){
if (line == NULL) {
printf("\n%s", "Reached end of file while parsing.");
return(0);
}
printf("%s",line);
item = setupNode(line);
line = strtok_r(NULL,newLine, &saveptr);
addNodeBottom(item, root);
// free(item);
}
return 0;
}
char* openFile(char fileName[]){
FILE *file;
file = fopen(fileName,"r+");
/*read file*/
long lSize;
char *buffer;
fseek( file , 0L , SEEK_END);
lSize = ftell( file);
rewind( file );
/* allocate memory for entire content */
buffer = calloc( 1, lSize+1 );
if( !buffer ) fclose(file),fputs("memory alloc fails",stderr),exit(1);
/* copy the file into the buffer */
if( 1!=fread( buffer , lSize, 1 , file) )
fclose(file),free(buffer),fputs("entire read fails",stderr),exit(1);
//printf("%s", buffer);
// printf("%s",content);
free(buffer);
fclose(file);
return buffer;
}
struct stock_item* setupNode(char* line){
struct stock_item *roots = {NULL};
roots = (struct stock_item *) malloc( sizeof(struct stock_item)+1 );
char *ptr;
char *prices;
const char del[2] = "|";
const char delm[2] = ".";
strcpy(roots->id, strtok_r(line,del,&ptr)); // returns the ID and stores in in the root node.
strcpy(roots->name, strtok_r(NULL,del,&ptr)); // returns the description and stores it in the root node.
strcpy(roots->description, strtok_r(NULL,del,&ptr)); // returns the description and stores it in the root node.
prices = strtok_r(NULL,del,&ptr); // returns a string of the price for vm_item.
int dol = atoi(strtok(prices,delm));
int cent = atoi(strtok(NULL,delm));
roots->price.dollars = dol;
roots->price.cents = cent;
int quantity = atoi(strtok_r(NULL,del,&ptr)); // returns how many items are in stock.
roots->on_hand = quantity;
return roots;
}
void addNodeBottom(struct stock_item *val, struct stock_item *head){
//create new node
struct vm_node *newNode = (struct vm_node*)malloc(sizeof(struct vm_node));
if(newNode == NULL){
printf("%s", "Unable to allocate memory for new node\n");
exit(-1);
}
newNode->data= val;
// printf("%s",newNode->data->id);
newNode->next = NULL; // Change 1
//check for first insertion
if(head->next == NULL){
head->next = newNode->data;
printf("\nadded at beginning\n");
}
else
{
//else loop through the list and find the last
//node, insert next to it
struct vm_node *current = head;
while (TRUE) { // Change 2
if(current->next == NULL)
{
current->next = newNode;
printf("\nadded later\n");
break; // Change 3
}
current = current->next;
};
}
}
代码运行时的输出是:
I0001|可乐|75 毫升 jar 装可乐|3.50|50
添加在开头
I0002|百事可乐|375 毫升 jar 装百事可乐|3.00|20
稍后添加
I0003|柠檬芝士蛋糕|美味的 1/8 片芝士蛋糕|4.00|10
稍后添加
I0004|Mars Bar|美味的 50 克 Mars Bar,按照您喜欢的方式冷藏。|3.00|20
稍后添加
I0005|柠檬挞|美味的柠檬黄油挞配糕点|3.75|12
稍后添加
解析时到达文件末尾。
此输出是使用 eclipse 生成的,当我在终端中运行它时,它只是响应一个没有显示的段错误。
#ifndef VM_TYPE
#define VM_TYPE
#define IDLEN 5
#define NAMELEN 40
#define DESCLEN 255
#define NUMDENOMS 8
#define UNUSED(var) (void)var
#define COIN_COUNT 20
#define DEFAULT_ONHAND 20
/* Type definition for our boolean type */
typedef enum truefalse
{
FALSE, TRUE
} BOOLEAN;
/* Each price will have a dollars and a cents component */
struct price
{
unsigned dollars,cents;
};
/* The different denominations of coins available */
enum denomination
{
FIVE_CENTS, TEN_CENTS, TWENTY_CENTS, FIFTY_CENTS, ONE_DOLLAR,
TWO_DOLLARS, FIVE_DOLLARS, TEN_DOLLARS
};
/* Each coin in the coins array will have a denomination (20 cents,
* 50 cents, etc) and a count - how many of that coin do we have on hand
*/
struct coin
{
enum denomination denom;
unsigned count;
};
/* The data structure that holds the data for each item of stock
*/
struct stock_item
{
char id[IDLEN+1];
char name[NAMELEN+1];
char description[DESCLEN+1];
struct price price;
unsigned on_hand;
struct stock_item *next;
};
/* The data structure that holds a pointer to the stock_item data and a
* pointer to the next node in the list
*/
struct vm_node
{
struct stock_item * data;
struct vm_node * next;
};
/* The head of the list - has a pointer to the rest of the list and a
* stores the length of the list
*/
struct vm_list
{
struct vm_node * head;
unsigned length;
};
/* This is the head of our overall data structure. We have a pointer to
* the vending machine list as well as an array of coins.
*/
struct vm
{
struct vm_list * item_list;
struct coin coins[NUMDENOMS];
char * foodfile;
char * coinsfile;
};
#endif
这些都是类型定义
这是正在读取的文件结构。
I0001|可乐|75 毫升 jar 装可乐|3.50|50
I0002|百事可乐|375 毫升 jar 装百事可乐|3.00|20
I0003|柠檬芝士蛋糕|美味的 1/8 片芝士蛋糕|4.00|10
I0004|Mars Bar|美味的 50 克 Mars Bar,按照您喜欢的方式冷藏。|3.00|20
I0005|柠檬挞|美味的柠檬黄油挞配糕点|3.75|12
最佳答案
错误 Address 0x0 is not stack'd, malloc'd or (recently) free'd
意味着 strcpy
的源是一个 NULL
指针。显然,strtok_r
返回的是 NULL
而不是您所期望的。
您应该以这样一种方式重写您的代码,即如果您的输入格式不正确,您会检测到 NULL
而不是崩溃。 (您的代码还有其他问题)。
关于c - Valgrind:1.strcpy 的读取大小无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26211971/
我遇到以下问题。我想读取一个包含数百万行和数百列的大型 csv。我想向下转换列的数据类型。我的方法是读取 csv,然后使用 pd.to_numeric() 对其进行向下转换。我不知道列数及其类型。在读
目前,我从 SQL server (2008) 数据库获取数据。 cyurrent的方法是使用DataTable,然后将其传递并使用。 if (parameters != null)
我有以下问题。我有一个巨大的 csv 文件,想用多处理加载它。对于一个包含 500000 行和 130 列不同数据类型的示例文件,Pandas 需要 19 秒。我试过 dask 因为我想多处理阅读。但
是否有关于用于序列化各种 MFC 数据结构的二进制格式的明确文档?我已经能够在十六进制编辑器中查看我自己的一些类,并使用 Java 的 ByteBuffer 类读取它们(使用自动字节顺序转换等)。 但
我正在使用 Selenium 进行测试,我们用 HTML 文件编写测试用例,并用它们制作测试套件,我们的要求是编写足够健壮的测试用例,以根据测试环境改变自身。 为此,我不希望在 HTML 脚本本身中包
我需要一个 JavaScript 代码来读取存储为 .txt 文件的字典(或者也可以保存为任何其他类型的文件。它也可以在线获得)并将其内容存储在一个变量中。我不能找到一种让 JavaScript 像
我正在尝试遍历包含 SSH 登录和其他日志的日志文本文件。 程序正在返回 SSH 登录的总数。 我的解决方案确实有效,但似乎有点慢(在 200mo 文件上大约需要 3.5 秒)。我想知道是否有任何方法
我正在将大量数据从一个电子表格复制到工作簿中的其他 160 个电子表格。目前,Excel (2013) 遇到错误,因为它没有足够的资源来完成操作。 我的目标是将工作表 4 中 V13:XI1150 范
我正在尝试读取一个有 1147 行的文本文件。下面的代码仅读取第 1050-1147 行。我的目标是读取整个文件并提取位于不同行的特定值以在脚本中使用。一个示例是包含“BlockList: 2”的行中
我正在为游戏编写解释器。用户将其移动输入解释器,程序执行该移动。 现在我想为每个决定实现一个时间限制。玩家不应该能够思考超过 30 秒来写一个移动并按下回车。 call_with_time_limit
以this file例如,我正在尝试读取 data.frame 中的数据。来自 the doc (pdf 文件,表 1),它遵循一些 fortran 约定。我尝试了以下但收效甚微: dir 0' 将
我正在使用 R 阅读 Outlook 附件。我的引用在这里:Download attachment from an outlook email using R 这是我的电子邮件的截图: 这每天都会发送
我不会从表格中读取行来将主题放在列表中 php脚本 $url_obj='http://'.$host.':8069/xmlrpc/object'; $sock=new xmlrpc_client($u
我有一个这样的 csv 文件: id,name,value 1,peter,5 2,peter\,paul,3 我如何读取此文件并告诉 R "\," 不表示新列,仅表示 ","。 我必须添加该文件
我正在尝试读取 ~/Library/Preferences/com.apple.mail.plist (在 Snow Leopard 上)以获取电子邮件地址和其他信息以进入“关于”对话框。我使用以下代
This question already has answers here: How do I use floating-point division in bash? (19个回答) 5个月前关闭
本练习的目标是读取输入文件并将其存储到表中,然后验证输入中的某些字段并输出任何错误记录。我需要读取并存储每个策略组,以便表中一次仅存储 5 条记录,而不是整个文件。 所以我需要读取一个包含 5 条记录
据我了解,LWT 插入始终以 SERIAL 一致性级别完成。如果为 true,这是否意味着读取作为 LWT 插入的行可以安全地以 ANY 的一致性级别读取? 换句话说,我假设 LWT 插入是完全一致的
我看到很多很多通过java脚本读取cookie的函数,但我只想在变量中使用它一次,我是JS新手。 这是我的代码 var TheNumber = (Math.random() + '') * 10000
我正在使用 asp.net 和 C#。我在服务器上部署了一个应用程序[已发布],现在我想查看该网站的代码,据我所知,我可以阅读程序集来查看代码。 请告诉我如何实现它。 提前致谢。 最佳答案 您可以使用
我是一名优秀的程序员,十分优秀!