Really basic..

D

deepak

if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;
return 0;

}

am i missing something fudamental thing...

thanks in advance.
 
V

Vladimir Oka

deepak said:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;
return 0;

}

am i missing something fudamental thing...

Yes. You're failing to allocate any memory for your data.

float **data;

only declares a pointer to where your data is, no memory is allocated.

Try:

#include <stdlib.h>
...

data = malloc( N_ROWS * N_COLS * sizeof **data);

if (data == NULL) /* do something about the error */;
...
 
V

Vladimir Oka

Vladimir said:
deepak said:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;
return 0;

}

am i missing something fudamental thing...

Yes. You're failing to allocate any memory for your data.

float **data;

only declares a pointer to where your data is, no memory is allocated.

Try:

#include <stdlib.h>
...

data = malloc( N_ROWS * N_COLS * sizeof **data);

if (data == NULL) /* do something about the error */;
...

Obviously, you'd need to `#define` N_ROWS and N_COLS as well.
 
C

Chris Dollin

deepak said:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

You've said "the variable `data` may hold a value which is a pointer to
pointer to float". You haven't (even in the snipped code) put such a
value into it. BOOM today.

You need to read up on `malloc` and pointers.
 
M

Marc Boyer

Le 13-06-2006 said:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;
return 0;

}

am i missing something fudamental thing...

Yes. You're failing to allocate any memory for your data.

float **data;

only declares a pointer to where your data is, no memory is allocated.

Try:

#include <stdlib.h>
...

data = malloc( N_ROWS * N_COLS * sizeof **data);

The problement of 2d array is not in the FAQ ?
With this malloc, you create a 1 flat dimensionnal array of N_ROWS *
N_COLS float.

But, notation data[0][0] assumes that data[0] is a pointer...

Marc Boyer
 
V

Vladimir Oka

Marc said:
Le 13-06-2006 said:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;
return 0;

}

am i missing something fudamental thing...

Yes. You're failing to allocate any memory for your data.

float **data;

only declares a pointer to where your data is, no memory is allocated.

Try:

#include <stdlib.h>
...

data = malloc( N_ROWS * N_COLS * sizeof **data);

The problement of 2d array is not in the FAQ ?

Yes it is: 6.16 said:
With this malloc, you create a 1 flat dimensionnal array of N_ROWS *
N_COLS float.

But, notation data[0][0] assumes that data[0] is a pointer...

Yes, I have bungled it.

I had a contiguous array in mind. From the FAQ:

int **array2 = malloc(nrows * sizeof(int *));
array2[0] = malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2 = array2[0] + i * ncolumns;
 
F

Frederick Gotham

deepak posted:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;


Firstly, specify the amount of rows and columns:

enum { cols = 8, rows = 8 };


Then choose either stack allocation:

float chessboard[cols][rows];

chessboard[0][0] = 23.23;


Or dynamic:

float (*chessboard)[rows] = malloc( sizeof( float[cols][rows] ) );

chessboard[0][0] = 23.45;
 
R

Richard Heathfield

Frederick Gotham said:
float (*chessboard)[rows] = malloc( sizeof( float[cols][rows] ) );

Better argument to malloc: sizeof *chessboard
chessboard[0][0] = 23.45;

Surely you're not going to do that before checking that chessboard != NULL?
 
A

August Karlstrom

Frederick said:
deepak posted:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;


Firstly, specify the amount of rows and columns:

enum { cols = 8, rows = 8 };

A slight misuse of the enum construct here, I think. The following is
probably clearer:

#define ROWS 8
#define COLS 8
Then choose either stack allocation:

float chessboard[cols][rows];

chessboard[0][0] = 23.23;


Or dynamic:

float (*chessboard)[rows] = malloc( sizeof( float[cols][rows] ) );

chessboard[0][0] = 23.45;


August
 
I

Ian Collins

August said:
Frederick said:
deepak posted:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;



Firstly, specify the amount of rows and columns:

enum { cols = 8, rows = 8 };


A slight misuse of the enum construct here, I think. The following is
probably clearer:

#define ROWS 8
#define COLS 8
Not realy, if some poor soul is debugging the code and wants to know the
value of rows, he'll have to find the #define, whereas with and enum, he
can check the value with his debugger.
 
F

Frederick Gotham

August Karlstrom posted:

A slight misuse of the enum construct here, I think. The following is
probably clearer:

#define ROWS 8
#define COLS 8



I don't know what the general consenus is in C, but coming from my
background of C++, macros are to be abhorred wherever an "enum" or
"typedef" can get the job done. There are many reasons why... here's just
one:

#define ROWS 8
#define COLS 8

int ArbitraryFunc( int ROWS, int COLS )
{
/* ... */
}
 
B

Barry Schwarz

deepak posted:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;


Firstly, specify the amount of rows and columns:

enum { cols = 8, rows = 8 };


Then choose either stack allocation:

C doesn't require a stack.
float chessboard[cols][rows];

chessboard[0][0] = 23.23;


Or dynamic:

float (*chessboard)[rows] = malloc( sizeof( float[cols][rows] ) );

