473,320 Members | 1,845 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,320 software developers and data experts.

Pointer problem

1
Hi,

I have a structure defined as

typedef struct bcurve
{
vector<CP> cpoints;
vector<int> seg;
}BEZIERCURVE;

(where CP is another structure containing the control points for a curve)

and I'm trying to get a pointer to an array of pointers to this structure.

What I have so far is:
Expand|Select|Wrap|Line Numbers
  1. BEZIERCURVE** allcurves = (BEZIERCURVE**)calloc(h*w,sizeof(BEZIERCURVE*));
  2.  
then later on:
Expand|Select|Wrap|Line Numbers
  1. BEZIERCURVE tempbc;
  2. // tempbc is passed to a function that works out what goes in the cpoints and seg fields
  3.  
  4. allcurves[idx] = &tempbc;
  5.  
So far so good, or at least it seems to be.
I tried to get the size of the cpoints vector from allcurves[idx] and it worked fine.

But after that I need to pass allcurves to a function of another class

Expand|Select|Wrap|Line Numbers
  1. BEZIERCURVE** m_curves;
  2. int *m_imgmap;
  3. .
  4. .
  5. .
  6. void GLDraw::SetBezierCurves(BEZIERCURVE** curves, int* imgmap, int w, int h)
  7. {
  8.         m_curves = curves;
  9.     m_imgmap = imgmap;
  10. .
  11. .
  12. .
  13.         // b is from a loop
  14.         int tmp = m_curves[b]->cpoints.size();
  15. .
  16. .
  17. }
  18.  
Now the m_curves[b]->cpoints.size() just gives me 0 which is wrong.
I'm probably doing something horribly wrong with the pointers but I'm fairly new in C++...

I'd be grateful for any suggestions..
Thanks
Tania
Jan 10 '08 #1
1 948
Savage
1,764 Expert 1GB
You are using C++,so why use calloc() when you have our beloved new operator?You should allways use operator new/new[] in C++,it might be using malloc to allocate it's memory but for difference it will also call object's constructor(if you specify a object as it's type),thus fully initializing it.
Second that's not how you allocate the required memory for a ponter to a pointer to a type.You first allocate array of pointers and then for every pointer in array you allocate it's own array of type elements.Freeing it is just a matter of reversing this process.

e.g

Expand|Select|Wrap|Line Numbers
  1. int **a;
  2.  
  3. a=new int*[size1];
  4. for(i=0;i<size1;i++)
  5. {
  6.   a[i]=new int[size2];
  7. }
from this you might have notices syntax for new(if you haven't used it before).

Also take note that when calling new[](which is called when you allocate arrays} object must have it's default constructor,and every call to new/new[] must be matched with a call to delete/delete[] when you are finished using that memory inside current scope.
Jan 10 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
51
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
2
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I...
6
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.