By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Really basic..

P: n/a
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.

Jun 13 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a

deepak wrote:
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 */;
...

Jun 13 '06 #2

P: n/a

Vladimir Oka wrote:
deepak wrote:
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.

Jun 13 '06 #3

P: n/a
deepak wrote:
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.

--
Chris "seeker" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Jun 13 '06 #4

P: n/a
Le 13-06-2006, Vladimir Oka <no****@btopenworld.com> a écrit*:

deepak wrote:
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
Jun 13 '06 #5

P: n/a

Marc Boyer wrote:
Le 13-06-2006, Vladimir Oka <no****@btopenworld.com> a écrit :

deepak wrote:
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 <http://c-faq.com/aryptr/dynmuldimary.html>.
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[i] = array2[0] + i * ncolumns;

Jun 13 '06 #6

P: n/a
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;

--

Frederick Gotham
Jun 17 '06 #7

P: n/a
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?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 17 '06 #8

P: n/a
Frederick Gotham wrote:
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
Jun 18 '06 #9

P: n/a
August Karlstrom wrote:
Frederick Gotham wrote:
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.

--
Ian Collins.
Jun 18 '06 #10

P: n/a
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 )
{
/* ... */
}

--

Frederick Gotham
Jun 18 '06 #11

P: n/a
On Sat, 17 Jun 2006 19:03:01 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote:
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
Jun 18 '06 #12

P: n/a
On 13 Jun 2006 01:44:39 -0700, "deepak" <dk*******@gmail.com> wrote:
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
Jun 18 '06 #13

P: n/a
Frederick Gotham said:
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 )
{
/* ... */
}


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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 18 '06 #14

P: n/a
On Sat, 17 Jun 2006 18:38:39 -0700, Barry Schwarz <sc******@doezl.net>
wrote:
On Sat, 17 Jun 2006 19:03:01 GMT, Frederick Gotham
<fg*******@SPAM.com> wrote:
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
Jun 18 '06 #15

P: n/a
Frederick Gotham wrote:
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 )
{
/* ... */
}

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
Jun 20 '06 #16

P: n/a
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.
--

Frederick Gotham
Jun 20 '06 #17

P: n/a
August Karlstrom <fu********@comhem.se> writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 20 '06 #18

P: n/a
Frederick Gotham <fg*******@SPAM.com> wrote:
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
Jun 21 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.