Visual studio 2010 malloc、范围、初始化(或缺少)
代码如下:Visual studio 2010 malloc、范围、初始化(或缺少),visual-studio-2010,scope,initialization,malloc,Visual Studio 2010,Scope,Initialization,Malloc,代码如下: // allocation void allocateSymbolStorage(char **pepperShakerList, char **pepperList) { // allocate storage for an array of pointers pepperShakerList = (char **) malloc(MAX_PEPPER_SHAKERS * sizeof(char *)); for (int i = 0; i
// allocation
void allocateSymbolStorage(char **pepperShakerList, char **pepperList)
{
// allocate storage for an array of pointers
pepperShakerList = (char **) malloc(MAX_PEPPER_SHAKERS * sizeof(char *));
for (int i = 0; i < MAX_PEPPER_SHAKERS; i++)
{
if ((pepperShakerList[i] = (char *) malloc(MAX_SHAKERNAME_LENGTH * sizeof(char))) == NULL)
fatalError("failed pepperShakerList alloc");
}
// allocate storage for an array of pointers
pepperList = (char **) malloc(MAX_PEPPERS * sizeof(char *));
for (int i = 0; i < MAX_PEPPERS; i++)
{
if ((pepperList[i] = (char *) malloc(MAX_PEPPER_LENGTH * sizeof(char))) == NULL)
fatalError("failed pepperList alloc");
}
}
void buildPepperShakers(void)
{
char **pepperShakerList, **pepperList;
allocateSymbolStorage(pepperShakerList, pepperList);
// ....
freeSymbolStorage(pepperShakerList, pepperList);
}
//分配
void allocateSymbolStorage(字符**pepperShakerList,字符**pepperList)
{
//为指针数组分配存储
pepperShakerList=(char**)malloc(最大PEPPER_SHAKERS*sizeof(char*);
对于(int i=0;i
以下是VS 2010的错误:
:警告C4700:使用了未初始化的局部变量“pepperList”
以下是困惑:
如果在分配函数中分配字符**,为什么会出现错误?这是一个超出范围的问题吗?这是您想要的,您需要取消引用传入的指针:
*pepperShakerList = (char *) malloc(MAX_PEPPER_SHAKERS * sizeof(char *));
假设您所谈论的是pepperList而不是符号列表,并且假设allocationSymbolStorage中的代码反映了您想要做的事情,那么VC的抱怨是正确的 目前,您的代码将崩溃,因为在buildPepperShakers()中,您无法从allocateSymbolStorage获取任何值 因此,您的allocateSymbolStorage应声明为:
void allocateSymbolStorage(char ***pepperShakerList, char ***pepperList)
然后将本地指针持有者变量在buildPepperShakers中的地址,即pepperList
和pepperShakerList
传递给分配函数,这样它就可以根据TJD的答案进行分配。即:
void buildPepperShakers(void) {
char **pepperShakerList, **pepperList;
allocateSymbolStorage(&pepperShakerList, &pepperList);
}
当然,您的allocateSymbolStorage主体现在变成:
void allocateSymbolStorage(char ***pepperShakerList_p, char ***pepperList_p)
{
char **pepperShakerList, **pepperList;
// allocate storage for an array of pointers
pepperShakerList = (char **) malloc(MAX_PEPPER_SHAKERS * sizeof(char *));
for (int i = 0; i < MAX_PEPPER_SHAKERS; i++)
{
if ((pepperShakerList[i] = (char *) malloc(MAX_SHAKERNAME_LENGTH * sizeof(char))) == NULL)
fatalError("failed pepperShakerList alloc");
}
// allocate storage for an array of pointers
pepperList = (char **) malloc(MAX_PEPPERS * sizeof(char *));
for (int i = 0; i < MAX_PEPPERS; i++)
{
if ((pepperList[i] = (char *) malloc(MAX_PEPPER_LENGTH * sizeof(char))) == NULL)
fatalError("failed pepperList alloc");
}
*pepperShakerList_p = pepperShakerList;
*pepperList_p = pepperList;
}
void allocateSymbolStorage(char***pepperShakerList\u p,char***pepperShakerList\u p)
{
char**pepperShakerList,**pepperList;
//为指针数组分配存储
pepperShakerList=(char**)malloc(最大PEPPER_SHAKERS*sizeof(char*);
对于(int i=0;i
现在VC不应该抱怨了。虽然这是一种丑陋的对象内存管理方式:-)但是没有
符号列表。你的意思是pepperList
?哪一行抛出了错误?我一生都无法在上面的代码中找到symbolist
,但它说明了范围问题,编译器警告的原因。ok,我做了更改,但还是同一个错误。我是否需要以不同的方式将char**传递给allocateSymbolStorage()函数?是的,您需要声明为char*pepperShakerList并传递为&pepperShakerList。我不是有意制造丑陋,您能告诉我如何更好地实现这一点吗?(我主修英语……正如你所看到的,我还在学习。):)太棒了。。。你会做得很好的!我教英语诗歌写作,如果我自己这么说的话,我是一名C语言程序员。。。编程和诗歌是相关的。。。