Allocate memory for arg[] before gets(). Use malloc().
Mandar
On Thu, 26 Sep 2002 Nikhil Joshi wrote :
Hi!
I've tried this simple prog to encrypt a file using XOR
But it gives "Segmentation Fault " in 'interactive' mode (pls see the source code)
it runs perfectly well in DOS (using Turbo C++ 3.0)
Can ny1 help pls ?
-- Nikhil Joshi
On 26 Sep 2002, Mandar Khanolkar wrote:
Allocate memory for arg[] before gets(). Use malloc().
thx guys 4 the suggestions. i've added the following code : But still i get the same error (Segmentation Fault) Also curious: How come the program runs perfectly in DOS without allocating memory ?
/* Now Allocate memory for arg[4] I dunno why the following doesn't work:
if ((arg = (char *) malloc (30)) == NULL)
Compiler gives the error: incompatible types in assignment
Is there some other method to allocate memory to array of pointers? */
if ((*arg = (char *) malloc (30)) == NULL)
{ printf ("Error allocating memory \n"); exit (1); }
P.S. I know this is not the right place to discuss C programming but guys pls bear with me ...
-- Nikhil Joshi
On Sep 27, 2002 at 07:40, Nikhil Joshi wrote:
thx guys 4 the suggestions. i've added the following code : But still i get the same error (Segmentation Fault) Also curious: How come the program runs perfectly in DOS without allocating memory ?
DOS ... I'm not sure, but I bet it has something to do with the real memory model. Either that or the Turbo C (what're you using?) compiler does the mallocing.
/* Now Allocate memory for arg[4] I dunno why the following doesn't work: if ((arg = (char *) malloc (30)) == NULL)
See your declaration, you have
char *arg[4];
Now I'm not quite sure if this is an array of pointers (I think it is), or a pointer to an array, but you have to probably do:
for(i=0;i<4;i++) { if ((arg[i] = (char *) malloc (30)) == NULL) perror("malloc"); }
Compiler gives the error: incompatible types in assignment
Naturally, you are assigning char * (the cast) to char ** (the array).
Is there some other method to allocate memory to array of pointers?
Yes.
*/ if ((*arg = (char *) malloc (30)) == NULL)
And here you're mallocing 30 bytes (you should use sizeof(char) to be completely safe, but perhaps ANSI C says that a char is always 1 byte) to arg[][].
I know this is not the right place to discuss C programming but guys pls bear with me ...
It's ok. Once you understand malloc() you'll understand pointers perfectly. I know I didn't get pointers until I understood malloc(). Actually, I grokked both at the same time. I'm now writing a program involving several structs, a call to select, a couple of calls to gettimeofday() (see the manpage), and at least 2 linked lists ::counts: msgid... the packet queue...:: yep, 2.
Okay, so it's not complicated. But have n such programs fork()ed off anotyher one, then initialised from the original based on a text .init file, and then have them do P2P networking, all in 2 weeks....
Yes, I've got C programming on the brain now.
While we're at it, do sendto() and recvfrom() send and receive a full packet, assuming it's SOCK_DGRAM (UDP)?
Satya wrote:
While we're at it, do sendto() and recvfrom() send and receive a full packet, assuming it's SOCK_DGRAM (UDP)?
Yes. UDP is supposed to preserve packet boundaries. The packet may get fragmented and reassembled at the IP layer but this is transparent (someone correct me if I'm wrong).
Manish
At 06:04 even 9/27/02 +0530, Manish wrote:
Yes. UDP is supposed to preserve packet boundaries. The packet may get fragmented and reassembled at the IP layer but this is transparent (someone correct me if I'm wrong).
Correct, IMHO. UDP provides connection less services to upper levels. It exists due to the need for a protocol which avoids the overhead of connection establishment (& release) when it is not needed. Sometimes you would like to send only one packet and get only one packet in return. TCP would be overkill here.
Yeps, IP can and mostly will fragment the TPDU (unless it is very small).
RFC 768
quasi
On Friday 27 September 2002 07:40 am, Nikhil Joshi wrote:
if ((*arg = (char *) malloc (30)) == NULL) { printf ("Error allocating memory \n"); exit (1); }
Boy, you're not advancing the "arg" pointer.... so, it is something like
arg[0] = (char *)malloc(30);
I guess, this is not what you want, right??? In this case, what you need to do is advance the arg pointer. Something like
for( i =0 ; i < 4; i++) { if((*(arg + i) = (char *)malloc(n * sizeof(char))) == NULL) { perror("Mem Allocation Error...\n"); exit(1); } }
I guess, in your earlier program, you'd done something like
for(i = 0; i < 4; i++) if((arg[i] = (char *)malloc(30)) == NULL)
I don't see any point why this should give you Segmentation fault. It seems to work for me though.... It is exactly what the above code will do, using the offset alongwith the pointer.
But, if you want a simple and better way, I suggest you use C++. The string class (which in fact is a typedef to the base_string<char> template) is simple to use. Also, it will handle the memory allocation problems internally. Refer to the documentation for more details.
Hi! success afterall ... I used following to allocate memory to arg
for (i=1;i<4;i++) if ((arg[i] = (char *) malloc (30)) == NULL) { printf ("Error allocating memory"); exit(1); }
iz there ny other method to allocate memory to array of pointers?
Hi Nikhil,
Seems that you got the solution by hit and trial. But do you understand the meaning of the code given below. If not, understand it , it may help you in understanding pointers to a great extent.
ciao tapesh
--- Nikhil Joshi nikhiljoshi@subdimension.com wrote:
Hi! success afterall ... I used following to allocate memory to arg
for (i=1;i<4;i++) if ((arg[i] = (char *) malloc (30)) == NULL) { printf ("Error allocating memory"); exit(1); }
iz there ny other method to allocate memory to array of pointers?
__________________________________________________ Do you Yahoo!? New DSL Internet Access from SBC & Yahoo! http://sbc.yahoo.com