(NASM, C)为什么需要 malloc 两次来创建一个数组,如此处所示

(NASM, C)Why would you need to malloc twice to create an array as shown here

char** names=(char**)malloc(count*sizeof(char*));
//while loop
names[count]=(char*)malloc(size+1);//why malloc again?

所以 char** names=(char**)malloc(count*sizeof(char*)); 创建一个指向托管 4 times count 字节的位置的指针,然后将指针位置存储到名称?

然后在while循环中,分配了size+1字节长的内存,并将其地址赋给了names[count],也就是names指向的位置?那么这里从第一个malloc创建的内存存放的是第二个malloc创建的内存位置?内存指针的大小是否为 4 个字节,以便我可以通过从内存位置开始移动到下一个 4 个字节来访问每个 names[count]

如果我的想法是正确的,那么这两行c代码的NASM实现是否正确:

;char** names=(char**)malloc(count*sizeof(char*));
  mov eax, dword count
  ;lea ebx, [eax*4]
  imul ebx, eax, 4
  push ebx
  call _malloc
  mov names, eax
  add esp, 4

;names[count]=(char*)malloc(size+1);
  mov ebx, size
inc ebx
push ebx
call _malloc
add esp,4
mov ebx, names
mov ecx, dword count
mov [ebx+ecx*4], eax

另外,这两行代码是以下 c 代码的一部分,用于从文件中提取名称并对它们进行排序:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  char path[]="names.txt";

  char szName[50];

  int count=0;

  int size=0;

  int x=0;
  int y=0;

  char* temp=0;
  int pos=0;

  FILE* file=fopen(path,"rt");

  if (file){

  while (!feof(file)){

  fscanf(file,"%50s",szName);
  count++;


  }

  }
  else{
       printf("Error opening file\n");
       return 1;
  }

  printf ("Count: %d\n",count);

  char** names=(char**)malloc(count*sizeof(char*));

  rewind(file);

  count=0;



  while (!feof(file)){

  fscanf(file,"%50s",szName);

  size=strlen(szName);

  names[count]=(char*)malloc(size+1);

  strcpy(names[count],szName);

  count++;


  }

  printf("Original file\n");

  for (x=0;x<count;x++){

  printf("Name %d:\t%s\n",x+1,names[x]);

  }


  for (x=0;x<count;x++){

  temp=names[x];
  pos=x;

  for (y=x;y<count;y++){

      if (strcmp(temp,names[y])>0){

      temp=names[y];
      pos=y;

      }    
  }

  temp=names[x];
  names[x]=names[pos];
  names[pos]=temp;    
  }

  printf("Sorted names\n");

  for (x=0;x<count;x++){

  printf("Name %d:\t%s\n",x+1,names[x]);

  }

  system("PAUSE");  
  return 0;
}

So char** names=(char**)malloc(count*sizeof(char*)); creates a pointer to a location hosting 4 times count bytes and then stores pointer location to names?

是的,如果 sizeof(char*) 在您的环境中是 4

Then in the while loop, size+1 bytes long memory is allocated and its address is given to names[count], which refers to the location pointed to by names?

前半部分正确,后半部分错误。 地址被提供给 names.

指向的位置前面的 count 个元素

So here the memory created from the first malloc stores the location of memory created by the second malloc?

是的,确实如此。

And is the size of memory pointer 4 bytes,

视环境而定

so that I can access each names[count] by moving to the next 4 bytes starting from the start of the memory location?

是的,如果 sizeof(char*) 在您的环境中是 4

If my thinking is correct, are these correct NASM implementations of these two lines of c code:

我发现代码没有错误,如果 int 在您的环境中是一个 32 位整数。

您的教授正在创建一个字符串数组。编写的代码不必要地令人困惑,但这个想法是合理的。

这是一个希望不那么令人困惑的例子

// copy an existing string just like strdup()
char* stringDuplicate(const char* string) {
    size_t size = strlen(string) + 1; // 1 additional for `[=10=]` terminator
    char* result = (char*)malloc(size * (sizeof(char)));
    strcpy(result, string);
    return result;
}

// copy an existing string array
char** stringArrayDuplicate(const char** stringArray, size_t size) {
    // Allocate an array which can hold size number of strings
    char** result = (char**)malloc(size * sizeof(char *));

    // For each string in stringArray, copy the string
    for (size_t i = 0; i < size; ++i) {
        result[i] = stringDuplicate(stringArray[i]);
    }

    return result;
}