Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   handling uninitialized pointed (How to check ?? ) (http://www.velocityreviews.com/forums/t440676-handling-uninitialized-pointed-how-to-check.html)

sanjaymeher@gmail.com 12-28-2005 09:10 AM

handling uninitialized pointed (How to check ?? )
 
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);
}


Richard Heathfield 12-28-2005 09:19 AM

Re: handling uninitialized pointed (How to check ?? )
 
sanjaymeher@gmail.com said:

> 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);


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)

Madhav 12-28-2005 09:30 AM

Re: handling uninitialized pointed (How to check ?? )
 
sanjaymeher@gmail.com wrote:

> 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 ??
>

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.


sanjaymeher@gmail.com 12-28-2005 09:34 AM

Re: handling uninitialized pointed (How to check ?? )
 
I just want to call like this


void main()
{
char *test;
//test = NULL; (Dont want to initialize it here ......)

addDynamicMemory(&test, 40);

}


Richard Heathfield 12-28-2005 09:51 AM

Re: handling uninitialized pointed (How to check ?? )
 
sanjaymeher@gmail.com said:

> I just want to call like this
>
>
> void main()


main returns int. I told you that already, I'm sure. If you can't get that
right, you are not ready for malloc.

--
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)

slebetman@yahoo.com 12-28-2005 10:50 AM

Re: handling uninitialized pointed (How to check ?? )
 
sanjaymeher@gmail.com wrote:
> I just want to call like this
>
> void main()


int main()

> {
> char *test;
> //test = NULL; (Dont want to initialize it here ......)
>


Simple:

char *test = NULL;

If you ALWAYS declare pointers like this, you will have no problems.
The answer is simply to NEVER declare pointer without initialising it
to NULL. If you see anywhere in your code where you declare a pointer
that is not initialised to NULL consider is a BUG. It's only 8
characters including spacebars, why not just type it?

Again: ALWAYS declare pointers by initialising it to NULL:

char *test = NULL;
int *idx = NULL;
char *buffer = NULL;
...


Madhav 12-28-2005 10:56 AM

Re: handling uninitialized pointed (How to check ?? )
 
sanjaymeher@gmail.com wrote:

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


The problem in this case is that you dont know (and can't check)
whether the "test" pointer points to a valid address in the heap. In
the code above, it could point to anything because the standard does
not specify initial value for pointers.

Also, please change the return type of main to int, and add a
"void" within the brackets if you don't want to use the command line
arguments.


Martin Ambuhl 12-28-2005 11:00 AM

Re: handling uninitialized pointed (How to check ?? )
 
sanjaymeher@gmail.com wrote:
> I just want to call like this
>
>
> void main()

^^^^
No, you don't. You're dead already.

Kenny McCormack 12-28-2005 11:28 AM

Re: handling uninitialized pointed (How to check ?? )
 
In article <1135761001.460004.30790@g47g2000cwa.googlegroups. com>,
sanjaymeher@gmail.com <sanjaymeher@gmail.com> wrote:
>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 ??


I don't know. Speak English, maybe?


sanjaymeher@gmail.com 12-28-2005 11:52 AM

Re: handling uninitialized pointed (How to check ?? )
 
I wanted this method to be full proof to be used by other user. My
method should able to handle if any uninitialized variable is coming..
This is really not the good part of C language if i try to speak
ENGLISH ....

Thanks
Sanjay



All times are GMT. The time now is 02:24 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.