标签: pointers

C:释放每一个malloc还是只放一个主要的?

我有一个指针***resultSet,我将其作为参数传递给我的SQL函数,该函数从数据库中读取未知数量的数据.数据的实际处理发生在函数之外.

清理这个指针构造时,我只需释放一次主指针,free就可以找到所有后续分配,还是我必须释放我创建的每个malloc?

while((row = mysql_fetch_row(result))) {

                // allocating the rows pointer
                resultSet = malloc(sizeof(void)*(int)mysql_num_rows);
                (*rows)++;

                for (i=0 ; i < mysql_num_fields(result); i++)               
                {
                // allocating the fields pointer
                *(resultSet+i) = malloc(sizeof(void)*(int)mysql_num_fields);

                // allocating the character pointer
                **resultSet = malloc(sizeof(char)*strlen(row[i])+1);
                (*fields)++;

                snprintf(**resultSet, strlen(row[i])+1, "%s", row[i]);
                printf("%s\t",**resultSet);
                if (i==6)
                {
                printf("\t %s\n",**resultSet);
                }
                }
    }
Run Code Online (Sandbox Code Playgroud)

c malloc free pointers

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

使用指针缩小C++中的类型转换

我在C++中使用指针进行向下类型转换时遇到了一些问题,在我想出这样做之前,谷歌基本上告诉我这是不可能的,而且我从C++学到的任何书都没有涉及到.我认为这会起作用......

long int TheLong=723330;
int TheInt1=0;
int TheInt2=0;
long int * pTheLong1 = &TheLong;
long int * pTheLong2 = &TheLong + 0x4;

TheInt1 = *pTheLong1;
TheInt2 = *pTheLong2;

cout << "The double is " << TheLong << " which is "
     << TheInt1 << " * " << TheInt2 << "\n";
Run Code Online (Sandbox Code Playgroud)

第五行的增量可能不正确,但输出让我担心我使用gcc 3.4.2的C编译器会自动将TheInt1转换为long int或其他东西.输出看起来像这样......

双倍是723330,即723330*4067360

TheInt1的输出不可能高,并且没有TheInt2的输出.

我有三个问题......

我是否走在正确的轨道上?

第五行的适当增量是多少?

为什么地狱是TheInt1/TheInt2允许如此大的价值?

c++ pointers type-conversion

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

指向其他向量元素的指针向量

EI具有作为向量的参数指针的函数:

void Function(std::vector<type>* aa)
Run Code Online (Sandbox Code Playgroud)

现在在这个函数里面我想过滤掉从那个向量到另一个向量的数据,我想通过改变这个临时值的值来改变原始向量的数据.该死的很难理解:

void Function(std::vector<type>* aa)
{
    std::vector<type*> temp; //to this vector I filter out data and by changning 
    //values of this vector I want to autmatically change values of aa vector
}
Run Code Online (Sandbox Code Playgroud)

我有类似的东西:

