我正在玩这个代码:
#include <stdlib.h>
#include <stdio.h>
void function(int *p){
free(p);
}
int main(){
int *i = calloc(3, sizeof(int));
function(i);
i[0] = 'a';
printf("%c\n", i[0]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望得到一个错误,但它打印'a',如果'a'被解除分配,为什么打印它?函数(int*p)中发生的事情有什么影响,如果main()?(如果可以的话,解释那个'p'指针发生了什么)
假设我在同一个函数中有这两个:
int *a = malloc(...);
int *b = a;
Run Code Online (Sandbox Code Playgroud)
它们都指向同一块内存,但是当我必须释放它时,我应该在它们两个上调用free,还是只调用其中一个?(再次解释为什么可能)
来自Mike Ash的评论:https: //www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html#comment-3abf26dd771b7bf2f28d04106993c07b
这是代码:
void Tester(int ign, float x, char y)
{
printf("float: %f char: %d\n", x, y);
}
int main(int argc, char **argv)
{
float x = 42;
float y = 42;
Tester(0, x, y);
void (*TesterAlt)(int, ...) = (void *)Tester;
TesterAlt(0, x, y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
他在主要功能中所做的演员对我来说非常不清楚.
TesterAlt是一个返回void的函数的指针,它与函数Tester的返回类型相同.他分配给这个函数指针,函数Tester,但是他将后一种返回类型转换为void类型的指针(我不确定这一点).
如果我编译代码改变该行:
void (*TesterAlt)(int, ...) = (void)Tester;
Run Code Online (Sandbox Code Playgroud)
我收到编译器错误:
initializing 'void (*)(int, ...)' with an expression of incompatible type 'void'
void (*TesterAlt)(int, ...) = (void) Tester;
Run Code Online (Sandbox Code Playgroud)
为什么他要做这个演员?他的语法是什么意思?
编辑:我对原始问题不是很清楚,我不明白这种语法以及我必须如何阅读它.
(void *)Tester; …Run Code Online (Sandbox Code Playgroud) 我试图使用这个函数:CACurrentMediaTime(),因为我需要当前时间,但是我得到了这个错误
Ld /Users/aruffolo/Library/Developer/Xcode/DerivedData/Star-hbmohpuyghsjeseyswvvpevxkesj/Build/Products/Debug/Star.app/Contents/MacOS/Star normal x86_64
cd "/Users/aruffolo/Documents/Programmazione/Learning Objective-c/Star"
setenv MACOSX_DEPLOYMENT_TARGET 10.8
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -L/Users/aruffolo/Library/Developer/Xcode/DerivedData/Star-hbmohpuyghsjeseyswvvpevxkesj/Build/Products/Debug -F/Users/aruffolo/Library/Developer/Xcode/DerivedData/Star-hbmohpuyghsjeseyswvvpevxkesj/Build/Products/Debug -filelist /Users/aruffolo/Library/Developer/Xcode/DerivedData/Star-hbmohpuyghsjeseyswvvpevxkesj/Build/Intermediates/Star.build/Debug/Star.build/Objects-normal/x86_64/Star.LinkFileList -mmacosx-version-min=10.8 -fobjc-arc -fobjc-link-runtime -framework SystemConfiguration -framework CoreMedia -framework Cocoa -o /Users/aruffolo/Library/Developer/Xcode/DerivedData/Star-hbmohpuyghsjeseyswvvpevxkesj/Build/Products/Debug/Star.app/Contents/MacOS/Star
Undefined symbols for architecture x86_64:
"_CACurrentMediaTime", referenced from:
-[MyView runLoop] in MyView.o
ld: symbol(s) not found for architecture x86_64
Run Code Online (Sandbox Code Playgroud) import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tetris extends JFrame {
public Tetris() {
add(new GamePanel());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setSize(800, 600);
setVisible(true);
setLocationRelativeTo(null);
setTitle("Tetris");
}
public class GamePanel extends JPanel {
public GamePanel(){
TetrisBoard tetraBoard= new TetrisBoard();
GridBagLayout layout= new GridBagLayout();
this.setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 2;
c.gridy = 1;
c.ipadx = 190;
c.ipady = 390;
c.insets.left= 360;
layout.setConstraints(tetraBoard, c);
this.add(tetraBoard);
setBackground(Color.WHITE);
}
@Override
public void paint(Graphics g){
super.paint(g); …Run Code Online (Sandbox Code Playgroud) 我有一个练习:给定一个AtomicIntegers矩阵(初始化为0),我必须为每一行运行一个线程,并为每一列的一个线程,在我减去1的行上,在我添加1的列上,所以在结束矩阵应保持原样.问题是矩阵发生了变化!下面是代码:该对象采用矩阵,一个布尔值,它将告诉他对列或行进行操作,以及它必须操作的列/行的索引.
import java.util.concurrent.atomic.AtomicInteger;
public class FifthExerciseSafe implements Runnable{
private Thread thread;
private boolean onRows;//tells if the object operates on the rows or on the columns
private AtomicInteger[][] matrix;
private int index;
public FifthExerciseSafe(AtomicInteger[][] matrix, boolean onRows, int index){
this.matrix = matrix;
this.onRows = onRows;
this.index = index;
}
public void start(){
thread = new Thread(this);
thread.start();
}
public boolean isOnRows() {
return onRows;
}
public void setOnRows(boolean onRows) {
this.onRows = onRows;
}
public AtomicInteger[][] getMatrix() {
return matrix;
} …Run Code Online (Sandbox Code Playgroud) 我试图了解如何使用xmm寄存器比较两个浮点数(32位).为了测试我在C中编写了这段代码(在程序集中调用代码):
#include "stdio.h"
extern int compare();
int main()
{
printf("Result: %d\n", compare());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是程序集,我想测试是否b <c,在这种情况下确实如此,代码应返回1,但它返回0:
section .data
a: dd 5.5555
b: dd 1.1111
c: dd 5.5555
section .text
global compare
compare:
; -------------------------------------------
; Entrace sequence
; -------------------------------------------
push ebp ; save base pointer
mov ebp, esp ; point to current stack frame
push ebx ; save general registers
push ecx
push edx
push esi
push edi
movss xmm0, [b]
movss xmm1, [c]
comiss xmm0, xmm1
jl change …Run Code Online (Sandbox Code Playgroud)