在这个小程序中,所有三个文件都有不同的职责。
该程序的目标是将data.c文件中的数据读入到testing.h中创建的struct数组中,然后可以在testing.c中访问和使用它。
我发现当我尝试在本地范围外打印并进一步尝试在文件外打印时输出会发生变化。
data.c
private void SignWithCertificate(X509Certificate2 cert)
{
ICollection<X509Certificate> chain = new List<X509Certificate>();
X509Chain x509chain = new X509Chain();
x509chain.Build(cert);
foreach (X509ChainElement x509ChainElement in x509chain.ChainElements)
{
chain.Add(DotNetUtilities.FromX509Certificate(x509ChainElement.Certificate));
}
IExternalSignature externalSignature = new X509Certificate2Signature(cert, "SHA-256");
PdfReader pdfReader = new PdfReader(_sourceFile);
FileStream signedPdf = new FileStream(_targetFile, FileMode.Create); //the output pdf file
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0');
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
//here set signatureAppearance at your will
signatureAppearance.Reason = _reason;
signatureAppearance.Location = _location;
signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
signatureAppearance.Acro6Layers = true;
signatureAppearance.Layer4Text = "";
signatureAppearance.SetVisibleSignature(new Rectangle(_coordinates), _pageNo, "Sig");
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CADES);
}
testing.c
void data() {
size_t friends[] = {1,3,4};
userdata[0].friends_ids = friends;
size_t friends2[] = {5,8,9};
userdata[1].friends_ids = friends2;
}
testing.h
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "testing.h"
#include "data.c"
int main() {
data();
for (int x=0; x<3; x++) {
printf("%d\n", userdata[0].friends_ids[x]);
}
printf("\n");
for (int x=0; x<3; x++) {
printf("%d\n", userdata[1].friends_ids[x]);
}
}
输出:
struct user {
size_t* friends_ids;
};
struct user userdata[2];
很明显这些是userdata [1]的垃圾值,但我很困惑为什么userdata [0]正确打印但userdata [1]输出垃圾值?
我试过的是为结构分配内存,但我不知道是否可能,因为我不断尝试错误说“初始化元素不是常数”。
1
3
4
-1942895168
1680886992
-320301600
答案 0 :(得分:0)
正如评论中所提到的,在发布的代码中可能存在 undefined behavior ,但由于发布的代码导致运行时错误,我没有那么远:第一次调用printf
函数中的main
时,解除引用超出范围的指针。 (可能是由于UB)
下面列出了问题(和建议的修复)。
void data() {...
不是有效的函数原型,应该抛出警告。为了您的目的,甚至:
void data(void) {...
虽然有效,但还不够。将其更改为:
void data(struct user User[2]) {...
顺便说一下,main
函数原型的最小签名是:
int main(void){...
完成这些更改并给出struct user
的定义后,请考虑对data
和main
进行的其他修改:(注意,以下建议仅限于说明一个使用函数参数传递/接收数据的方法。程序体系结构或其他定义struct
的方法留给其他讨论。)
void data(struct user User[2]) // pass struct as argument
{
size_t i;
int a[] = {1,2,3};
int b[] = {4,5,6};
for(i=0;i<sizeof(a)/sizeof(a[0]);i++)
{
userdata[0].friends_ids[i] = a[i];
userdata[1].friends_ids[i] = b[i];
}
}
int main(void) {
userdata[0].friends_ids = calloc(3, sizeof(size_t)); // create memory
userdata[1].friends_ids = calloc(3, sizeof(size_t));
data(&userdata[2] ); //populate struct pointers (pass pointer to struct)
// values returned from call to data() are refreshed and ready to use.
for (int x=0; x<3; x++) { // use struct data
printf("%d\n", userdata[0].friends_ids[x]);
}
printf("\n");
for (int x=0; x<3; x++) {
printf("%d\n", userdata[1].friends_ids[x]);
}
free(userdata[0].friends_ids); // free memory
free(userdata[1].friends_ids);
return 0;
}