void Announce_Event(std::vector<Event>& foo)
{
    std::vector<Event> current;
    tm current_time = {0,0,0,0,0,0,0,0,0};
    time_t thetime;
    thetime = time(NULL);
    localtime_s(&current_time, &thetime);
    for (unsigned i = 0; i < foo.size(); ++i) {
        if (foo[i].day == current_time.tm_mday &&
            foo[i].month == current_time.tm_mon &&
            foo[i].year == current_time.tm_year+1900)
        {
            current.push_back(foo[i]);
        }
    }
    std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ pointers vector

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

单链表C++

嘿伙计们,我正在研究期中考试,正在努力尝试使用单一链表创建一个简单的程序.我想要它做的是在列表中插入"1","2","3","4"并打印出来.请看下面的代码:

#include <iostream>
#include <string>
using namespace std;

class node{
public:
    node(int data);
    friend class slist;
private:
    int data;
    node *next;
};

node::node(int data){
    data = data;
    next = NULL;
}

class slist{
public:
    slist(){
        head = NULL;
    }
    void insert(int item);
    void output();

private:
    node* head;
};   

void slist::insert(int item){
    node* newnode = new node(item);
    if(head == NULL)
    {
        head = newnode;
    }
    else
    {
        newnode->next = head;
        head = newnode;
    }
}

void slist::output(){
    node* p = head; …
Run Code Online (Sandbox Code Playgroud)

c++ pointers linked-list

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

将指针传递给结构

嗨,我有一个函数A ( xy * abc),指向结构的指针.

typedef struct
{
int a;
char * b;
} xy;

typedef struct
{
xy c;
xy d;
} uv;

uv *sha;
Run Code Online (Sandbox Code Playgroud)

如果我需要调用函数Acd使用uv,我应该如何传递参数?我A用这个来调用函数:

A (&sha->c);
A (&sha->d);
Run Code Online (Sandbox Code Playgroud)

这个电话是否正确?

请帮助我

c c++ pointers structure

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

来自指针回溯的C++分段错误

我目前正在获得一个seg错误,并且它从我认为是main中的一行开始,在gdb中进行回溯之后,我基本上可以找到它,但我不确定需要改变什么.这是我按顺序看到的地方:

首先是主要:

DeckOps *deck = new DeckOps(filename);
Run Code Online (Sandbox Code Playgroud)

我相信是导致它的线,回溯也包括

class DeckOps{
 public:
  DeckOps(string filename);
  ~DeckOps();

 private:
  dlist *deck;
}
Run Code Online (Sandbox Code Playgroud)

然后是.cpp文件

DeckOps::DeckOps(string filename){

  ifstream inF;

  inF.open(filename.c_str());

  if (inF.fail()){
    cerr << "Error opening file" << endl;
    exit(1);
  }
  int deckcount = 28;
  int card;
  for(int i = 0; i <= deckcount; i++){
    inF >> card;
    deck->insertRear(card);
  }
  inF.close();
}
Run Code Online (Sandbox Code Playgroud)

然后最后是最后一个地方

void dlist::insertRear(int d){
  LinkNode *link = new LinkNode();
  int *nd = new int();
  *nd = d;
  link->data= nd;

  if(first == 0){
    first = …
Run Code Online (Sandbox Code Playgroud)

c++ pointers linked-list segmentation-fault

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

将内存分配给类的指针

QGeoRoutingManager:http://apidocs.meego.com/1.0/qtmobility/qgeoroutingmanager-members.html

这个类没有构造函数.我忘记了为其指针分配内存的方法.

我做了:

QGeoRoutingManager *a = new QGeoRoutingManager ();
Run Code Online (Sandbox Code Playgroud)

这归因于错误:

calculateRoute.cpp:16: error: no matching function for call to ‘QtMobility::QGeoRoutingManager::QGeoRoutingManager()’
../../../../tarBalls/qt-mobility-opensource-src-1.2.0/install/include/QtLocation/qgeoroutingmanager.h:91: note: candidates are: QtMobility::QGeoRoutingManager::QGeoRoutingManager(const QtMobility::QGeoRoutingManager&)
Run Code Online (Sandbox Code Playgroud)

我应该传递什么,根据错误消息"const QtMobility :: QGeoRoutingManager&"

c++ qt pointers qt-mobility

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

在Objective-C代码中从不兼容的指针类型分配

昨天,我复制并编译了下面的代码,很好.但今天当我编译代码时,它给了我一个警告,不会运行.exe.我是Objective-C的新手,我在窗口上使用GNUstep.

testString.m: In function 'main':
testString.m:5:13: warning: assignment from incompatible pointer type
** testString.m:5:13 it front of (=)
Run Code Online (Sandbox Code Playgroud)

这是代码.

//testString.m
#import <Foundation/Foundation.h>
int main (int argc,  const char * argv[])
{
NSString *testString = [[NSString alloc] init ];
testString = "Here's a test string in testString!";
NSLog(@"testString: %@", testString);

return 0;
}
Run Code Online (Sandbox Code Playgroud)

types pointers objective-c variable-assignment

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

指针混乱

我是C++的新手,我遇到了一个我不明白的错误.

class1* a = (class1*)p1;
class2* b = (class2*)p2;
a->foo(b);
Run Code Online (Sandbox Code Playgroud)

错误是:

error: no matching function for call to 'a::foo(b*&)'
note: candidates are: void a::foo(const b&)
Run Code Online (Sandbox Code Playgroud)

我怎么做到这一点?

c++ pointers

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

这会导致内存泄漏吗?

好吧,所以我和其他人有些分歧,我希望有人比我们中的任何一个人都更了解c ++可以解决这个问题.假设我们在函数内部的某个代码块(对于tilemap引擎):

void loadTiles()
{
Tile* tile = new Tile();
Level->addTile(x, y, tile); //x and y are just arbitrary ints.
/* when addTile is called, it fills the values of the chunk of memory pointed to by tile to the predefined chunk of memory created in the Level object. */
//Then, to remove the dangling pointer safely,
tile = NULL;
} //Then the actual memory pointed to by tile is deallocated here.
Run Code Online (Sandbox Code Playgroud)

Level类有一个名为map [] []的2D Tile数组,它的addTile函数看起来完全像这样:

void Level::addTile(int x, int …
Run Code Online (Sandbox Code Playgroud)

c++ memory pointers memory-leaks allocation

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