问题列表 - 第13145页

如何检查文件是否存在并且在C++中是否可读?

我有一个fstream my_file("test.txt"),但我不知道test.txt是否存在.如果它存在,我想知道我是否也可以阅读它.怎么做?

我用的是Linux.

c++ linux fstream file exists

63
推荐指数
6
解决办法
8万
查看次数

可可64位二进制文​​件泄漏内存?(释放NSData不释放内存)

我一直在玩我的应用程序的不同版本,似乎有些奇怪的事情要发生:

我的应用程序有5mb空闲的足迹.当保存上传文件大小的文件时.上传后,应释放保留的内存.现在构建中存在差异(gc =垃圾收集器):

  • 32位i386 no-GC:立即释放所有内存.
  • 32位i386 GC:几乎所有内存都立即被释放.其余的一段时间后.
  • 64位x86_64 no-GC:释放最小内存.像10%
  • 64位x86_64 GC:根本没有内存被释放.记忆保持数小时.(活动星期一)

我正在使用LLVM和CLANG.我一直在运行今天的仪器,并检查泄漏/僵尸/等.一切似乎都很干净.(该应用程序相当简单.)

这种行为有解释吗?


更新:

那是一些奇怪的东西.我把问题归结为:

我将一个20mb的文件加载到NSData中并释放它.我这样做没有启用任何垃圾收集.代码是:

NSData *bla = [[NSData alloc] initWithContentsOfFile:@"/bigshit"];
[bla release];
Run Code Online (Sandbox Code Playgroud)

当我为i386 32bit构建时,20mb被分配并释放.当我将构建切换到64位x86_64时,版本什么都不做.20mb住宿分配.

上部图片32位以下64 http://kttns.org/zguxn

两个应用程序之间没有区别,除了上面的一个是32位而下面的64位.没有GC运行.(启用GC后,会出现同样的问题.)


更新2:

当我从头开始创建一个新的cocoa应用程序时,可以观察到相同的行为,只有applicationDidFinishLaunching中的高位代码:在64位模式下,内存不会被释放.i386按预期工作.

NSString而不是NSData出现同样的问题.当我启动64位内核时,它也会出现.(启动时保持64位.)

操作系统是10.6.0

64-bit cocoa memory-leaks objective-c nsdata

13
推荐指数
1
解决办法
1393
查看次数

在C中连接字符串,哪种方法更有效?

我遇到了这两种连接字符串的方法:

共同部分:

char* first= "First";
char* second = "Second";
char* both = malloc(strlen(first) + strlen(second) + 2);
Run Code Online (Sandbox Code Playgroud)

方法1:

strcpy(both, first);
strcat(both, " ");       // or space could have been part of one of the strings
strcat(both, second);
Run Code Online (Sandbox Code Playgroud)

方法2:

sprintf(both, "%s %s", first, second);
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,内容both都是"First Second".

我想知道哪一个更有效(我必须执行几个连接操作),或者如果你知道更好的方法来做到这一点.

c string performance concatenation

56
推荐指数
4
解决办法
13万
查看次数

C++继承/ VTable问题

更新:使用直接方法调用示例替换析构函数示例.

嗨,

如果我有以下代码:

class a
{
public:
    virtual void func0(); // a has a VTable now
    void func1();
};
class b : public a
{
public:
    void func0() { a::func0(); }
    void func2();
};
Run Code Online (Sandbox Code Playgroud)
  1. B中有VTable吗?B没有虚函数,但从b :: func0()调用:: func0()
  2. func1是否驻留在VTable中?这不是虚拟的.
  3. func2是否驻留在VTable中?
  4. 如果在b :: func0()中没有aa :: func0()调用,上述答案是否会有所不同?

谢谢

c++ virtual inheritance class vtable

10
推荐指数
2
解决办法
2637
查看次数

rails - activerecord ...抓住第一个结果

我想从表中获取最新的条目.如果我只是使用sql,你可以做到

Select top 1 * from table ORDER BY EntryDate DESC
Run Code Online (Sandbox Code Playgroud)

我想知道是否有一个良好的积极记录方式来做到这一点.
我可以这样做:

table.find(:order => 'EntryDate DESC').first
Run Code Online (Sandbox Code Playgroud)

但似乎会抓取整个结果集,然后使用ruby来选择第一个结果.我希望ActiveRecord创建只带来一个结果的sql.

activerecord ruby-on-rails

4
推荐指数
1
解决办法
8003
查看次数

PHP:Switch的两个值?

如果"汽车"或"法拉利"作为输入,它应该打印"汽车或法拉利".我怎样才能实现它?

<?php
$car ='333';
switch($car)
{

        case car OR ferrari:
                print("car or ferrari");
                break;
        case cat:
                print("cat");
                break;
        default:
                print("default");
                break;
}
?>
Run Code Online (Sandbox Code Playgroud)

php switch-statement

5
推荐指数
2
解决办法
1万
查看次数

具有基于属性的随机访问的对象集合的Python数据结构

我需要一组对象,这些对象可以通过每个对象共有的某个(唯一)属性进行查找.现在我正在使用一个dicitionary将字典键分配给属性.这是我现在拥有的一个例子:

class Item():
    def __init__(self, uniq_key, title=None):
        self.key = uniq_key
        self.title = title

item_instance_1 = Item("unique_key1", title="foo")
item_instance_2 = Item("unique_key3", title="foo")
item_instance_3 = Item("unique_key2", title="foo")

item_collection = {
        item_instance_1.key: item_instance_1,
        item_instance_2.key: item_instance_2,
        item_instance_3.key: item_instance_3
        }

