Malloc problems

This is a discussion on Malloc problems within the C Programming forums, part of the General Programming Boards category; THis isn't working! Help! =P all = (words *) malloc(sizeof(words)); I get an error about the )! and it looks ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    Malloc problems

    THis isn't working! Help! =P



    all = (words *) malloc(sizeof(words));

    I get an error about the )!

    and it looks just fine to me

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Looks ok, but let's see what you have 'all' declared as. Also, you don't have to cast malloc, or any other void* in C.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    28
    stuct words *ALL[50];

    Is how words got defined!

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Rog
    Rog is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    Originally posted by Sebastiani
    Looks ok, but let's see what you have 'all' declared as. Also, you don't have to cast malloc, or any other void* in C.
    Right. Casting the pointer returned from malloc will suppress warning messages that could help to reveal subtle coding errors.

    I am always amazed at how many "experienced" programmers don't know this, and I'm always pleased when I find one who does.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >stuct words *ALL[50];
    >all = (words *) malloc(sizeof(words));

    In C, the keyword struct would need to precede words. C is case sensitive, so ALL and all are different things.

    Casting malloc
    It might be beneficial to use the last form.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using malloc
    By ulillillia in forum C Programming
    Replies: 34
    Last Post: 02-20-2008, 06:41 PM
  2. Problems with malloc
    By Zachary Lewis in forum C Programming
    Replies: 7
    Last Post: 02-18-2008, 06:45 AM
  3. Problems with pointers and malloc()
    By Deirdre in forum C Programming
    Replies: 3
    Last Post: 10-28-2007, 05:20 PM
  4. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  5. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21