使用双指针内存后功能中分配 [英] Using Double Pointers after memory allocated within function

查看:109
本文介绍了使用双指针内存后功能中分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C双指针玩,如果我创建初始化表函数不知道,它崩溃上回到主当我试图使用由InitStringTable分配的内存。我相信一个简单的解决方法就是让strTable全球然后,我相信它的美好,但我preFER不这样做,因为这更是一个学习的过程中,我在各地传递表进行修改,即我应该能够InitStringTable之后修改strTable从主或其他功能modifyTable。
 感谢您的帮助,你可以给。

  INT的main()
{
    焦炭** strTable;    //分配内存的字符串表。
    InitStringTable(strTable);
    //下面行应该能够字符串复制到新分配表。
    //以下行导致然而崩溃。
    的strcpy(strTable [0],ABCDEF);    的strcpy(strTable [1],XY);
}//分配内存的字符串表。这个函数应该创建一个表
大小// 10串与每串50个字符长。在code编译罚款。
无效InitStringTable(字符**表)
{
   INT I = 0;   表=(字符**)的malloc(sizeof的(字符)* 10);   对于(I = 0; I&小于10;我+ +)
   {
      表[i] =(字符*)malloc的(的sizeof(char)的* 50);
   }   对于(I = 0; I&小于10;我+ +)
   {
      memset的(表[I],0,50);
   }   的strcpy(表[0],字符串1);
}


解决方案

C是传值。

分配到的值丢失从返回InitStringTable()


另外分配指针字符时,要求的空间指向字符

所以这个:

  ... =(字符**)的malloc(sizeof的(字符)* 10);

至少应为(假定C):

  ... =的malloc(sizeof的(字符*)* 10);


一个可能的方法,这将是:

 的#include<&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&errno.h中GT;INT InitStringTable(CHAR *** ppptable,常量为size_t N,常量为size_t升)
{
   INT结果为0;   如果(NULL == ppptable)
   {
     结果= -1;
     错误号= EINVAL;
   }
   其他
   {
     (* ppptable)=的ma​​lloc(N * sizeof的(** ppptable));
     如果(NULL ==(* ppptable))
     {
       结果= -1;
     }
     其他
     {
       为size_t I = 0;
       对于(; I< N ++ I)
       {
         (* ppptable)[I] =释放calloc(L,sizeof的(*(* ppptable)[I]));
         如果(NULL ==(* ppptable)[I])
         {
           结果= -1;           / *中间失败,需要清理。 * /
           为(;我大于0; --i)
           {
             自由((* ​​ppptable)[I-1]);
           }           免费(* ppptable);
           (* ppptable)= NULL;           打破;
         }
       }
     }
   }   返回结果;
 }

这样称呼它:

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;INT InitStringTable(CHAR *** ppptable,常量为size_t N,常量为size_t升);INT主要(无效)
{
  INT结果= EXIT_SUCCESS;
  焦炭** strTable = NULL;  如果(-1 == InitStringTable(安培; strTable,10,42))// *分配10个串了42个字符数组。 * /
  {
    PERROR(InitStringTable()失败);
    结果= EXIT_FAILURE;
  }
  其他
  {
    的strcpy(strTable [0],ABCDEF);
    的strcpy(strTable [1],XY);
  }  返回结果;
}

和不,我不会进入这个荒谬的你不想成为一个三星级的程序员!讨论。

I was playing with double pointers in C and was wondering if I create a function that initializes the table, it crashes on going back to main when I try to make use of the memory allocated by InitStringTable. I believe a simple fix is to make strTable global and then I believe its OK, but I prefer not to do so as this is more of a learning exercise for me in passing the table around for modification i.e. I should be able to modify strTable from main or another function modifyTable after InitStringTable. Thanks for any help you can give.

int main()
{
    char** strTable;

    // Allocates memory for string table.
    InitStringTable(strTable);
    // Below lines should be able to copy strings into newly allocated table.
    // Below lines cause crash however.
    strcpy(strTable[0], "abcdef");

    strcpy(strTable[1], "xy");
}

// Allocates memory for the string table. This function should create a table
// of size 10 strings with each string 50 chars long. The code compiles fine.
void InitStringTable(char** table)
{
   int i = 0;

   table = (char**)malloc(sizeof(char)*10);

   for(i = 0; i < 10; i++)
   {
      table[i] = (char*)malloc(sizeof(char)*50);
   }

   for(i = 0; i < 10; i++)
   {
      memset(table[i], 0, 50);
   }

   strcpy(table[0], "string1");
}

解决方案

C is pass by value.

The value assigned to table is lost on returning from InitStringTable().


Also when allocating pointers to char ask for room for pointers to char.

So this:

... = (char**)malloc(sizeof(char)*10);

shall at least be (assuming C):

... = malloc(sizeof(char*)*10);


A possible approach to this would be:

#include <stdlib.h>
#include <string.h>
#include <errno.h>

int InitStringTable(char *** ppptable, const size_t n, const size_t l)
{
   int result = 0;

   if (NULL == ppptable)
   {
     result = -1;
     errno = EINVAL;
   }
   else
   {
     (*ppptable) = malloc(n * sizeof(**ppptable));
     if (NULL == (*ppptable))
     {
       result = -1;
     }
     else
     {
       size_t i = 0;
       for(; i < n; ++i)
       {
         (*ppptable)[i] = calloc(l, sizeof(*(*ppptable)[i]));
         if (NULL == (*ppptable)[i])
         {
           result = -1; 

           /* Failing in the middle requires clean-up. */
           for (; i > 0; --i)
           {
             free((*ppptable)[i-1]);
           }

           free(*ppptable); 
           (*ppptable) = NULL;

           break;
         }
       }
     }
   }

   return result;
 }

Call it like this:

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

int InitStringTable(char *** ppptable, const size_t n, const size_t l);

int main(void)
{
  int result = EXIT_SUCCESS;
  char ** strTable = NULL;

  if ( -1 == InitStringTable(&strTable, 10, 42)) //* Allocate array with 10 "strings" à 42 chars. */
  {
    perror("InitStringTable() failed");
    result = EXIT_FAILURE;
  }
  else
  {
    strcpy(strTable[0], "abcdef");
    strcpy(strTable[1], "xy");
  }

  return result;
}

And no, I won't get into this ridiculous "You don't wanna be a 3-star-programmer!" discussion.

这篇关于使用双指针内存后功能中分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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