item_instance_1.key = "new_key"
Run Code Online (Sandbox Code Playgroud)

现在这似乎是一个相当麻烦的解决方案,因为密钥不是对属性的引用,而是在赋值时获取key-attribute的值,这意味着:

  • 字典的键复制已经以对象属性和形式存在的信息
  • 当对象属性被更改时,字典键不会更新.

使用列表并迭代对象似乎效率更低.

那么,对于这种特殊情况,是否有比dict更合适的数据结构,一组对象给我随机访问基于某个对象属性?

这需要与Python 2.4一起使用,因为这就是我所困扰的(在工作中).

如果不是很明显,我是Python的新手.

python

4
推荐指数
1
解决办法
3577
查看次数

ASP.NET用户控件:Page_Load在设置属性之前触发

这真让我抓狂.

我有一个非常简单的用户控件:

public int? ImageId {set; get;}

protected void Page_Load(object sender, EventArgs e)
{
     ... do something with ImageId...
}
Run Code Online (Sandbox Code Playgroud)

然后我将此控件放在UpdatePanel中的ListView页面上:

<asp:ListView ID="ListViewImages"  runat="server" DataSourceID="src">
  <LayoutTemplate>
    <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
  </LayoutTemplate>
  <ItemTemplate>
    <My:MyControl ImageId='<%# Eval("Id") %>' ID="cipPreview" runat="server"  />
  </ItemTemplate>
</asp:ListView>
Run Code Online (Sandbox Code Playgroud)

问题是Page_Load在ASP.NET设置ImageId之前触发.在调试器的帮助下,我发现由于某种原因,MyControl中的ImageId是SET,但只有在Page_Load完成处理后才会发生.怎么了?

c# asp.net properties pageload

10
推荐指数
1
解决办法
1万
查看次数

XSLT函数和命名空间

我是XSLT的新手,我已经完成了基本的转换.接下来我想尝试日期操作,因为我的数据将有时间戳.但是,我似乎无法使用任何日期功能,这让我非常沮丧.我正在使用Firefox 3.5,xsltproc 1.1.24,xalan 1.10和XMLSpy 2009进行测试,他们都说我尝试使用的功能不存在.

我的xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="datetime.xsl"?>

<watcher>
  <event id="1" date="2009-09-04T13:49:10-0500" type="ABCD">This is a test  </event>
</watcher>
</code>
Run Code Online (Sandbox Code Playgroud)

我的xsl看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:template match="event[@type='ABCD']">
<!--            Date: <xsl:value-of select="day-from-dateTime(xs:dateTime(@date))"/> -->
<!--            Date: <xsl:value-of select="day-from-dateTime(@date)"/> -->
                Date: <xsl:value-of select="fn:day-from-dateTime(@date)"/>
</xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

如果我制作样式表版本2,XMLSpy会抱怨它无法投射我的日期: XSLT 2.0 Debugging Error: Error in XPath 2.0 expression (Cast failed, invalid lexical value - xs:dateTime '2009-09-04T13:49:10-0500')

如果我将其保留为版本1,它会抱怨不同的错误: XSLT 1.0 Debugging Error: Error in XPath expression (Unknown function - Name …

xslt xpath

5
推荐指数
1
解决办法
1万
查看次数

这是一个有效的XML吗?

我觉得这个XML无效,有人可以解释一下原因吗?

我认为它有点东西这个点我的元素名称?

estate_price.price_suggestion

还有什么其他的东西对这个XML无效吗?

XML

 \\ <?xml version="1.0" encoding="UTF-8"?>

<iad>
  <DataTag>
    <element id="0">
      <changed_string>content</changed_string>
      <no_of_bedrooms>content</no_of_bedrooms>
      <published_string>content</published_string>
      <mmo>content</mmo>
      <postcode>content</postcode>
      <utmx>content</utmx>
      <utmy>content</utmy>
      <disposed>content</disposed>
      <property_type>content</property_type>
      <isprivate>content</isprivate>
      <heading>content</heading>
      <published>content</published>
      <estate_price.price_suggestion>content</estate_price.price_suggestion>
      <ownership_type>content</ownership_type>
      <estate_size.useable_area>content</estate_size.useable_area>
      <adid>content</adid>
      <address>content</address>
      <sqmtrprice>content</sqmtrprice>
      <estate_size.primary_room_area>content</estate_size.primary_room_area>
      <location>content</location>
      <changed>content</changed>
      <orgname>content</orgname>
    </element>
    <element id="1">
      <changed_string>content</changed_string>
      <no_of_bedrooms>content</no_of_bedrooms>
      <published_string>content</published_string>
      <mmo>content</mmo>
      <postcode>content</postcode>
      <utmx>content</utmx>
      <utmy>content</utmy>
      <disposed>content</disposed>
      <property_type>content</property_type>
      <isprivate>content</isprivate>
      <heading>content</heading>
      <published>content</published>
      <estate_price.price_suggestion>content</estate_price.price_suggestion>
      <ownership_type>content</ownership_type>
      <estate_size.useable_area>content</estate_size.useable_area>
      <adid>content</adid>
      <address>content</address>
      <sqmtrprice>content</sqmtrprice>
      <estate_size.primary_room_area>content</estate_size.primary_room_area>
      <location>content</location>
      <changed>content</changed>
      <orgname>content</orgname>
    </element>
  </DataTag>
</iad>
Run Code Online (Sandbox Code Playgroud)

xml schema xsd dtd standards-compliance

1
推荐指数
1
解决办法
255
查看次数