如何通过在C中推进内存地址来存储结果 [英] How to store results by advancing memory address in c

查看:63
本文介绍了如何通过在C中推进内存地址来存储结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例1:

#include <stdio.h>
#include <string.h>
void saveData(void* data) // only allow to use void* data 
{
  // ...After some calculation: 
  int inputData[5] = {1,2,3,4,5};

  memcpy((int*)data, inputData, sizeof(inputData));
}
int main()
{
  int myNum[256];

  saveData((void*)myNum);

  printf("\n%d%d%d%d%d\n",myNum[0],myNum[1],myNum[2],myNum[3],myNum[4]);

}

我在main中有一个int数组,并将该数组传递给函数saveData,

I have an int array in main, and pass the array into function saveData,

在函数savaData中,我将inputData [5]保存到myNum [256];

In function savaData, I save the inputData[5] into myNum[256];

工作正常.

示例2:

如果我有一个struct数组而不是int数组怎么办:

#include <stdio.h>
#include <string.h>
struct Analysis{
      int a;
      int b[10];
  };

void saveData(void* data, size_t size)  // update here for size param
{
  struct Analysis a1;
  a1.a = 1;
  memset(a1.b, 0, 10*sizeof(int));

  for(int i=0; i<10; i++)
  {
      a1.b[i] = 1;
      printf("%d", a1.b[i]);
  }

  struct Analysis a2;
  a2.a = 2;
  memset(a2.b, 0, 10*sizeof(int));

  for(int i=0; i<10; i++)
  {
      a2.b[i] = 2;
      printf("%d", a2.b[i]);
  }

  //memcpy((int*)data, inputData, sizeof(inputData));
  //How can I copy a1 and a2 into memory space of data(ana[2]) here;
}
int main()
{
  struct Analysis ana[2];

  saveData((void*)ana, sizeof ana[0]); // update here

  for(int i=0; i<2; i++)
  {
      printf("\n%d\n", ana[i].a);
      for(int j=0; j<10; j++)
        printf("%d", ana[i].b[j]);
  }
}

那么,如何在函数saveData中将a1和a2复制到ana [2]的内存中?

我认为是:

我可以将数据(ana)转换为char *以通过其地址并存储结果;

I can cast data(ana) into char* to go through its address and store results;

完成ana [0]后,可以使用sizeof(ana [0])将指针前进到下一个

Once I finish ana[0], I can use sizeof(ana[0]) to advance the pointer to next

一个(ana [1]).

one(ana[1]).

****更新:----------------------------------------- ---------------------------

****UPDATE:--------------------------------------------------------------------

size-这是用于保存每个文件结果的结构的大小.传递此信息很重要.没有此大小,您将无法正确访问已传递给映射的阵列的插槽.由于结果是空指针,因此为了支持多种类型的数组,常规指针算法将无法正常工作,因此大小在这里至关重要.

size - This is the size of the struct used to hold the results for each file. It is VITAL to pass this information. Without this size, you will not be able to correctly access the slots of the array that you have passed to map. Since results is a void pointer, in order to support multiple types of arrays, the regular pointer arithmetic will not work correctly, hence size is crucial here.

推荐答案

那么,如何在函数saveData中将a1和a2复制到ana [2]的内存中?

So, How can I copy a1 and a2 into memory of ana[2] in function saveData?

有很多方法.一种是您使用int s演示它的方式的精确模拟:

There are many ways. One is the exact analog of the way you demonstrate doing it with ints:

void saveData(void* data)
{
  // ...After some calculation: 
  struct Analysis aa[2] = {
    { 1, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
    { 2, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } }
  };

  // note: no need to cast 'data' or 'aa' as long as a prototype is visible
  memcpy(data, aa, sizeof(aa));
}

当然,所有这些都引出了一个问题,即当您的代码对其引用对象的类型以及包含对象的容量(和存在)进行关键假设时,为什么要将saveData()的参数声明为类型void *大批.感觉您太普通了.

Of course, all this begs the question of why you are declaring saveData()'s parameter as type void * when your code makes key assumptions about the type of the its referent and the capacity (and existence) of the containing array. It feels like you are being overly generic.

我认为是:

What I think is:

我可以将数据(ana)转换为char *以通过其地址存储 结果;

I can cast data(ana) into char* to go through its address and store results;

完成ana [0]之后,我可以使用sizeof(ana [0])前进指针 到下一个(ana [1]).

Once I finish ana[0], I can use sizeof(ana[0]) to advance the pointer to next one(ana[1]).

是的,您可以这样做,但是为什么呢?如果要转换指针的类型,则将其转换为要写入的数据的类型(例如struct Analysis *),并像数组一样使用它. (还要注意,赋值运算符可在structunion上使用.):

Yes, you can do that, but why? If you're going to convert the pointer's type, then convert it to the type of the data you are writing to it (e.g. struct Analysis *) and use it just like an array. (And note, too, that the assignment operator works on structs and unions.):

void saveData(void* data)
{
  // ...After some calculation: 
  struct Analysis aa[2] = {
    { 1, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
    { 2, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } }
  };

  struct Analysis *data_array = data;

  for (int i = 0; i < 2; i++) {
      data_array[i] = aa[i];
  }    
}

这与您描述的内容大致相同,但更加清晰.

That's pretty much equivalent to what you describe, but cleaner and clearer.

这篇关于如何通过在C中推进内存地址来存储结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