473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Array of Pointers and Freeing problem

hi,
Following is problematic Code

main()
{
char *ptr[1]={NULL};
ftn(ptr);
}
void ftn(**data)
{
*data=malloc(4);
memset(*data,0,4);
strcpy(*data,"HAI");
}

May 12 '07 #1
3 1984
On May 12, 12:40 am, anand1...@yahoo.com wrote:
hi,
Following is problematic Code

main()
{
char *ptr[1]={NULL};
ftn(ptr);
printf("String is %s\n",*ptr)
/*It PRints "HAI" -No Problem*/
if(*ptr !=NULL)
free(*ptr);
/*It Says Invalid Pointer*/
}

void ftn(**data)
{
*data=malloc(4);
memset(*data,0,4);
strcpy(*data,"HAI");

}-
hi,
Please let me why the free is throwing "Inavild Pointer" Error.
Your help is highly aprreciated.
Regards
Anand

May 12 '07 #2
an*******@yahoo.com said:
hi,
Following is problematic Code

main()
{
char *ptr[1]={NULL};
ftn(ptr);
}
void ftn(**data)
{
*data=malloc(4);
memset(*data,0,4);
strcpy(*data,"HAI");
}
You're right. The code is problematic. For one thing, it won't compile.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 12 '07 #3
an*******@yahoo.com said:
On May 12, 12:40 am, anand1...@yahoo.com wrote:
>hi,
Following is problematic Code

main()
{
char *ptr[1]={NULL};
ftn(ptr);
printf("String is %s\n",*ptr)
/*It PRints "HAI" -No Problem*/
if(*ptr !=NULL)
free(*ptr);
/*It Says Invalid Pointer*/
>}

void ftn(**data)
{
*data=malloc(4);
memset(*data,0,4);
strcpy(*data,"HAI");

}-

hi,
Please let me why the free is throwing "Inavild Pointer" Error.

foo.c:2: warning: return-type defaults to `int'
foo.c:2: warning: function declaration isn't a prototype
foo.c: In function `main':
foo.c:3: `NULL' undeclared (first use in this function)
foo.c:3: (Each undeclared identifier is reported only once
foo.c:3: for each function it appears in.)
foo.c:3: initializer element is not computable at load time
foo.c:3: (near initialization for `ptr[0]')
foo.c:4: warning: implicit declaration of function `ftn'
foo.c:5: warning: implicit declaration of function `printf'
foo.c:7: parse error before `if'
foo.c:10: warning: control reaches end of non-void function
foo.c: At top level:
foo.c:12: parse error before `*'
foo.c:13: warning: function declaration isn't a prototype
foo.c:13: warning: type mismatch with previous implicit declaration
foo.c:4: warning: previous implicit declaration of `ftn'
foo.c:13: warning: `ftn' was previously implicitly declared to return
`int'
foo.c: In function `ftn':
foo.c:14: `data' undeclared (first use in this function)
foo.c:14: warning: implicit declaration of function `malloc'
make: *** [foo.o] Error 1

Sort those out, and come on back if you're still having trouble.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
23
by: Steffen Fiksdal | last post by:
I have programmed a library which offers an API which takes both 'const char*' and 'char**' pointers as input parameters. An example: extern int apicall( int sessioncontext, const...
7
by: Akhil | last post by:
How much is Pointers different in C# compared to C++? Do we have destructors in C#? if yes, for what do we use it? -- Cheers, Akhil
5
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the...
11
by: seberino | last post by:
Suppose a C extension locally built an array of PyObject* 's as follows... my_array = malloc(n * sizeof(PyObject*)); for (i = 0; i < n; i++) { my_array = PyList_New(0); } Q1: Must I do a...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
11
by: Piotrek | last post by:
Hi, I have no idea why my reallocation function (myadd) is breaking up my array. I'm trying to solve it and it took me few hours already. Maybe you can look at my code and explain me what is...
20
by: silverburgh.meryl | last post by:
In my code, I have an array of char* pointer which is populated statically: void function1() { char *ppsz_argv2 = { "abc" , "def", "dummy"}; //... }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.