This allocates too much memory. You want
float (*chessboard)[rows] = malloc(cols * sizeof (float[rows]);
or the equivalent but much preferred
float (*chessboard)[rows] = malloc(cols * sizeof *chessboard);
chessboard[0][0] = 23.45;


Remove del for email
 
B

Barry Schwarz

if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;

why this is alway giving some kind of memory error..

#include "stdio.h"
int main()
{
float **data;
data[0][0]=23.23;

You cannot dereference a pointer (which is what the [] operator does)
until you have initialized/assigned the pointer to point to some
memory you actually own. Both data and data[0] are uninitialized
pointers you have to deal with.
return 0;

}

am i missing something fudamental thing...

thanks in advance.


Remove del for email
 
R

Richard Heathfield

Frederick Gotham said:
August Karlstrom posted:





I don't know what the general consenus is in C, but coming from my
background of C++, macros are to be abhorred wherever an "enum" or
"typedef" can get the job done. There are many reasons why... here's just
one:

#define ROWS 8
#define COLS 8

int ArbitraryFunc( int ROWS, int COLS )
{
/* ... */
}

It is fairly widely accepted in C circles that objects and functions should
be given mixed or lower case identifiers, so in practice the above problem
tends not to arise. I would have no hesitation in using the #defines given
above, in a C program. In C++, I'd probably use a const int, because in C++
that is the idiom. But this is not a C++ newsgroup.
 
B

Barry Schwarz

deepak posted:
if i define like,

float **data;

then can't i do this in my programm..

data[0][0]=23.23;


Firstly, specify the amount of rows and columns:

enum { cols = 8, rows = 8 };


Then choose either stack allocation:

C doesn't require a stack.
float chessboard[cols][rows];

chessboard[0][0] = 23.23;


Or dynamic:

float (*chessboard)[rows] = malloc( sizeof( float[cols][rows] ) );

This allocates too much memory. You want
float (*chessboard)[rows] = malloc(cols * sizeof (float[rows]);
or the equivalent but much preferred
float (*chessboard)[rows] = malloc(cols * sizeof *chessboard);

Disregard. Jetlag leads to erroneous posts..
chessboard[0][0] = 23.45;


Remove del for email


Remove del for email
 
A

August Karlstrom

Frederick said:
August Karlstrom posted:





I don't know what the general consenus is in C, but coming from my
background of C++, macros are to be abhorred wherever an "enum" or
"typedef" can get the job done. There are many reasons why... here's just
one:

#define ROWS 8
#define COLS 8

int ArbitraryFunc( int ROWS, int COLS )
{
/* ... */
}
As Richard mentions further down the thread, using all caps for formal
parameters is bad practice.

A variable of an enumerated type holds a "symbol" (C as opposed to e.g.
Lisp don't have "real" symbols). The value of the "symbol" is (usually)
not important to the client. A typical example is

enum animal {CAT, DOG, BIRD};
...
enum animal x = BIRD;

If you aim for clarity, my suggestion to use `#define' is preferable.

Hint: If you were to give your anonymous type

enum { cols = 8, rows = 8 };

a name, what would that be? Moreover, e.g.

enum foo { cols = 8, rows = 8};
...
enum foo x = rows;

doesn't make sense.


August
 
F

Frederick Gotham

August Karlstrom posted:
Hint: If you were to give your anonymous type

enum { cols = 8, rows = 8 };

a name, what would that be? Moreover, e.g.

enum foo { cols = 8, rows = 8};
...
enum foo x = rows;

doesn't make sense.



I would choose ANYTHING over a macro, e.g.:

unsigned const rows = 8U;
unsigned const cols = 8U;
typedef unsigned Dimension;

Or:

enum Dimension { rows = 8U, cols = 8U };



I must admit that I don't understand why macros are not seen as as
respulsive in the general C community, as they are in the general C++
community.
 
K

Keith Thompson

August Karlstrom said:
A variable of an enumerated type holds a "symbol" (C as opposed to
e.g. Lisp don't have "real" symbols). The value of the "symbol" is
(usually) not important to the client. A typical example is

enum animal {CAT, DOG, BIRD};
...
enum animal x = BIRD;

If you aim for clarity, my suggestion to use `#define' is preferable.

Hint: If you were to give your anonymous type

enum { cols = 8, rows = 8 };

a name, what would that be? Moreover, e.g.

enum foo { cols = 8, rows = 8};
...
enum foo x = rows;

doesn't make sense.

Actually, using an enum declaration to declare named constants is a
fairly common idiom. It's arguably an abuse of the feature, but it's
also the best way to create a symbolic constant (at least one of type
int). A macro, though it's perfectly usable with care, brings in all
the problems of the preprocessor (lack of scope, counterintuitive
results in expressions, etc). A const declaration creates a read-only
variable, not a true constant.

So for the above, I might write:

enum { COLS = 8 };
enum { ROWS = 8 };

By separating COLS and ROWS into two separate declarations, this
avoids giving the impression that I'm creating a meaningful type.
Putting the names in all-caps makes it clear that they're constants.
And to answer your question, I wouldn't give such a type a name;
creating a type is just a side effect of what's intended to be a
constant declaration.
 
R

Richard Bos

Frederick Gotham said:
I would choose ANYTHING over a macro, e.g.:

unsigned const rows = 8U;
unsigned const cols = 8U;

Whyever the Us after the constants? It's not as if it changes anything.
typedef unsigned Dimension;

Or:

enum Dimension { rows = 8U, cols = 8U };

I must admit that I don't understand why macros are not seen as as
respulsive in the general C community, as they are in the general C++
community.

Possibly because C was created to get the job done properly, rather than
to prove a point.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top