LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2011, 12:30 PM   #1
instinct46
LQ Newbie
 
Registered: Dec 2005
Location: Liverpool
Distribution: Slackware
Posts: 18

Rep: Reputation: 0
Structure malloc problem


I'm having a problem where my opengl program is crashing, the reason why opengl wasn't in the title is because the error isn't a opengl related problem.

Their is something wrong with my code when I'm trying to malloc memory for a two-dimensional pointer. The problem is in the lines after the for loops, their are 2.

Code:
#include <stdio.h>
#include <stdlib.h>

#include "objectheader.h"


void defobj(struct objContain *tmpstruc){
int loopa = 0;

    tmpstruc->faces = 1;
    tmpstruc->type = 1;

    tmpstruc->xcods == (int**) malloc(sizeof(int) * (tmpstruc->faces));
    for(loopa = 0; loopa < tmpstruc->faces; loopa++)
        tmpstruc->xcods[loopa] = (int*) malloc(sizeof(int) * (tmpstruc->type + 3));

    tmpstruc->ycods == malloc(sizeof(int) * (tmpstruc->faces));
    for(loopa = 0; loopa < tmpstruc->faces; loopa++)
        tmpstruc->ycods[loopa] = (int*) malloc(sizeof(int) * (tmpstruc->type + 3));
}

Code:
#ifndef _OBJECT_HEADER_H_
#define _OBJECT_HEADER_H_

#include <stdio.h>

struct objContain{
    unsigned int faces;
    int type; //TRIANGLE = 0 OR SQUARE = 1
    int **xcods;
    int **ycods;
};

#endif

Last edited by instinct46; 09-07-2011 at 12:31 PM.
 
Old 09-07-2011, 12:35 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
This looks wrong:
Code:
   tmpstruc->xcods == (int**) malloc(sizeof(int) * (tmpstruc->faces));
    for(loopa = 0; loopa < tmpstruc->faces; loopa++)
        tmpstruc->xcods[loopa] = (int*) malloc(sizeof(int) * (tmpstruc->type + 3));
Suggested alternative:
Code:
   tmpstruc->xcods = (int**) malloc(sizeof(int *) * (tmpstruc->faces));
    for(loopa = 0; loopa < tmpstruc->faces; loopa++)
        tmpstruc->xcods[loopa] = (int*) malloc(sizeof(int) * (tmpstruc->type + 3));

Last edited by paulsm4; 09-07-2011 at 01:07 PM.
 
1 members found this post helpful.
Old 09-07-2011, 12:39 PM   #3
instinct46
LQ Newbie
 
Registered: Dec 2005
Location: Liverpool
Distribution: Slackware
Posts: 18

Original Poster
Rep: Reputation: 0
Haha cheers... might put the laptop down for a while and rest my eyes.
 
Old 09-07-2011, 12:51 PM   #4
ambrop7
Member
 
Registered: May 2011
Distribution: Gentoo
Posts: 98

Rep: Reputation: 16
This is still wrong. 'xcods' is declared as a pointer to an int pointer (int **). If you use it as a handle to a malloc'd array, the array must contain int pointers, not ints (which you're allocating).

Code:
tmpstruc->xcods = (int**) malloc(sizeof(int *) * (tmpstruc->faces));
for(loopa = 0; loopa < tmpstruc->faces; loopa++)
    tmpstruc->xcods[loopa] = (int*) malloc(sizeof(int) * (tmpstruc->type + 3));
The same can be achieved more nicely, avoiding such bugs:

Code:
tmpstruc->xcods = (int**) malloc(sizeof(tmpstruc->xcods[0]) * (tmpstruc->faces));
for(loopa = 0; loopa < tmpstruc->faces; loopa++)
    tmpstruc->xcods[loopa] = (int*) malloc(sizeof(tmpstruc->xcods[loopa][0]) * (tmpstruc->type + 3));
Also, in C, you don't need to cast the return value of malloc(), and any void pointer.

Last edited by ambrop7; 09-07-2011 at 12:55 PM.
 
1 members found this post helpful.
Old 09-07-2011, 01:30 PM   #5
instinct46
LQ Newbie
 
Registered: Dec 2005
Location: Liverpool
Distribution: Slackware
Posts: 18

Original Poster
Rep: Reputation: 0
I only cast to keep the compiler from popping up with lots of warnings :P

I didn't know you could do sizeof(tmpstruc->xcods[0])

Cheers
 
Old 09-07-2011, 01:40 PM   #6
ambrop7
Member
 
Registered: May 2011
Distribution: Gentoo
Posts: 98

Rep: Reputation: 16
Quote:
Originally Posted by instinct46 View Post
I only cast to keep the compiler from popping up with lots of warnings :P
That's probably because you're compiling it as C++. If you don't use C++, you should compile it as C (with gcc not g++).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C - How to malloc for a structure golmschenk Programming 5 03-08-2010 12:16 PM
Convert directory structure from long file names in Linux to DOS 8.3 structure? manorina Linux - Software 5 09-12-2009 09:18 AM
Help diagnosing malloc performance problem johnsfine Programming 6 08-30-2008 11:12 AM
A problem with malloc failing dogbird Linux - Software 1 11-17-2005 05:58 AM
problem using malloc in C huble Programming 10 03-27-2005 12:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration