473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Working in Unix but not in windows

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
12 1415
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
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
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
Ooops Allocated for only one char and assigning,
strcpy(*test,"TEST_11"), it to seven chars is the main culprit

Dec 28 '05 #5
Can you atleast tell me the exact error messages as I can compile and
run the code in borland

Ashwin

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

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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
6
by: Matthew | last post by:
How would I go about creating a simple "hello world" program that will run in Unix. I am using MS Visual C++.
6
by: Shaun Heveron | last post by:
Hi, I'm working on a portable ANSI C++ application designed to compile and execute under Windows and LINUX/UNIX. Is there a portable way of obtaining the current working directory? Sorry, if...
22
by: Ryan M | last post by:
I've been programming for a while, but most of my experience is on unix. How do C compilers work on operating systems that weren't written in C? And that have no libc? Compiling C on unix seems...
18
by: Sharon | last post by:
is microsoft going to develop .Net for Unix? or at lest CLR for Unix? 10x
4
by: knapak | last post by:
Hello I'm a self instructed amateur attempting to read a huge file from disk... so bear with me please... I just learned that reading a file in binary is faster than text. So I wrote the...
14
by: gio | last post by:
I have a problem and the solution should works under windows and unix OS. Suppose I have a program ex.c in the directory X (so the current working directory of ex.c is X). Also suppose I have...
4
by: BT | last post by:
I just started working with some Server Side Includes to streamline a website that I inherited. I think that I've got the SSI stuff figured out, but it is a pain to work on. When I edit an SSI, I...
1
RRick
by: RRick | last post by:
I have a unix C++ project that needs to be converted over to windows visual studio. I'm not sure of the exact version of VS, but it's a recent version, probabIy 2003 or 2005. I would like the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.