By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

Working in Unix but not in windows

P: n/a
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);

}

Dec 28 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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

Dec 28 '05 #2

P: n/a
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

Dec 28 '05 #3

P: n/a
sa*********@gmail.com 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!"
Dec 28 '05 #4

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

Dec 28 '05 #5

P: n/a
Can you atleast tell me the exact error messages as I can compile and
run the code in borland

Ashwin

Dec 28 '05 #6

P: n/a
>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
Dec 28 '05 #7

P: n/a
On 27 Dec 2005 21:26:50 -0800, "sa*********@gmail.com"
<sa*********@gmail.com> 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
Dec 28 '05 #8

P: n/a
On 27 Dec 2005 22:16:32 -0800, "sa*********@gmail.com"
<sa*********@gmail.com> wrote:
Ooops Allocated for only one char and assigning,
strcpy(*test,"TEST_11"), it to seven chars is the main culprit


eight.

Jim
Dec 28 '05 #9

P: n/a

sa*********@gmail.com 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).

Dec 28 '05 #10

P: n/a
"haroon" <ha***********@gmail.com> wrote:
sa*********@gmail.com 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?


There is no particular "work properly" for code which exhibits undefined
behaviour. Working as if there were memory available is proper. Crashing
immediately is fine. Writing over other, possibly completely unrelated,
objects, or even over return addresses or executable code, is legal.
Appearing to work as the programmer naively expects it to on the
development machine, but crashing and burning when demonstrated on a
customer's differently kitted-out machine is also just dandy.

Richard
Dec 28 '05 #11

P: n/a
haroon said:

sa*********@gmail.com 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?


There isn't a "properly", when you step outside the rules of the language.
If you start shooting at the other players, you shouldn't be surprised if
they start behaving in a way you don't expect and can't predict. That
behaviour may well differ from one platform to another.
Can any body explain this? I tried this code on linux and it works
fine there as well (with space for 1 character allocated).


The code is broken. That Linux copes well with it is a tribute to Linux, but
the correct approach is not to try to understand why it coped well (which
will be pretty obvious to anyone who ever wrote an allocator), but to fix
your code, which is a gerharsterly mess.

Firstly, remove that silly iostream.h header, which isn't a C header and
isn't even a C++ header.
Secondly, remove every single cast.
Thirdly, replace every single instance of sizeof(char) with 1, or remove it
completely.
Fourthly, make all necessary changes in your program to reflect the fact
that strlen returns a size_t, not an int.
Fifthly, change your program so that it detects any failure to meet an
allocation request and takes appropriate remedial action.

--
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)
Dec 28 '05 #12

P: n/a
Richard Heathfield <in*****@invalid.invalid> wrote:
Firstly, remove that silly iostream.h header, which isn't a C header and
isn't even a C++ header.
Secondly, remove every single cast.
Thirdly, replace every single instance of sizeof(char) with 1, or remove it
completely.


Or if you want to be ready for when you'll start using a wide character
type, use *<the char pointer>; in this case, **ptr.

Richard
Dec 28 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.