Working in Unix but not in windows

S

sanjaymeher

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);

}
 
A

ashwin

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
 
S

sanjaymeher

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
 
A

Artie Gold

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 said:
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
Once you've done all that, please post back if you run into problems.

HTH,
--ag
 
S

sanjaymeher

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

ashwin

Can you atleast tell me the exact error messages as I can compile and
run the code in borland

Ashwin
 
J

JimS

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
 
G

Gordon Burditt

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
 
H

haroon

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).
 
R

Richard Bos

haroon said:
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
 
R

Richard Heathfield

haroon said:
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.
 
R

Richard Bos

Richard Heathfield said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top