Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Working in Unix but not in windows

Reply
Thread Tools

Working in Unix but not in windows

 
 
sanjaymeher@gmail.com
Guest
Posts: n/a
 
      12-28-2005
This piece of code is not working in windows but working in Unix Not
getting the reason .. help me out

#include <iostream.h>
#include <stdio.h>
#include <string.h>

int addDynamicMemory(char **ptr, int size);

int addDynamicMemory(char **ptr, int size)
{
int currSize;
currSize = strlen(*ptr);
size = currSize + size;
printf("before re Allocation size is %d\n",size);
*ptr = (char*) realloc(*ptr, size*sizeof(char));
printf("memory allocated \n");
return 0;
}

int main(int argc, char* argv[])
{
char **test;
char *test1;
ws1_c = "san jay";

test = &test1;
*test = (char*) malloc(1*sizeof(char)); // If U will make it *test =
(char*) malloc(10*sizeof(char)) It will work .... amazing ..
strcpy(*test,"TEST_11");
printf("Curr val of one %s\n",*test);

*test = (char*) realloc(*test, 20*sizeof(char));
strcat(*test,"3333333333");
printf("After allocation val is %s\n",*test);

addDynamicMemory(test, 40) ;
strcat(*test,"444444444");
printf("After allocation val is %s\n",*test);

addDynamicMemory(test, 20) ;
strcat(*test,"7777777");
printf("After allocation val is %s\n",*test);

addDynamicMemory(test, 20) ;
strcat(*test,"888888888888888");
printf("After allocation val is %s\n",*test);

}

 
Reply With Quote
 
 
 
 
ashwin
Guest
Posts: n/a
 
      12-28-2005
Hi,
Chk this code and let me know if this is what you want...otherwise I
might have changed the wrong things. If that is what you wanted I can
explain what is wrong with your code on windows

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int addDynamicMemory(char **ptr, int size);

int addDynamicMemory(char **ptr, int size)
{
int currSize;
currSize = strlen(*ptr);
size = currSize + size;
printf("before re Allocation size is %d\n",size);
*ptr = (char*) realloc(*ptr, size*sizeof(char));
printf("memory allocated \n");
return 0;

}

//int main(int argc, char* argv[])
void main()
{
char **test;
char *test1;
//ws1_c = "san jay";

test = &test1;
*test = (char*) malloc(1*sizeof(char)); // If U will make it *test =
(char*) malloc(10*sizeof(char)) It will work .... amazing ..
strcpy(*test,"TEST_11");
printf("Curr val of one %s\n",*test);

*test = (char*) realloc(*test, 20*sizeof(char));
strcat(*test,"3333333333");
printf("After allocation val is %s\n",*test);

addDynamicMemory(test, 40) ;
strcat(*test,"444444444");
printf("After allocation val is %s\n",*test);

addDynamicMemory(test, 20) ;
strcat(*test,"7777777");
printf("After allocation val is %s\n",*test);

addDynamicMemory(test, 20) ;
strcat(*test,"888888888888888");
printf("After allocation val is %s\n",*test);

}

Ashwin

 
Reply With Quote
 
 
 
 
sanjaymeher@gmail.com
Guest
Posts: n/a
 
      12-28-2005
Hi Ashwin

Still Facing the same problem after changing the main() signature also
.... I am compiling this in MS VC++ and facing this problem..

Reply me back

Sanjay

 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      12-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> This piece of code is not working in windows but working in Unix Not
> getting the reason .. help me out
>
> #include <iostream.h>

This is not a standard C header (it's not a C++ header either, but I
digress...). Fortunately (in this context) you don't use it.

> #include <stdio.h>
> #include <string.h>

#include <stdlib.h> /* for malloc()/realloc() prototypes */
>
> int addDynamicMemory(char **ptr, int size);
>
> int addDynamicMemory(char **ptr, int size)

The definition will serve quite well as a prototype, thank you.
> {
> int currSize;
> currSize = strlen(*ptr);
> size = currSize + size;
> printf("before re Allocation size is %d\n",size);
> *ptr = (char*) realloc(*ptr, size*sizeof(char));


Do not cast the return value from *alloc functions. It is unnecessary
and can mask errors (as it has here...see above).

`sizeof(char) is 1 by definition.

*Never* directly realloc() -- it can create a memory leak if it fails.
(Always* realloc() to a temp.)

> printf("memory allocated \n");
> return 0;
> }
>
> int main(int argc, char* argv[])
> {
> char **test;
> char *test1;
> ws1_c = "san jay";
>
> test = &test1;
> *test = (char*) malloc(1*sizeof(char)); // If U will make it *test =
> (char*) malloc(10*sizeof(char)) It will work .... amazing .


Why not just use `test1'?
[...as well as same comments about casting the return value and
sizeof(char)]
..
> strcpy(*test,"TEST_11");


You've only allocated space for one `char'. What do you expect?

> printf("Curr val of one %s\n",*test);
>
> *test = (char*) realloc(*test, 20*sizeof(char));
> strcat(*test,"3333333333");
> printf("After allocation val is %s\n",*test);
>
> addDynamicMemory(test, 40) ;
> strcat(*test,"444444444");
> printf("After allocation val is %s\n",*test);
>
> addDynamicMemory(test, 20) ;
> strcat(*test,"7777777");
> printf("After allocation val is %s\n",*test);
>
> addDynamicMemory(test, 20) ;
> strcat(*test,"888888888888888");
> printf("After allocation val is %s\n",*test);
>
> }
>

