或者更确切地说,strtok如何生成它的返回值指向的字符串?它是否动态分配内存?我问,因为我不确定是否需要在以下代码中释放令牌:
STANDARD_INPUT变量用于退出过程,以防我内存耗尽以进行分配,字符串是测试对象.
int ValidTotal(STANDARD_INPUT, char *str)
{
char *cutout = NULL, *temp, delim = '#';
int i = 0; //Checks the number of ladders in a string, 3 is the required number
temp = (char*)calloc(strlen(str),sizeof(char));
if(NULL == temp)
Pexit(STANDARD_C); //Exit function, frees the memory given in STANDARD_INPUT(STANDARD_C is defined as the names given in STANDARD_INPUT)
strcpy(temp,str);//Do not want to touch the actual string, so copying it
cutout = strtok(temp,&delim);//Here is the lynchpin -
while(NULL != cutout)
{
if(cutout[strlen(cutout) - 1] …Run Code Online (Sandbox Code Playgroud) 我正在尝试_crtBreakAlloc在此链接中建议的Watch窗口中使用,但值行显示'identifier'_crtBreakAlloc"未识别',它根本不起作用.
我究竟做错了什么?我顺便使用Visual Studio.
代码示例:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <malloc.h>
int main()
{
int *arr = (int*)malloc(10 * sizeof(int)); //breakpoint here
free(arr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后我将_crtBreakAlloc写入Watch窗口的Name字段,并在遇到断点时按Enter键.
我想在我的程序中有条件地包含头文件.是否可能,如果可以,我该怎么做?
我的想法是做这样的事情:
switch(opt)
{
case 0:
{
#include "matrix.h"
break;
}
case 1:
{
#include "grid.h"
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是VS在我写这篇文章时的表现.这样对吗?
因此,如果我做对了,你应该为条件使用布尔表达式.然而,当我尝试使用时,impact == null我收到以下错误:
Conditional breakpoint has compilation error(s).
Reason:
Syntax error on token "==", invalid AssignmentOperator.
Run Code Online (Sandbox Code Playgroud)
是什么赋予了?我做错了什么,我该如何解决?
我尝试包括string.h和stdlib.h,但仍然得到未定义的引用编译错误.
这使我得出结论,它是在我不包括的不同的库中.它在哪里?
我正在使用gcc编译器 - 代码是用Windows编写的,但是将在unix服务器上编译和运行.
所以,我试图scanf在32位ATT程序集中使用该函数并继续得到分段错误,尽管使用的代码Computer Systems: A Programmer's Perspective与我自己的简单C输入程序生成的汇编代码几乎相同.我不知道我做错了什么,并希望得到一些帮助.
我的测试汇编代码(segfaults):
.data
.align 4
fmt: .string "%d"
str: .string "Input a number: "
.text
.global main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $40, %esp
movl $str, (%esp)
call printf
leal 36(%esp), %eax
movl %eax, 4(%esp)
movl $fmt, (%esp)
call scanf
pushl -4(%ebp)
call printf
movl %ebp, %esp
popl %ebp
ret
Run Code Online (Sandbox Code Playgroud)
C代码和它的汇编:
C:
#include <stdio.h>
int main()
{
int i, j;
printf("%s\n","Enter 2 numbers:");
scanf("%d %d",&i,&j);
printf("i = …Run Code Online (Sandbox Code Playgroud) 我正在尝试替换所有使用 Files 和 FileReaders 的实例,并用 InputStreams 和适当的读取器替换它们,以准备将应用程序打包到 jar 中 - 但是,当我尝试使用ClassLoader.getSystemClassLoader().getResourceAsStream()File 方法设法找到的相同路径时,它由于找不到文件而失败。
我使用和输出的代码:
文件中的文本:
e:Easy
definitions/easy_level_definitions.txt
h:Hard
definitions/hard_level_definitions.txt
d:Derp
definitions/derp_level_definitions.txt
Run Code Online (Sandbox Code Playgroud)
代码:
String line;
File f = new File("src/resources/level_sets.txt");
BufferedReader reader1 = null;
try {
reader1 = new BufferedReader(new FileReader(f));
while ((line = reader1.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader1 != null) {
reader1.close();
}
} catch (IOException e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我在X86汇编中有几个关于内存和寄存器的问题:
我有一个字符串"abcdefgh",寄存器%eax保存一个指向字符串的指针.现在我movl (%eax), %edx用来抓取字符串的前四个字节%edx.它们如何存储在寄存器中?是字符d在%dl寄存器中,还是字符a?
movb %eax, %dl例如,当使用它的哪个%eax字节实际上移动了?一个%al或一个?甚至可以这样做吗?或者我应该使用这样的指针 - movb (%eax), %dh- 取指针指向的第一个字节?
如果我打开一个文本文件open("file.txt",O_RDONLY),是否需要将其关闭close()?
愚蠢的问题,我知道,但我不完全确定进程的结束是否会打开打开的文件.
与此相关的问题: 如何读取未知长度的输入字符串?
几个问题:
这条线if(!str)return str是什么意思?
为什么是int ch整数?它不应该是一个炭?
size完全给定函数的目的是什么?
问一个单独的问题因为我理解了这个方法,而不是引擎盖下的一些东西.
c ×7
assembly ×2
string ×2
att ×1
classloader ×1
crt ×1
eclipse ×1
endianness ×1
free ×1
header ×1
inputstream ×1
java ×1
memory-leaks ×1
scanf ×1
strcat-s ×1
strtok ×1
system-calls ×1
unix ×1
x86 ×1