|
#1
|
|
|
|
|
PURPOSE: see the comments.
WHAT I GOT: infinite loop /* This program will simply create an array of pointers to integers * and will fill it with some values while using malloc to create * pointers to fill the array and then will print the values pointed * by those pointers * */ #include <stdio.h> #include <stdlib.h> enum MAXSIZE { ARRSIZE = 100 }; int main( void ) { int* arr_p[ARRSIZE]; int** pp; int *mp, *np; int i; int null_int = 0; pp = arr_p; np = &null_int; for( i = 0; i < ARRSIZE - 1; ++i ) { mp = malloc( sizeof( int ) ); mp = &i; arr_p[i] = mp; } arr_p[i] = np; while( **pp ) { printf("**pp = %d\n", **pp); } return 0; } |
|
|
|
#2
|
|
|
|
|
arnuld said:
[..] > > int null_int = 0; > > pp = arr_p; > np = &null_int; >> for( i = 0; i < ARRSIZE - 1; ++i ) > { > mp = malloc( sizeof( int ) ); You don't check to see whether this fails to allocate the requested memory... > mp = &i; ....which doesn't really matter, since you throw it away here. > arr_p[i] = mp; > } Well done. You just filled arr_p with a bunch of pointers to i. Why bother with the malloc call, then? You could save yourself (ARRSIZE-1) * sizeof(int) bytes of memory leak by removing the malloc call completely. > arr_p[i] = np; > > while( **pp ) pp has the value &arr_p[0], so **pp attempts to find the int value at *arr_p[0]. Since arr_p[0] points to i, we're looking for the value of i. That value is indeterminate, because the object i never had a value assigned to it. Thus, the behaviour is undefined. This loop could execute forever (the most likely possibility) or not at all (probably the second most likely) or something completely weird could happen. > { > printf("**pp = %d\n", **pp); > } You don't even /try/ to iterate through the array, do you? You might want to take a long hard look at Visual Basic or RCX. |
|
#3
|
|
|
|
|
> On Fri, 02 May 2008 13:44:50 +0000, Richard Heathfield wrote:
>> for( i = 0; i < ARRSIZE - 1; ++i ) >> { >> mp = malloc( sizeof( int ) ); > You don't check to see whether this fails to allocate the requested > memory... done >> mp = &i; > ...which doesn't really matter, since you throw it away here. well, then how can I generate a new variable every-time I enter into the loop ? > Well done. You just filled arr_p with a bunch of pointers to i. Why bother > with the malloc call, then? You could save yourself (ARRSIZE-1) * > sizeof(int) bytes of memory leak by removing the malloc call completely. I can but I did not because I want to understand the how pointers to arrays to pointers behave and learn them. > You don't even /try/ to iterate through the array, do you? okay, here is a little fixed version. I can't do anything about variable i in for loop as I said, I can't find a way to generate new variables at every iteration through the loop. > You might want to take a long hard look at Visual Basic http://lispmachine.wordpress.com/200...urn-for-vbnet/ > or RCX. never heard of it |
|
#4
|
|
|
|
|
"arnuld" <NoSpam> wrote in message
news:0440 > PURPOSE: see the comments. > WHAT I GOT: infinite loop > I've modified your code so that it behaves better: #include <stdio.h> #include <stdlib.h> enum MAXSIZE { ARRSIZE = 100 }; int main( void ) { int *arr_p[ARRSIZE]; int **pp; int *np; int i; int null_int = 0; pp = arr_p; np = &null_int; for( i = 0; i < ARRSIZE - 1; ++i ) { arr_p[i] = malloc( sizeof( int ) ); *arr_p[i] = 1000+i; /* initial the ints to some recognisable (and non-0!) value */ } arr_p[i] = np; /* sentinel (*np contains 0) */ while( **pp) /* Step through array using pp */ { printf("**pp = %d\n", **pp); ++pp; } return 0; } -- Bartc |
|
#5
|
|
|
|
|
[BTW, there seems to be somthing odd in the way you newsreader add
attribution lines. I've fixed it, I think.] arnuld <NoSpam> writes: > On Fri, 02 May 2008 13:44:50 +0000, Richard Heathfield wrote: >> > done >> > well, then how can I generate a new variable every-time I enter into the > loop ? That is what malloc is for. C calls them objects, and you make them exactly as you have written it. The error is overwriting the pointer you get from malloc with one to i. Pointers to local variables like i (automatic storage in C terms) are very rarely used. |
|
#6
|
|
|
|
|
arnuld wrote:
[..] > > arr_p[i] = np; > > while( **pp ) > { > printf("**pp = %d\n", **pp); > } > > return 0; > } /* BEGIN new.c */ /* ** This program will simply create an array of pointers to integers ** and will fill it with some values while using malloc to create ** pointers to fill the array and then will print the values pointed ** by those pointers */ #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 void free_ptrs(int **p, size_t n); int main(void) { int i; int *arr_p[MAXSIZE]; int **pp; puts("/* BEGIN new.c output */\n"); for (i = 0; i < MAXSIZE - 1; ++i ) { arr_p[i] = malloc(sizeof *arr_p[i]); if (arr_p[i] == NULL) { printf("arr_p[%d] == NULL\n", i); break; } *arr_p[i] = i; } arr_p[i] = NULL; for (pp = arr_p; *pp != NULL; ++pp) { printf("**pp = %d\n", **pp); } free_ptrs(arr_p, i); puts("\n/* END new.c output */"); return 0; } void free_ptrs(int **p, size_t n) { while (n-- != 0) { free(*p); *p++ = NULL; } } /* END new.c */ |
|
#7
|
|
|
|
|
arnuld wrote:
Path: news.motzarella.org!motzarella.org!newsfeed.straub-nv.de! goblin1!goblin2!goblin.stu.neva.ru!news.net.uni-c.dk!dotsrc.org! filter.dotsrc.org!news.dotsrc.org!not-for-mail From: arnuld <NoSpam> Ah! I see that you've taken precisely the inverse of the advice at one of the links I gave your earlier. Are you aware that this bit of smartness could get you banned by your news server? I'm not sure, but I also think that Microsoft *could* sue you. Mind you, not that they're going to ever bother with such minute plankton, when they have bigger fish to fry. You have no legal right to use that email address unless you are an employee of Microsoft and have received their permission to do so. Subject: Re: printing values of "arrays of pointers" Date: Fri, 02 May 2008 19:05:36 +0500 User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's cleansing moment of clarity.) Message-Id: <pan.2008.05.02.14.05.27.83095> Newsgroups: comp.lang.c References: <pan.2008.05.02.13.37.10.570440> <8uGdnZYzZpxJhIbVnZ2dnUVZ8selnZ2d> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Lines: 56 Organization: SunSITE.dk - Supporting Open source NNTP-Posting-Host: 121.246.233.222 X-Trace: news.sunsite.dk DXC=_V [g3R;E0oDTKD0DTdP>YSB=nbEKnk;L?8>K\5E[I;]`AOkTH[3o6lP<B6S 1\>;C<okYMidCE<;]_OU6jcM6:f60l:OJoZT67UB^cTY:nb4 X-Complaints-To: staff Xref: news.motzarella.org comp.lang.c:30654203 |
|
#8
|
|
|
|
|
"santosh" <santosh.k83> wrote in message
news:mht1 > arnuld wrote: > > Path: news.motzarella.org!motzarella.org!newsfeed.straub-nv.de! > goblin1!goblin2!goblin.stu.neva.ru!news.net.uni-c.dk!dotsrc.org! > filter.dotsrc.org!news.dotsrc.org!not-for-mail > From: arnuld <NoSpam> > You have no legal right to use that email address unless you are an > employee of Microsoft and have received their permission to do so. Don't know the OP's newsreader but: if you open a news account with Outlook Express, it does ask: "Email address: ..... for example: someone" Could someone really be sued for following this advice too literally? -- Bartc |
|
#9
|
|
|
|
|
In article <fvh64c$mht$1>,
santosh <santosh.k83> wrote: >You have no legal right to use that email address unless you are an >employee of Microsoft and have received their permission to do so. Do you have any basis for this claim? I don't recall any laws being passed on the subject. Trademark law isn't relevant, since he's not engaged in trade. He is clearly not using their name for any fraudulent purpose. He's not misrepresenting himself (even if that were illegal) since no-one would take it as a real email address. RFCs have no legal force. Nothing stops me from going around wearing a badge saying "Bill Gates, Microsoft", unless I do it for some illegal purpose. Why should a header in a news posting be any different? At most he might be violating his ISP's terms and conditions. -- Richard |
|
#10
|
|
|
|
|
Richard Tobin wrote:
> In article <fvh64c$mht>, > santosh <santosh.k83> wrote: >> Do you have any basis for this claim? I don't recall any laws being > passed on the subject. Trademark law isn't relevant, since he's not > engaged in trade. He is clearly not using their name for any > fraudulent purpose. He's not misrepresenting himself (even if that > were illegal) since no-one would take it as a real email address. > RFCs have no legal force. > > Nothing stops me from going around wearing a badge saying "Bill Gates, > Microsoft", unless I do it for some illegal purpose. Why should a > header in a news posting be any different? > > At most he might be violating his ISP's terms and conditions. I don't know about other countries, but I believe we have rules in our so-called "Cyber Laws" that prohibit willfull impersonation and misrepresentation. This would be particularly applicable if arnuld were to comment upon Microsoft under this false email address. And why would no one take it as a real email address? It's a perfectly valid domain and hostname, though the latter may not exist. It's not a proper munged address, nor an invalid address. More practically Sunsite could terminate his account with them if they were to be alerted to his behaviour. Their rules clearly say that users are only to use their own email addresses which actually exist and can be delivered to, in the Sender field. |
|
#11
|
|
|
|
|
santosh wrote, On 03/05/08 11:51:
> Richard Tobin wrote: > >> In article <fvh64c$mht$1>, >> santosh <santosh.k83> wrote: >> >>> You have no legal right to use that email address unless you are an >>> employee of Microsoft and have received their permission to do so. >> Do you have any basis for this claim? I don't recall any laws being >> passed on the subject. Trademark law isn't relevant, since he's not <snip> > I don't know about other countries, but I believe we have rules in our > so-called "Cyber Laws" that prohibit willfull impersonation and > misrepresentation. This would be particularly applicable if arnuld were > to comment upon Microsoft under this false email address. And why would > no one take it as a real email address? It's a perfectly valid domain > and hostname, though the latter may not exist. It's not a proper munged > address, nor an invalid address. Also some of us (well, me anyway) deliberately use addresses in our domains that look like spamtraps. I've found that spam@ gets spammed less than other addresses I've used. > More practically Sunsite could terminate his account with them if they > were to be alerted to his behaviour. Their rules clearly say that users > are only to use their own email addresses which actually exist and can > be delivered to, in the Sender field. Ah well, I'm sure at some point someone will decide to report him to them if he continues. |
|
#12
|
|
|
|
|
arnuld said:
>> On Sat, 03 May 2008 16:21:35 +0530, santosh wrote: >>> More practically Sunsite could terminate his account with them if they >> were to be alerted to his behaviour. Their rules clearly say that users >> are only to use their own email addresses which actually exist and can >> be delivered to, in the Sender field. >> If I use my real email ID that means I am inviting 100% of spam. I don't > want to be spammed and no one wants to be spammed. Nobody wants to be spammed. On the other hand, how would you like it if someone used *your* real address in *their* From field? All you have to do is invent an address that is in the correct form but guaranteed not to exist. For example, spamlid is a well-formed email address that definitely doesn't exist (because there is no top-level domain called "invalid"). This is not an area where originality is terribly important. If in doubt, use spamlid for From and Reply-To, and mung your real email address in your sig block. |
|
#13
|
|
|
|
|
arnuld wrote:
>> Richard Heathfield wrote: > .... snip ... > >> All you have to do is invent an address that is in the correct >> form but guaranteed not to exist. For example, spamlid >> is a well-formed email address that definitely doesn't exist >> (because there is no top-level domain called "invalid"). This is >> not an area where originality is terribly important. If in doubt, >> use spamlid for From and Reply-To, and mung your real >> email address in your sig block. > > how about my present address ? It doe not use any real domain > name and is different enough from common words like spam, invalid, > nospam etc. No. People can create new domains and addresses at any time. The ..invalid domain is guaranteed not to exist, and thus to never cause problems. |
|
#14
|
|
|
|
|
> On Sat, 03 May 2008 16:21:35 +0530, santosh wrote:
> More practically Sunsite could terminate his account with them if they > were to be alerted to his behaviour. Their rules clearly say that users > are only to use their own email addresses which actually exist and can > be delivered to, in the Sender field. If I use my real email ID that means I am inviting 100% of spam. I don't want to be spammed and no one wants to be spammed. Regarding microsoft.com, I got that idea from my friend. He told me that when he is using Outlook Express on WindowsXP, he gets this: ....@microsoft.com, as default address. |
|
#15
|
|
|
|
|
> On Sat, 03 May 2008 16:21:35 +0530, santosh wrote:
> More practically Sunsite could terminate his account with them if they > were to be alerted to his behaviour. Their rules clearly say that users > are only to use their own email addresses which actually exist and can > be delivered to, in the Sender field. I think this Server Policy is good: [url down] |
|
|
|
|
| Similar Threads | |
| Thread | Thread Starter |
| "parameter values not valid" printing in Outlook I am trying to print email messages from OUtlook and I started getting a message that says, "Could not complete this operation. On or more parameter vales are not valid." ... |
Fred |
| Mixed value results when hash key values contain special characters "$", "\" or "_" I have a hash of hashes. Some of the values that I put into hash keys have "special" characters in them (like "\" or "_"). When these values end up as keys in a hash, I get... |
funnybroad |
| Finally, a release candidate of the "Pointers" document (introduction to pointers) Not yet perfect, but: [..] [..] To access the table of contents, use the "Bookmarks" tab in Adobe Acrobat. Comments, corrections, praise, nits, etc., are still welcome! |
Alf P. Steinbach |
| Finally, a release candidate of the "Pointers" document (introduction to pointers) Not yet perfect, but: [..] [..] To access the table of contents, use the "Bookmarks" tab in Adobe Acrobat. Comments, corrections, praise, nits, etc., are still welcome! |
Alf P. Steinbach |
| "expandable" arrays - using pointers ... How to ? Hi, I want to create a container structure (array/list etc), that is expandable. Currently, I have static arrays like this SomeStruct *ptrArray[MAX_NUM_STRUCT] ; What I... |
Alfonso Morra |
|
Privacy Policy | All times are GMT. The time now is 04:22 PM.
|
|
|