Obviously, you lack a clue.
Fortunately, clues are easy to obtain. Read a good book on C
(http://www/accu/org is a good source for suggestions; K&R II is hard to
beat). Read the C-FAQ. Browse news:comp.lang.c.

Once you've done all that, please post back if you run into problems.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
 
Reply With Quote
 
sanjaymeher@gmail.com
Guest
Posts: n/a
 
      12-28-2005
Ooops Allocated for only one char and assigning,
strcpy(*test,"TEST_11"), it to seven chars is the main culprit

 
Reply With Quote
 
ashwin
Guest
Posts: n/a
 
      12-28-2005
Can you atleast tell me the exact error messages as I can compile and
run the code in borland

Ashwin

 
Reply With Quote
 
JimS
Guest
Posts: n/a
 
      12-28-2005
On 27 Dec 2005 21:26:50 -0800, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:

>This piece of code is not working in windows but working in Unix Not
>getting the reason .. help me out
>
>#include <iostream.h>


I'll ignore the above line.

>#include <stdio.h>
>#include <string.h>
>
>int addDynamicMemory(char **ptr, int size);
>
>int addDynamicMemory(char **ptr, int size)
>{
> int currSize;
> currSize = strlen(*ptr);
> size = currSize + size;
> printf("before re Allocation size is %d\n",size);
> *ptr = (char*) realloc(*ptr, size*sizeof(char));
> printf("memory allocated \n");
> return 0;
>}
>
>int main(int argc, char* argv[])
>{
> char **test;
> char *test1;
> ws1_c = "san jay";
>
> test = &test1;
> *test = (char*) malloc(1*sizeof(char));


Here you've allocated 1 char of space.

/* If U will make it *test = (char*) malloc(10*sizeof(char)) It will
work .... amazing .. */

Not really...

> strcpy(*test,"TEST_11");
> printf("Curr val of one %s\n",*test);


Here you copy 8 chars into your 1 char of space.

Three more notes
1) sizeof(char) by definition is always 1
2) don't cast the results of malloc and realloc. It usully hides a
missing #include <stdlib.h> (which is missing in your example).
3) You should always check the return result of malloc and realloc for
success.

Jim
 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      12-28-2005
>This piece of code is not working in windows but working in Unix Not
>getting the reason .. help me out
>
>#include <iostream.h>
>#include <stdio.h>
>#include <string.h>
>
>int addDynamicMemory(char **ptr, int size);
>
>int addDynamicMemory(char **ptr, int size)
>{
> int currSize;
> currSize = strlen(*ptr);


The memory needed for a string of strlen() N is N+1, *not* N (the
'\0' terminator requires space, too). Passing strlen(something)
rather than strlen(something)+1 to malloc() or realloc() is a red
flag that you're going to write on more memory than you allocated.

> size = currSize + size;
> printf("before re Allocation size is %d\n",size);
> *ptr = (char*) realloc(*ptr, size*sizeof(char));
> printf("memory allocated \n");
> return 0;
>}

Gordon L. Burditt
 
Reply With Quote
 
JimS
Guest
Posts: n/a
 
      12-28-2005
On 27 Dec 2005 22:16:32 -0800, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:

>Ooops Allocated for only one char and assigning,
>strcpy(*test,"TEST_11"), it to seven chars is the main culprit


eight.

Jim
 
Reply With Quote
 
haroon
Guest
Posts: n/a
 
      12-28-2005

(E-Mail Removed) wrote:
> Ooops Allocated for only one char and assigning,
> strcpy(*test,"TEST_11"), it to seven chars is the main culprit


But why did it work properly on Unix and not on Windows? Can any body
explain this? I tried this code on linux and it works fine there as
well (with space for 1 character allocated).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Net::HTTP working on Windows, but not Unix James Edward Gray II Ruby 3 03-03-2006 02:21 AM
Cookies working on intranet but NOT working on Internet Martin Heuckeroth ASP .Net 5 04-01-2005 01:37 AM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C Programming 18 01-10-2005 03:35 AM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C++ 12 01-10-2005 03:35 AM
my own perl "dos->unix"/"unix->dos" Robert Wallace Perl Misc 7 01-22-2004 10:59 PM



Advertisments