处理未初始化的指针(如何检查?) [英] handling uninitialized pointed (How to check ?? )

查看:132
本文介绍了处理未初始化的指针(如何检查?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




现在addDynamicMemory(char ** ptr,int size)方法可以在输入ptr被初始化为NULL或者处理
时处理一些东西。但是如何使用
改进这种方法来处理未初始化的指向甚至。任何答案??


谢谢,

Sanjay

#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int addDynamicMemory(char ** ptr,int size)

{

/ *查看并判断大小内存是否可用* /

int currSize;

if( * ptr == NULL)

{

* ptr =(char *)malloc(size * sizeof(char));

if(b) ptr == NULL)

{

printf(" Initialized memory as null \\\
");

return -1;

}

其他

{

printf(无法将内存初始化为空\ n);

返回-1;

}

}


currSize = strlen(* ptr);

size = currSize + size;

* ptr =(char *)realloc(* ptr,size * sizeof(char));


if(ptr!= NULL)

{

printf(" re Allocation size is%d \ nn,size);

返回0;

}


printf("重新分配失败\ n;;

返回-1;

}


// int main(int argc, char * argv [])

void main()

{

char * test;

test = NULL ;


addDynamicMemory(& test,40);

printf(首先测试值是%s \ n,测试);

strcpy(test," 444444444");

printf(" After allocation val is%s \ n",test);

addDynamicMemory(& test,50);

strcat(test," 5555555555");

printf(" after allocation val is%s \ n",test);

}

Hi,

Right now addDynamicMemory(char **ptr, int size) method can able to
handle if input ptr is intitialized to NULL or something. But how to
improve this method to handle uninitialized pointed even. Any Answer ??

Thanks,
Sanjay
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int addDynamicMemory(char **ptr, int size)
{
/* See and chek whether size memory is available or not */
int currSize;
if(*ptr == NULL)
{
*ptr = (char*) malloc(size * sizeof(char));
if(ptr == NULL)
{
printf("Initialized memory as null \n");
return -1;
}
else
{
printf("Can not Initialized memory as null \n");
return -1;
}
}

currSize = strlen(*ptr);
size = currSize + size;
*ptr = (char*) realloc(*ptr, size*sizeof(char));

if(ptr != NULL)
{
printf(" re Allocation size is %d\n",size);
return 0;
}

printf(" re Allocation failed \n");
return -1;
}

//int main(int argc, char* argv[])
void main()
{
char *test;
test = NULL;

addDynamicMemory(&test, 40);
printf("At first test value is %s\n",test);
strcpy(test,"444444444");
printf("After allocation val is %s\n", test);

addDynamicMemory(&test, 50);
strcat(test,"5555555555");
printf("After allocation val is %s\n", test);
}

推荐答案

sa ********* @ gmail.com 说:


现在addDynamicMemory(char ** ptr,int size)方法能够处理输入ptr被初始化为NULL或者其他东西。但是如何改进这种方法来处理未初始化的指向甚至。任何答案?


不要发送未初始化的指针。

#include< stdio.h>
#include< stdlib.h>
#include< string.h>
int addDynamicMemory(char ** ptr,int size)


int addDynamicMemory(char ** ptr, size_t size)

{
/ *查看并判断大小内存是否可用* /
int currSize;


size_t currSize;

char * tmp; / *你需要在一分钟内* /

if(* ptr == NULL)


如果ptr为NULL,* ptr是一个糟糕的举动。

{
* ptr =(char *)malloc(size * sizeof(char));


* ptr = malloc(size);

if(ptr == NULL)


太晚了。

{
printf(" Initialized memory as null \\\
");
返回-1;
}

{
printf(无法初始化内存为空\ n);
返回-1;
}


currSize = strlen( * ptr);
size = currSize + size;
* ptr =(char *)realloc(* ptr,size * sizeof(char));


currSize = strlen(* ptr);

tmp = realloc(* ptr,size);

if(tmp! = NULL)

{

* ptr = tmp;

size + = currSize;

}

else

{

你还有一些工作要做。通话失败。你将如何处理这个?

}

if(ptr!= NULL)
{/> printf(" re Allocation size is%d \\ \
",大小);


因为正确的大小类型是size_t,%d不会再削减它。

返回0;
}

printf(重新分配失败\ n);
返回-1;
}
// int main(int argc,char * argv [])
void main()


int main(无效)

{
char * test;
test = NULL;


char * test = NULL;

addDynamicMemory(& test,40);
Hi,

Right now addDynamicMemory(char **ptr, int size) method can able to
handle if input ptr is intitialized to NULL or something. But how to
improve this method to handle uninitialized pointed even. Any Answer ??
Don''t send it uninitialised pointers.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int addDynamicMemory(char **ptr, int size)
int addDynamicMemory(char **ptr, size_t size)
{
/* See and chek whether size memory is available or not */
int currSize;
size_t currSize;
char *tmp; /* you''ll need this in a minute */
if(*ptr == NULL)
If ptr is NULL, *ptr is a bad move.
{
*ptr = (char*) malloc(size * sizeof(char));
*ptr = malloc(size);
if(ptr == NULL)
Too late.
{
printf("Initialized memory as null \n");
return -1;
}
else
{
printf("Can not Initialized memory as null \n");
return -1;
}
}

currSize = strlen(*ptr);
size = currSize + size;
*ptr = (char*) realloc(*ptr, size*sizeof(char));
currSize = strlen(*ptr);
tmp = realloc(*ptr, size);
if(tmp != NULL)
{
*ptr = tmp;
size += currSize;
}
else
{
You have some work to do. The call failed. How will you handle this?
}

if(ptr != NULL)
{
printf(" re Allocation size is %d\n",size);
Because the right type for size is size_t, %d won''t cut it any more.
return 0;
}

printf(" re Allocation failed \n");
return -1;
}

//int main(int argc, char* argv[])
void main()
int main(void)
{
char *test;
test = NULL;
char *test = NULL;
addDynamicMemory(&test, 40);




如果你没有测试它,为什么还要回复一个值呢?


-

Richard Heathfield

" Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



Why bother returning a value if you don''t test it?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


sa ********* @ gmail.com 写道:
sa*********@gmail.com wrote:


现在addDynamicMemory(char ** ptr,int size)方法能够处理输入ptr被初始化为NULL或其他什么。但是如何改进这种方法来处理未初始化的指向甚至。任何答案??
Hi,

Right now addDynamicMemory(char **ptr, int size) method can able to
handle if input ptr is intitialized to NULL or something. But how to
improve this method to handle uninitialized pointed even. Any Answer ??



你的意思是检查包含

NULL以外的随机值的指针吗?我想你在谈论一个案例,你只需将指针

传递给你的addDynamicMemory(),而不是malloc()ed。哪个不是

null。


我认为你无法测试这个案例。要么在将它传递给你自己的addDynamicMemory()之前必须明确地将它设为NULL

,或者为它分配

一些空间。


如果我错了,请纠正我。


-Madhav。


Do you mean to check for pointers containing random values other than
NULL? I think you are talking about a case when you just pass a pointer
to your addDynamicMemory( ) which is not "malloc()ed" and which is not
null.

I think you cannot test this case. Either you have to make it NULL
explicitly before passing it to your own addDynamicMemory() or allocate
some space for it.

Please correct me if I am wrong.

-Madhav.


我只想打电话给这个

void main()

{

char * test;

// test = NULL; (不要在这里初始化......)


addDynamicMemory(& test,40);


}

I just want to call like this
void main()
{
char *test;
//test = NULL; (Dont want to initialize it here ......)

addDynamicMemory(&test, 40);

}


这篇关于处理未初始化的指针(如何检查?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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