在Unix中工作但不在Windows中工作 [英] Working in Unix but not in windows

查看:55
本文介绍了在Unix中工作但不在Windows中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码不能在windows中工作但在Unix上工作不是

得到原因..帮帮我


#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("在重新分配大小为%之前) d \ n",size);

* ptr =(char *)realloc(* ptr,size * sizeof(char));

printf(" memory已分配\ n");

返回0;

}


int main(int argc,char * argv [] )

{

char ** test;

char * test1;

ws1_c =" san jay" ;


test =& test1;

* test =(char *)malloc(1 * sizeof(char)); //如果U会成功* test =

(char *)malloc(10 * sizeof(char))它会起作用.... :(很棒..

strcpy(* test," TEST_11");

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


* test =(char *)realloc(* test,20 * sizeof(char));

strcat(* test," 3333333333");

printf( 在分配val之后是%s \ n,* test);


addDynamicMemory(test,40);

strcat(* test,& ; 444444444");

printf(" After allocation val is%s \ nn",* 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是%s \ n,*测试);


}

解决方案



Chk这段代码让我知道这是不是你想要的......否则我可能会改变错误的东西。如果那就是你想要的那样我可以用b $ b解释你的代码在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( 在重新分配大小为%d \ n之前,大小);

* ptr =(char *)realloc(* ptr,size * sizeof(char));

printf(内存已分配\ n);

返回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)); //如果U会成功* test =

(char *)malloc(10 * sizeof(char))它会起作用.... :(很棒..

strcpy(* test," TEST_11");

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


* test =(char *)realloc(* test,20 * sizeof(char));

strcat(* test," 3333333333");

printf( 在分配val之后是%s \ n,* test);


addDynamicMemory(test,40);

strcat(* test,& ; 444444444");

printf(" After allocation val is%s \ nn",* 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是%s \ n,*测试);


}


Ashwin


您好Ashwin


在更改main()签名后仍然面临同样的问题

....我在MS VC ++中编译它并面临这个问题。 。


回复我


Sanjay


sa ********* @ gmail.com 写道:

这个一段代码不能在Windows中工作,但在Unix中工作没有
得到原因..帮助我

#include< iostream.h>
这不是标准的C标题(它也不是C ++标题,但是我/ b $ b离开......)。幸运的是(在这种情况下)你不会使用它。

#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)
定义将很好地作为原型,谢谢。 {
int currSize;
currSize = strlen(* ptr);
size = currSize + size;
printf(重新分配大小为%d \ n之前, size);
* ptr =(char *)realloc(* ptr,size * sizeof(char));


不要从* alloc函数转换返回值。这是不必要的

并且可以掩盖错误(因为它在这里......见上文)。


`sizeof(char)的定义是1。 br />

*从不直接realloc() - 如果失败就会造成内存泄漏。

(总是* realloc()到temp。)

printf(" memory allocated \ n");
返回0;
}
int main(int argc,char * argv [] )
{
char ** test;
char * test1;
ws1_c =" san jay" ;;

test =& test1;
* test =(char *)malloc(1 * sizeof(char)); //如果U会成功* test =
(char *)malloc(10 * sizeof(char))它会起作用.... :(很棒。


为什么不直接使用`test1''?

[...以及关于转换返回值的相同评论和

sizeof(char)]

.. strcpy(* test," TEST_11");


你只为一个'char'分配了空间。你有什么期望?

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

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

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

addDynamicMemory(test,20);
s trcat(* test," 888888888888888");
printf(分配后val为%s \ n,* test);

}


显然,你缺乏线索。

幸运的是,线索很容易获得。阅读一本好书C

http:// www / accu / org 是一个很好的建议来源; K& R II很难获得b
)。阅读C-FAQ。浏览新闻:comp.lang.c。


完成所有这些后,如果遇到问题,请回复。


HTH,

- g

-

Artie Gold - 德克萨斯州奥斯汀
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays

如果你没有什么可隐瞒的,你没有尝试!


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

}

解决方案

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


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


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!"


这篇关于在Unix中工作但不在Windows中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